Skip to content
Browse files

Get rid of set_association_target and association_loaded? as the part…

…s of the code that need that can now just use association_proxy(:name).loaded?/target=
  • Loading branch information...
1 parent 42b2e4f commit 681ab53ba15f9fc95c8a91e50bb0138aa66967b2 @jonleighton jonleighton committed with tenderlove Jan 9, 2011
View
19 activerecord/lib/active_record/association_preload.rb
@@ -133,7 +133,7 @@ def add_preloaded_records_to_collection(parent_records, reflection_name, associa
def add_preloaded_record_to_collection(parent_records, reflection_name, associated_record)
parent_records.each do |parent_record|
- parent_record.send("set_#{reflection_name}_target", associated_record)
+ parent_record.send(:association_proxy, reflection_name).target = associated_record
end
end
@@ -158,14 +158,17 @@ def set_association_single_records(id_to_record_map, reflection_name, associated
seen_keys[seen_key] = true
mapped_records = id_to_record_map[seen_key]
mapped_records.each do |mapped_record|
- association_proxy = mapped_record.send("set_#{reflection_name}_target", associated_record)
+ association_proxy = mapped_record.send(:association_proxy, reflection_name)
+ association_proxy.target = associated_record
association_proxy.send(:set_inverse_instance, associated_record)
end
end
id_to_record_map.each do |id, records|
next if seen_keys.include?(id.to_s)
- records.each {|record| record.send("set_#{reflection_name}_target", nil) }
+ records.each do |record|
+ record.send(:association_proxy, reflection_name).target = nil
+ end
end
end
@@ -232,10 +235,14 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
end
def preload_has_one_association(records, reflection, preload_options={})
- return if records.first.send("loaded_#{reflection.name}?")
+ return if records.first.send(:association_proxy, reflection.name).loaded?
id_to_record_map, ids = construct_id_map(records, reflection.options[:primary_key])
options = reflection.options
- records.each {|record| record.send("set_#{reflection.name}_target", nil)}
+
+ records.each do |record|
+ record.send(:association_proxy, reflection.name).target = nil
+ end
+
if options[:through]
through_records = preload_through_records(records, reflection, options[:through])
@@ -317,7 +324,7 @@ def preload_through_records(records, reflection, through_association)
end
def preload_belongs_to_association(records, reflection, preload_options={})
- return if records.first.send("loaded_#{reflection.name}?")
+ return if records.first.send(:association_proxy, reflection.name).loaded?
options = reflection.options
klasses_and_ids = {}
View
12 activerecord/lib/active_record/associations.rb
@@ -1495,21 +1495,9 @@ def association_accessor_methods(reflection, association_proxy_class)
association.target.nil? ? nil : association
end
- redefine_method("loaded_#{reflection.name}?") do
- association = association_proxy(reflection.name)
- association && association.loaded?
- end
-
redefine_method("#{reflection.name}=") do |record|
association_proxy(reflection.name).replace(record)
end
-
- redefine_method("set_#{reflection.name}_target") do |target|
- association = association_proxy(reflection.name)
- association.target = target
- association.loaded
- association
- end
end
def collection_reader_method(reflection, association_proxy_class)
View
3 activerecord/lib/active_record/associations/association_proxy.rb
@@ -213,7 +213,8 @@ def aliased_table
# Set the inverse association, if possible
def set_inverse_instance(record)
if record && invertible_for?(record)
- record.send("set_#{inverse_reflection_for(record).name}_target", @owner)
+ inverse = record.send(:association_proxy, inverse_reflection_for(record).name)
+ inverse.target = @owner
end
end
View
3 activerecord/lib/active_record/associations/class_methods/join_dependency.rb
@@ -223,7 +223,8 @@ def construct_association(record, join_part, row)
end
def set_target_and_inverse(join_part, association, record)
- association_proxy = record.send("set_#{join_part.reflection.name}_target", association)
+ association_proxy = record.send(:association_proxy, join_part.reflection.name)
+ association_proxy.target = association
association_proxy.send(:set_inverse_instance, association)
end
end
View
2 activerecord/test/cases/associations/has_one_through_associations_test.rb
@@ -197,7 +197,7 @@ def test_preloading_has_one_through_on_belongs_to
MemberDetail.find(:all, :include => :member_type)
end
@new_detail = @member_details[0]
- assert @new_detail.loaded_member_type?
+ assert @new_detail.send(:association_proxy, :member_type).loaded?
assert_not_nil assert_no_queries { @new_detail.member_type }
end

0 comments on commit 681ab53

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