Skip to content
Browse files

Delegate through_reflection and source_reflection to reflection

  • Loading branch information...
1 parent 32eef69 commit 8b00da52586229f7ad6ecf8af455924f604eb282 @jonleighton jonleighton committed with tenderlove Feb 20, 2011
View
20 activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -43,7 +43,7 @@ def insert_record(record, validate = true)
private
def through_record(record)
- through_association = owner.association(reflection.through_reflection.name)
+ through_association = owner.association(through_reflection.name)
attributes = construct_join_attributes(record)
through_record = Array.wrap(through_association.target).find { |candidate|
@@ -52,7 +52,7 @@ def through_record(record)
unless through_record
through_record = through_association.build(attributes)
- through_record.send("#{reflection.source_reflection.name}=", record)
+ through_record.send("#{source_reflection.name}=", record)
end
through_record
@@ -61,7 +61,7 @@ def through_record(record)
def build_record(attributes)
record = super(attributes)
- inverse = reflection.source_reflection.inverse_of
+ inverse = source_reflection.inverse_of
if inverse
if inverse.macro == :has_many
record.send(inverse.name) << through_record(record)
@@ -74,7 +74,7 @@ def build_record(attributes)
end
def target_reflection_has_associated_record?
- if reflection.through_reflection.macro == :belongs_to && owner[reflection.through_reflection.foreign_key].blank?
+ if through_reflection.macro == :belongs_to && owner[through_reflection.foreign_key].blank?
false
else
true
@@ -84,7 +84,7 @@ def target_reflection_has_associated_record?
def update_through_counter?(method)
case method
when :destroy
- !inverse_updates_counter_cache?(reflection.through_reflection)
+ !inverse_updates_counter_cache?(through_reflection)
when :nullify
false
else
@@ -93,29 +93,29 @@ def update_through_counter?(method)
end
def delete_records(records, method)
- through = owner.association(reflection.through_reflection.name)
+ through = owner.association(through_reflection.name)
scope = through.scoped.where(construct_join_attributes(*records))
case method
when :destroy
count = scope.destroy_all.length
when :nullify
- count = scope.update_all(reflection.source_reflection.foreign_key => nil)
+ count = scope.update_all(source_reflection.foreign_key => nil)
else
count = scope.delete_all
end
delete_through_records(through, records)
- if reflection.through_reflection.macro == :has_many && update_through_counter?(method)
- update_counter(-count, reflection.through_reflection)
+ if through_reflection.macro == :has_many && update_through_counter?(method)
+ update_counter(-count, through_reflection)
end
update_counter(-count)
end
def delete_through_records(through, records)
- if reflection.through_reflection.macro == :has_many
+ if through_reflection.macro == :has_many
records.each do |record|
through.target.delete(through_record(record))
end
View
2 activerecord/lib/active_record/associations/has_one_through_association.rb
@@ -12,7 +12,7 @@ def replace(record)
private
def create_through_record(record)
- through_proxy = owner.association(reflection.through_reflection.name)
+ through_proxy = owner.association(through_reflection.name)
through_record = through_proxy.send(:load_target)
if through_record && !record
View
46 activerecord/lib/active_record/associations/through_association.rb
@@ -3,12 +3,12 @@ module ActiveRecord
module Associations
module ThroughAssociation #:nodoc:
- delegate :source_options, :through_options, :to => :reflection
+ delegate :source_options, :through_options, :source_reflection, :through_reflection, :to => :reflection
protected
def target_scope
- super.merge(reflection.through_reflection.klass.scoped)
+ super.merge(through_reflection.klass.scoped)
end
def association_scope
@@ -31,15 +31,15 @@ def creation_attributes
end
def aliased_through_table
- name = reflection.through_reflection.table_name
+ name = through_reflection.table_name
reflection.table_name == name ?
- reflection.through_reflection.klass.arel_table.alias(name + "_join") :
- reflection.through_reflection.klass.arel_table
+ through_reflection.klass.arel_table.alias(name + "_join") :
+ through_reflection.klass.arel_table
end
def construct_owner_conditions
- super(aliased_through_table, reflection.through_reflection)
+ super(aliased_through_table, through_reflection)
end
def construct_joins
@@ -48,23 +48,23 @@ def construct_joins
conditions = []
- if reflection.source_reflection.macro == :belongs_to
- reflection_primary_key = reflection.source_reflection.association_primary_key
- source_primary_key = reflection.source_reflection.foreign_key
+ if source_reflection.macro == :belongs_to
+ reflection_primary_key = source_reflection.association_primary_key
+ source_primary_key = source_reflection.foreign_key
if options[:source_type]
- column = reflection.source_reflection.foreign_type
+ column = source_reflection.foreign_type
conditions <<
right[column].eq(options[:source_type])
end
else
- reflection_primary_key = reflection.source_reflection.foreign_key
- source_primary_key = reflection.source_reflection.active_record_primary_key
+ reflection_primary_key = source_reflection.foreign_key
+ source_primary_key = source_reflection.active_record_primary_key
if source_options[:as]
column = "#{source_options[:as]}_type"
conditions <<
- left[column].eq(reflection.through_reflection.klass.name)
+ left[column].eq(through_reflection.klass.name)
end
end
@@ -89,19 +89,19 @@ def construct_joins
# situation it is more natural for the user to just create or modify their join records
# directly as required.
def construct_join_attributes(*records)
- if reflection.source_reflection.macro != :belongs_to
+ if source_reflection.macro != :belongs_to
raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
end
join_attributes = {
- reflection.source_reflection.foreign_key =>
+ source_reflection.foreign_key =>
records.map { |record|
- record.send(reflection.source_reflection.association_primary_key)
+ record.send(source_reflection.association_primary_key)
}
}
if options[:source_type]
- join_attributes[reflection.source_reflection.foreign_type] =
+ join_attributes[source_reflection.foreign_type] =
records.map { |record| record.class.base_class.name }
end
@@ -117,8 +117,8 @@ def construct_join_attributes(*records)
# has a different meaning to scope.where(x).where(y) - the first version might
# perform some substitution if x is a string.
def add_conditions(scope)
- unless reflection.through_reflection.klass.descends_from_active_record?
- scope = scope.where(reflection.through_reflection.klass.send(:type_condition))
+ unless through_reflection.klass.descends_from_active_record?
+ scope = scope.where(through_reflection.klass.send(:type_condition))
end
scope = scope.where(interpolate(source_options[:conditions]))
@@ -144,14 +144,14 @@ def through_conditions
end
def stale_state
- if reflection.through_reflection.macro == :belongs_to
- owner[reflection.through_reflection.foreign_key].to_s
+ if through_reflection.macro == :belongs_to
+ owner[through_reflection.foreign_key].to_s
end
end
def foreign_key_present?
- reflection.through_reflection.macro == :belongs_to &&
- !owner[reflection.through_reflection.foreign_key].nil?
+ through_reflection.macro == :belongs_to &&
+ !owner[through_reflection.foreign_key].nil?
end
end
end

0 comments on commit 8b00da5

Please sign in to comment.
Something went wrong with that request. Please try again.