Skip to content

Commit

Permalink
move complex logic to it's own method
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jan 15, 2011
1 parent a0a69b0 commit 2afd6c7
Showing 1 changed file with 19 additions and 15 deletions.
Expand Up @@ -353,21 +353,7 @@ def load_target
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
Expand Down Expand Up @@ -450,6 +436,24 @@ def add_record_to_target_with_callbacks(record)
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).
Expand Down

0 comments on commit 2afd6c7

Please sign in to comment.