lib/mongoid/association/macros.rb in mongoid-7.3.5 vs lib/mongoid/association/macros.rb in mongoid-7.4.0

- old
+ new

@@ -1,7 +1,6 @@ # frozen_string_literal: true -# encoding: utf-8 module Mongoid module Association # This module contains the core macros for defining associations between @@ -11,23 +10,24 @@ included do class_attribute :embedded, instance_reader: false class_attribute :embedded_relations class_attribute :relations + # @api private + class_attribute :aliased_associations self.embedded = false self.embedded_relations = BSON::Document.new self.relations = BSON::Document.new + self.aliased_associations = {} end # This is convenience for libraries still on the old API. # # @example Get the associations. # person.associations # # @return [ Hash ] The associations. - # - # @since 2.3.1 def associations self.relations end module ClassMethods @@ -162,12 +162,10 @@ # end # # @param [ Symbol ] name The name of the association. # @param [ Hash ] options The association options. # @param [ Proc ] block Optional block for defining extensions. - # - # @since 2.0.0.rc.1 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 @@ -196,9 +194,12 @@ 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) + if assoc.respond_to?(:store_as) && assoc.store_as != name + self.aliased_associations[assoc.store_as] = name + end end end end end end