Skip to content

Commit

Permalink
Avoid double hash lookups in AR::Reflection when reflecting associati…
Browse files Browse the repository at this point in the history
…ons/aggregations
  • Loading branch information
carlosantoniodasilva committed Mar 3, 2012
1 parent 14f06dd commit a767357
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions activerecord/lib/active_record/reflection.rb
Expand Up @@ -23,11 +23,11 @@ module Reflection # :nodoc:
module ClassMethods
def create_reflection(macro, name, options, active_record)
case macro
when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
klass = options[:through] ? ThroughReflection : AssociationReflection
reflection = klass.new(macro, name, options, active_record)
when :composed_of
reflection = AggregateReflection.new(macro, name, options, active_record)
when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
klass = options[:through] ? ThroughReflection : AssociationReflection
reflection = klass.new(macro, name, options, active_record)
when :composed_of
reflection = AggregateReflection.new(macro, name, options, active_record)
end

self.reflections = self.reflections.merge(name => reflection)
Expand All @@ -44,7 +44,8 @@ def reflect_on_all_aggregations
# Account.reflect_on_aggregation(:balance) # => the balance AggregateReflection
#
def reflect_on_aggregation(aggregation)
reflections[aggregation].is_a?(AggregateReflection) ? reflections[aggregation] : nil
reflection = reflections[aggregation]
reflection if reflection.is_a?(AggregateReflection)
end

# Returns an array of AssociationReflection objects for all the
Expand All @@ -68,7 +69,8 @@ def reflect_on_all_associations(macro = nil)
# Invoice.reflect_on_association(:line_items).macro # returns :has_many
#
def reflect_on_association(association)
reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
reflection = reflections[association]
reflection if reflection.is_a?(AssociationReflection)
end

# Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
Expand All @@ -77,7 +79,6 @@ def reflect_on_all_autosave_associations
end
end


# Abstract base class for AggregateReflection and AssociationReflection. Objects of
# AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.
class MacroReflection
Expand Down Expand Up @@ -452,7 +453,6 @@ def conditions
# Recursively fill out the rest of the array from the through reflection
conditions += through_conditions

# And return
conditions
end
end
Expand Down

0 comments on commit a767357

Please sign in to comment.