8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/ar_merge.rb', line 8
def merge!(other,options={})
raise "cannot merge wit a new record" if other.new_record?
raise "cannot merge with myself" if other == self
(options[:associations]||[]).each do |association_name|
other.send(association_name).each do |object|
send(association_name).concat object
end
counter = "#{association_name}_count"
if other.respond_to?(counter)
other.send(counter).times{self.class.increment_counter(counter, id)}
end
end
(options[:attributes]||[]).each do |attr|
send("#{attr}=",other.send(attr)) if send(attr).blank?
end
other.reload.destroy
save!
end
|