lib/mongoid/association/macros.rb in mongoid-8.1.7 vs lib/mongoid/association/macros.rb in mongoid-9.0.0

- old
+ new

@@ -1,10 +1,9 @@ # frozen_string_literal: true module Mongoid module Association - # This module contains the core macros for defining associations between # documents. They can be either embedded or referenced. module Macros extend ActiveSupport::Concern @@ -33,11 +32,11 @@ # @return [ Hash<String, String> ] The aliased associations hash. # # @api private class_attribute :aliased_associations - # @return [ Set<String> ] The set of associations that are configured + # @return [ Set<String> ] The set of associations that are configured # with :store_as parameter. class_attribute :stored_as_associations self.embedded = false self.embedded_relations = BSON::Document.new @@ -51,15 +50,15 @@ # @example Get the associations. # person.associations # # @return [ Hash ] The associations. def associations - self.relations + relations end + # 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 # persistence methods on the child object will cause a save to fail. # @@ -75,11 +74,11 @@ # embedded_in :person # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. - # @param [ Proc ] block Optional block for defining extensions. + # @param &block Optional block for defining extensions. def embedded_in(name, options = {}, &block) define_association!(__method__, name, options, &block) end # Adds the association from a parent document to its children. The name @@ -98,11 +97,11 @@ # embedded_in :person # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. - # @param [ Proc ] block Optional block for defining extensions. + # @param &block Optional block for defining extensions. def embeds_many(name, options = {}, &block) define_association!(__method__, name, options, &block) end # Adds the association from a parent document to its child. The name @@ -121,11 +120,11 @@ # embedded_in :person # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. - # @param [ Proc ] block Optional block for defining extensions. + # @param &block Optional block for defining extensions. def embeds_one(name, options = {}, &block) define_association!(__method__, name, options, &block) end # Adds a referenced association from the child Document to a Document @@ -143,15 +142,17 @@ # has_one :game # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. - # @param [ Proc ] block Optional block for defining extensions. + # @param &block Optional block for defining extensions. def belongs_to(name, options = {}, &block) define_association!(__method__, name, options, &block) end + # rubocop:disable Naming/PredicateName + # Adds a referenced association from a parent Document to many # Documents in another database or collection. # # @example Define the association. # @@ -165,11 +166,11 @@ # belongs_to :person # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. - # @param [ Proc ] block Optional block for defining extensions. + # @param &block Optional block for defining extensions. def has_many(name, options = {}, &block) define_association!(__method__, name, options, &block) end # Adds a referenced many-to-many association between many of this @@ -187,11 +188,11 @@ # has_and_belongs_to_many :people # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. - # @param [ Proc ] block Optional block for defining extensions. + # @param &block Optional block for defining extensions. def has_and_belongs_to_many(name, options = {}, &block) define_association!(__method__, name, options, &block) end # Adds a referenced association from the child Document to a Document @@ -209,23 +210,25 @@ # has_one :game # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. - # @param [ Proc ] block Optional block for defining extensions. + # @param &block Optional block for defining extensions. def has_one(name, options = {}, &block) define_association!(__method__, name, options, &block) end + # rubocop:enable Naming/PredicateName + private def define_association!(macro_name, name, options = {}, &block) Association::MACRO_MAPPING[macro_name].new(self, name, options, &block).tap do |assoc| assoc.setup! - self.relations = self.relations.merge(name => assoc) + self.relations = relations.merge(name => assoc) if assoc.embedded? && assoc.respond_to?(:store_as) && assoc.store_as != name - self.aliased_associations[assoc.store_as] = name - self.stored_as_associations << assoc.store_as + aliased_associations[assoc.store_as] = name + stored_as_associations << assoc.store_as end end end end end