lib/mongoid/association/embedded/embedded_in/proxy.rb in mongoid-8.1.7 vs lib/mongoid/association/embedded/embedded_in/proxy.rb in mongoid-9.0.0

- old
+ new

@@ -2,25 +2,29 @@ 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 + # class inherits from Mongoid::Association::Proxy and forwards + # 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. # Association::Embedded::EmbeddedIn.new(person, address, association) # # @param [ Document ] base The document the association hangs off of. # @param [ Document ] target The target (parent) of the association. - # @param [ Association ] association The association metadata. + # @param [ Mongoid::Association::Relatable ] association The association metadata. # # @return [ In ] The proxy. def initialize(base, target, association) - init(base, target, association) do + super do characterize_one(_target) bind_one end end @@ -63,13 +67,11 @@ # @example Set the base association. # object.characterize_one(document) # # @param [ Document ] document The document to set the association metadata on. def characterize_one(document) - unless _base._association - _base._association = _association.inverse_association(document) - end + _base._association ||= _association.inverse_association(document) end # Are we able to persist this association? # # @example Can we persist the association? @@ -79,9 +81,21 @@ def persistable? _target.persisted? && !_binding? && !_building? end class << self + # Returns the eager loader for this association. + # + # @param [ Array<Mongoid::Association> ] associations The + # associations to be eager loaded + # @param [ Array<Mongoid::Document> ] docs The parent documents + # that possess the given associations, which ought to be + # populated by the eager-loaded documents. + # + # @return [ Mongoid::Association::Embedded::Eager ] + def eager_loader(associations, docs) + Eager.new(associations, docs) + end # Returns true if the association is an embedded one. In this case # always true. # # @example Is this association embedded?