From 11a5fd4d7d2eac970d9f30a79a117ff464744d04 Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 17 Jan 2024 12:31:51 +0900 Subject: [PATCH] Additional backport --- lib/mongoid/association/bindable.rb | 6 +++--- lib/mongoid/association/embedded/embedded_in/proxy.rb | 2 -- lib/mongoid/association/embedded/embeds_many/proxy.rb | 2 -- lib/mongoid/association/embedded/embeds_one/proxy.rb | 2 -- lib/mongoid/association/macros.rb | 2 -- lib/mongoid/association/nested/many.rb | 4 +++- lib/mongoid/association/proxy.rb | 10 ++++------ lib/mongoid/association/referenced/belongs_to/proxy.rb | 2 -- .../referenced/has_and_belongs_to_many/proxy.rb | 4 ---- lib/mongoid/association/referenced/has_many/proxy.rb | 1 - lib/mongoid/association/referenced/has_one/eager.rb | 1 - lib/mongoid/association/referenced/has_one/proxy.rb | 2 -- lib/mongoid/deprecation.rb | 6 +++--- 13 files changed, 13 insertions(+), 31 deletions(-) diff --git a/lib/mongoid/association/bindable.rb b/lib/mongoid/association/bindable.rb index 8896e2334..e5b27d953 100644 --- a/lib/mongoid/association/bindable.rb +++ b/lib/mongoid/association/bindable.rb @@ -135,7 +135,7 @@ def bind_foreign_key(keyed, id) # @param [ Mongoid::Document ] typed The document that stores the type field. # @param [ String ] name The name of the model. def bind_polymorphic_type(typed, name) - return unless _association.type + return unless _association.type && !typed.frozen? try_method(typed, _association.type_setter, name) end @@ -151,7 +151,7 @@ def bind_polymorphic_type(typed, name) # @param [ Mongoid::Document ] typed The document that stores the type field. # @param [ String ] name The name of the model. def bind_polymorphic_inverse_type(typed, name) - return unless _association.inverse_type + return unless _association.inverse_type && !typed.frozen? try_method(typed, _association.inverse_type_setter, name) end @@ -167,7 +167,7 @@ def bind_polymorphic_inverse_type(typed, name) # @param [ Mongoid::Document ] doc The base document. # @param [ Mongoid::Document ] inverse The inverse document. def bind_inverse(doc, inverse) - return unless doc.respond_to?(_association.inverse_setter) + return unless doc.respond_to?(_association.inverse_setter) && !doc.frozen? try_method(doc, _association.inverse_setter, inverse) end diff --git a/lib/mongoid/association/embedded/embedded_in/proxy.rb b/lib/mongoid/association/embedded/embedded_in/proxy.rb index d91d446d3..e5278652c 100644 --- a/lib/mongoid/association/embedded/embedded_in/proxy.rb +++ b/lib/mongoid/association/embedded/embedded_in/proxy.rb @@ -4,7 +4,6 @@ module Mongoid module Association module Embedded class EmbeddedIn - # Transparent proxy for embedded_in associations. # An instance of this class is returned when calling the # association getter method on the child document. This @@ -12,7 +11,6 @@ class EmbeddedIn # most of its methods to the target of the association, i.e. # the parent document. class Proxy < Association::One - # Instantiate a new embedded_in association. # # @example Create the new association. diff --git a/lib/mongoid/association/embedded/embeds_many/proxy.rb b/lib/mongoid/association/embedded/embeds_many/proxy.rb index 19e544c8e..4472e51b9 100644 --- a/lib/mongoid/association/embedded/embeds_many/proxy.rb +++ b/lib/mongoid/association/embedded/embeds_many/proxy.rb @@ -6,7 +6,6 @@ module Mongoid module Association module Embedded class EmbedsMany - # Transparent proxy for embeds_many associations. # An instance of this class is returned when calling the # association getter method on the parent document. This @@ -18,7 +17,6 @@ class Proxy < Association::Many # Class-level methods for the Proxy class. module ClassMethods - # Returns the eager loader for this association. # # @param [ Array ] associations The diff --git a/lib/mongoid/association/embedded/embeds_one/proxy.rb b/lib/mongoid/association/embedded/embeds_one/proxy.rb index 5dbe9ccb0..d6578eb3c 100644 --- a/lib/mongoid/association/embedded/embeds_one/proxy.rb +++ b/lib/mongoid/association/embedded/embeds_one/proxy.rb @@ -4,7 +4,6 @@ module Mongoid module Association module Embedded class EmbedsOne - # Transparent proxy for embeds_one associations. # An instance of this class is returned when calling the # association getter method on the parent document. This @@ -12,7 +11,6 @@ class EmbedsOne # most of its methods to the target of the association, i.e. # the child document. class Proxy < Association::One - # The valid options when defining this association. # # @return [ Array ] The allowed options when defining this association. diff --git a/lib/mongoid/association/macros.rb b/lib/mongoid/association/macros.rb index 627cb39be..2a20e452d 100644 --- a/lib/mongoid/association/macros.rb +++ b/lib/mongoid/association/macros.rb @@ -2,7 +2,6 @@ module Mongoid module Association - # This module contains the core macros for defining associations between # documents. They can be either embedded or referenced. module Macros @@ -58,7 +57,6 @@ def associations # Class methods for associations. module ClassMethods - # Adds the association back to the parent document. This macro is # necessary to set the references from the child back to the parent # document. If a child does not define this association calling diff --git a/lib/mongoid/association/nested/many.rb b/lib/mongoid/association/nested/many.rb index 5400670c6..743bf3000 100644 --- a/lib/mongoid/association/nested/many.rb +++ b/lib/mongoid/association/nested/many.rb @@ -176,7 +176,9 @@ def update_nested_relation(parent, id, attrs) first = existing.first converted = first ? convert_id(first.class, id) : id - if existing.exists?(_id: converted) + # The next line cannot be written as `existing.exists?(_id: converted)`, + # otherwise tests will fail. + if existing.where(_id: converted).exists? # rubocop:disable Rails/WhereExists # document exists in association doc = existing.find(converted) if destroyable?(attrs) diff --git a/lib/mongoid/association/proxy.rb b/lib/mongoid/association/proxy.rb index 4c15658fc..650a8df85 100644 --- a/lib/mongoid/association/proxy.rb +++ b/lib/mongoid/association/proxy.rb @@ -4,16 +4,17 @@ module Mongoid module Association - # This class is the superclass for all association proxy objects, and contains # common behavior for all of them. class Proxy extend Forwardable + alias_method :extend_proxy, :extend + # Specific methods to prevent from being undefined. # # @api private - KEEP_METHODS = %i[ + KEEPER_METHODS = %i[ send object_id equal? @@ -25,11 +26,9 @@ class Proxy extend_proxies ].freeze - alias_method :extend_proxy, :extend - # We undefine most methods to get them sent through to the target. instance_methods.each do |method| - next if method.to_s.start_with?('__') || KEEP_METHODS.include?(method) + next if method.to_s.start_with?('__') || KEEPER_METHODS.include?(method) undef_method(method) end @@ -201,7 +200,6 @@ def execute_callbacks_around(name, doc) end class << self - # Apply ordering to the criteria if it was defined on the association. # # @example Apply the ordering. diff --git a/lib/mongoid/association/referenced/belongs_to/proxy.rb b/lib/mongoid/association/referenced/belongs_to/proxy.rb index 0735e6641..2d6ec8316 100644 --- a/lib/mongoid/association/referenced/belongs_to/proxy.rb +++ b/lib/mongoid/association/referenced/belongs_to/proxy.rb @@ -4,7 +4,6 @@ module Mongoid module Association module Referenced class BelongsTo - # Transparent proxy for belong_to associations. # An instance of this class is returned when calling the # association getter method on the subject document. @@ -99,7 +98,6 @@ def persistable? end class << self - # Get the Eager object for this type of association. # # @example Get the eager loader object diff --git a/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb b/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb index 04c454688..76b88c785 100644 --- a/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +++ b/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb @@ -4,7 +4,6 @@ module Mongoid module Association module Referenced class HasAndBelongsToMany - # Transparent proxy for has_and_belongs_to_many associations. # An instance of this class is returned when calling # the association getter method on the subject document. @@ -13,10 +12,8 @@ class HasAndBelongsToMany # i.e. the array of documents on the opposite-side collection # which must be loaded. class Proxy < Referenced::HasMany::Proxy - # Class-level methods for HasAndBelongsToMany::Proxy module ClassMethods - # Get the eager loader object for this type of association. # # @example Get the eager loader object @@ -90,7 +87,6 @@ def <<(*args) end unsynced(_base, foreign_key) and self end - alias_method :push, :<< # Appends an array of documents to the association. Performs a batch diff --git a/lib/mongoid/association/referenced/has_many/proxy.rb b/lib/mongoid/association/referenced/has_many/proxy.rb index 67ae50396..2f9407682 100644 --- a/lib/mongoid/association/referenced/has_many/proxy.rb +++ b/lib/mongoid/association/referenced/has_many/proxy.rb @@ -4,7 +4,6 @@ module Mongoid module Association module Referenced class HasMany - # Transparent proxy for has_many associations. # An instance of this class is returned when calling the # association getter method on the subject document. This class diff --git a/lib/mongoid/association/referenced/has_one/eager.rb b/lib/mongoid/association/referenced/has_one/eager.rb index 397ad88d0..911bf5658 100644 --- a/lib/mongoid/association/referenced/has_one/eager.rb +++ b/lib/mongoid/association/referenced/has_one/eager.rb @@ -4,7 +4,6 @@ module Mongoid module Association module Referenced class HasOne - # Eager class for has_one associations. class Eager < Association::Eager diff --git a/lib/mongoid/association/referenced/has_one/proxy.rb b/lib/mongoid/association/referenced/has_one/proxy.rb index 676594e36..8070d097e 100644 --- a/lib/mongoid/association/referenced/has_one/proxy.rb +++ b/lib/mongoid/association/referenced/has_one/proxy.rb @@ -4,7 +4,6 @@ module Mongoid module Association module Referenced class HasOne - # Transparent proxy for has_one associations. # An instance of this class is returned when calling the # association getter method on the subject document. This class @@ -12,7 +11,6 @@ class HasOne # its methods to the target of the association, i.e. the # document on the opposite-side collection which must be loaded. class Proxy < Association::One - # Class-level methods for HasOne::Proxy module ClassMethods diff --git a/lib/mongoid/deprecation.rb b/lib/mongoid/deprecation.rb index fd7e261cc..a7f26639d 100644 --- a/lib/mongoid/deprecation.rb +++ b/lib/mongoid/deprecation.rb @@ -17,10 +17,10 @@ def initialize # # @return [ Array ] The deprecation behavior. def behavior - @behavior ||= Array(lambda do |message, callstack, _deprecation_horizon, _gem_name| + @behavior ||= Array(lambda do |*args| logger = Mongoid.logger - logger.warn(message) - logger.debug(callstack.join("\n ")) if debug + logger.warn(args[0]) + logger.debug(args[1].join("\n ")) if debug end) end end