aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association_collection.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-01-14 16:46:02 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-14 16:46:02 -0800
commit2afd6c75f18aee67fab85efef2b970572d459db3 (patch)
treedafec75ea39fa10b5c3f9047425e1bd0696cfb3c /activerecord/lib/active_record/associations/association_collection.rb
parenta0a69b045adeced5754251706a0401b75e7e03ec (diff)
downloadrails-2afd6c75f18aee67fab85efef2b970572d459db3.tar.gz
rails-2afd6c75f18aee67fab85efef2b970572d459db3.tar.bz2
rails-2afd6c75f18aee67fab85efef2b970572d459db3.zip
move complex logic to it's own method
Diffstat (limited to 'activerecord/lib/active_record/associations/association_collection.rb')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb34
1 files changed, 19 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index bf40446871..60a8d71d26 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -353,21 +353,7 @@ module ActiveRecord
unless loaded?
begin
if @target.is_a?(Array) && @target.any?
- @target = find_target.map do |f|
- i = @target.index(f)
- if i
- @target.delete_at(i).tap do |t|
- keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names)
- # FIXME: this call to attributes causes many NoMethodErrors
- attributes = f.attributes
- (attributes.keys - keys).each do |k|
- t.send("#{k}=", attributes[k])
- end
- end
- else
- f
- end
- end + @target
+ @target = merge_target_lists(find_target, @target)
else
@target = find_target
end
@@ -450,6 +436,24 @@ module ActiveRecord
end
private
+ def merge_target_lists(loaded, existing)
+ loaded.map do |f|
+ i = existing.index(f)
+ if i
+ existing.delete_at(i).tap do |t|
+ keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names)
+ # FIXME: this call to attributes causes many NoMethodErrors
+ attributes = f.attributes
+ (attributes.keys - keys).each do |k|
+ t.send("#{k}=", attributes[k])
+ end
+ end
+ else
+ f
+ end
+ end + existing
+ end
+
# Do the relevant stuff to insert the given record into the association collection. The
# force param specifies whether or not an exception should be raised on failure. The
# validate param specifies whether validation should be performed (if force is false).