diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-02-20 20:44:29 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-21 10:16:14 -0800 |
commit | 8b00da52586229f7ad6ecf8af455924f604eb282 (patch) | |
tree | 7ea4b14e3ad6ed3cdd10560f26ad3306d7650de4 /activerecord/lib/active_record | |
parent | 32eef69dc1abbf9b67de780a882754e1717c2a3b (diff) | |
download | rails-8b00da52586229f7ad6ecf8af455924f604eb282.tar.gz rails-8b00da52586229f7ad6ecf8af455924f604eb282.tar.bz2 rails-8b00da52586229f7ad6ecf8af455924f604eb282.zip |
Delegate through_reflection and source_reflection to reflection
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 34 insertions, 34 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index f1ca5ad2c6..acac68fda5 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -43,7 +43,7 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 426809f56f..d76d729303 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -12,7 +12,7 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index f8da0c5de7..e1d60ccb17 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord # 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 @@ module ActiveRecord # 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 @@ module ActiveRecord 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 |