Skip to content

Commit

Permalink
Add has_and_belongs_to_many back to reflections hash.
Browse files Browse the repository at this point in the history
This adds back `has_and_belongs_to_many` reflection to the reflections
hash. Even thought we are not using that internally, as
`Model.reflections` is a public method we need to keep the hash of
reflections coherent.

ad7b5ef is the commit that made
has_and_belongs_to_many became a has_many :through and a has_many.

[fixes #14682]
  • Loading branch information
arthurnn committed May 23, 2014
1 parent 78169e1 commit bb6861d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1606,6 +1606,9 @@ def destroy_associations
end

has_many name, scope, hm_options, &extension

reflection = ActiveRecord::Reflection::AssociationReflection.new(:has_and_belongs_to_many, name, scope, options, self)
self.reflections = self.reflections.except(middle_reflection.name).merge!(name => reflection)
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions activerecord/lib/active_record/reflection.rb
Expand Up @@ -4,8 +4,10 @@ module Reflection # :nodoc:
extend ActiveSupport::Concern

included do
class_attribute :_reflections
class_attribute :reflections
class_attribute :aggregate_reflections
self._reflections = {}
self.reflections = {}
self.aggregate_reflections = {}
end
Expand All @@ -22,6 +24,7 @@ def self.create(macro, name, scope, options, ar)
end

def self.add_reflection(ar, name, reflection)
ar._reflections = ar._reflections.merge(name => reflection)
ar.reflections = ar.reflections.merge(name => reflection)
end

Expand Down Expand Up @@ -62,7 +65,7 @@ def reflect_on_aggregation(aggregation)
# Account.reflect_on_all_associations(:has_many) # returns an array of all has_many associations
#
def reflect_on_all_associations(macro = nil)
association_reflections = reflections.values
association_reflections = :has_and_belongs_to_many == macro ? reflections.values : _reflections.values
macro ? association_reflections.select { |reflection| reflection.macro == macro } : association_reflections
end

Expand All @@ -77,7 +80,7 @@ def reflect_on_association(association)

# Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
def reflect_on_all_autosave_associations
reflections.values.select { |reflection| reflection.options[:autosave] }
_reflections.values.select { |reflection| reflection.options[:autosave] }
end
end

Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/reflection_test.rb
Expand Up @@ -200,7 +200,12 @@ def test_association_reflection_in_modules
end

def test_reflection_should_not_raise_error_when_compared_to_other_object
assert_nothing_raised { Firm.reflections[:clients] == Object.new }
assert_nothing_raised { Firm._reflections[:clients] == Object.new }
end

def test_has_and_belongs_to_many_reflection
assert_equal :has_and_belongs_to_many, Category.reflections[:posts].macro
assert_equal :posts, Category.reflect_on_all_associations(:has_and_belongs_to_many).first.name
end

def test_has_many_through_reflection
Expand Down

0 comments on commit bb6861d

Please sign in to comment.