Permalink
Browse files

Refactoring .reflections public method.

Now the internal reflections will hold a reference to its public
representation, so when the outside world calls `Account.reflection` we
can build a list of public reflections.
  • Loading branch information...
1 parent 6259e4e commit 00b024218f1c91fde1217ae4951bd2c817bc9ea9 @arthurnn arthurnn committed May 24, 2014
@@ -1577,6 +1577,8 @@ def has_and_belongs_to_many(name, scope = nil, options = {}, &extension)
scope = nil
end
+ habtm_reflection = ActiveRecord::Reflection::AssociationReflection.new(:has_and_belongs_to_many, name, scope, options, self)
+
builder = Builder::HasAndBelongsToMany.new name, self, options
join_model = builder.through_model
@@ -1590,6 +1592,7 @@ def has_and_belongs_to_many(name, scope = nil, options = {}, &extension)
Builder::HasMany.define_callbacks self, middle_reflection
Reflection.add_reflection self, middle_reflection.name, middle_reflection
+ middle_reflection.parent_reflection = [name.to_s, habtm_reflection]
include Module.new {
class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -1610,9 +1613,7 @@ 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.to_s => reflection)
+ self._reflections[name.to_s].parent_reflection = [name.to_s, habtm_reflection]
end
end
end
@@ -166,7 +166,7 @@ def destroy_row
end
def each_counter_cached_associations
- reflections.each do |name, reflection|
+ _reflections.each do |name, reflection|
yield association(name) if reflection.belongs_to? && reflection.counter_cache_column
end
end
@@ -307,7 +307,6 @@ def accepts_nested_attributes_for(*attr_names)
attr_names.each do |association_name|
if reflection = _reflect_on_association(association_name)
reflection.autosave = true
- reflect_on_association(association_name).autosave = true
add_autosave_association_callbacks(reflection)
nested_attributes_options = self.nested_attributes_options.dup
@@ -7,11 +7,8 @@ module Reflection # :nodoc:
included do
class_attribute :_reflections
- # @api public
- class_attribute :reflections
class_attribute :aggregate_reflections
self._reflections = {}
- self.reflections = {}
self.aggregate_reflections = {}
end
@@ -28,7 +25,6 @@ def self.create(macro, name, scope, options, ar)
def self.add_reflection(ar, name, reflection)
ar._reflections = ar._reflections.merge(name.to_s => reflection)
- ar.reflections = ar.reflections.merge(name.to_s => reflection)
end
def self.add_aggregate_reflection(ar, name, reflection)
@@ -57,6 +53,24 @@ def reflect_on_aggregation(aggregation)
aggregate_reflections[aggregation.to_s]
end
+ # Returns a Hash of name of the reflection as the key and a AssociationReflection as the value.
+ #
+ # Account.reflections # => {balance: AggregateReflection}
+ #
+ # @api public
+ def reflections
+ ref = {}
+ _reflections.each do |name, reflection|
+ parent_name, parent_reflection = reflection.parent_reflection
+ if parent_name
+ ref[parent_name] = parent_reflection
+ else
+ ref[name] = reflection
+ end
+ end
+ ref
+ end
+
# Returns an array of AssociationReflection objects for all the
# associations in the class. If you only want to reflect on a certain
# association type, pass in the symbol (<tt>:has_many</tt>, <tt>:has_one</tt>,
@@ -142,6 +156,10 @@ def initialize(macro, name, scope, options, active_record)
def autosave=(autosave)
@automatic_inverse_of = false
@options[:autosave] = autosave
+ _, parent_reflection = self.parent_reflection
+ if parent_reflection
+ parent_reflection.autosave = autosave
+ end
end
# Returns the class for the macro.
@@ -206,6 +224,7 @@ def klass
end
attr_reader :type, :foreign_type
+ attr_accessor :parent_reflection # [:name, Reflection]
def initialize(macro, name, scope, options, active_record)
super

0 comments on commit 00b0242

Please sign in to comment.