5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/form_obj/array.rb', line 5
def update_attributes(vals)
ids_exists = []
items_to_add = []
vals.each do |val|
id = primary_key(val)
item = self.find { |i| i.primary_key == id }
if item
item.update_attributes(val)
ids_exists << id
else
items_to_add << val
end
end
ids_to_remove = self.map(&:primary_key) - ids_exists
self.delete_if { |item| ids_to_remove.include? item.primary_key }
items_to_add.each do |item|
self.create.update_attributes(item)
end
sort! { |a, b| vals.index { |val| primary_key(val) == a.primary_key } <=> vals.index { |val| primary_key(val) == b.primary_key } }
end
|