Permalink
Browse files

improve update logic

  • Loading branch information...
1 parent 66122f4 commit 3f11791e1da5122b2c8f9c4b3861d6d240caf451 @netskin-ci netskin-ci committed Sep 17, 2010
Showing with 11 additions and 4 deletions.
  1. +11 −4 lib/sequel_bulk_attributes.rb
View
15 lib/sequel_bulk_attributes.rb
@@ -9,15 +9,22 @@ def self.configure(model, opts={}, &block)
model.associations.each do |association|
association = model.association_reflection(association)
next unless [:one_to_many].include?(association[:type])
+
model.class_eval do
define_method("#{association[:name]}=") do |list|
- instance_variable_set("@_queued_#{association[:name]}", list)
+ cur = send(association[:name])
+ instance_variable_set("@_#{association[:name]}_add", list.reject{ |v| cur.detect{ |v1| v.pk == v1.pk } })
+ instance_variable_set("@_#{association[:name]}_remove", cur.reject{ |v| list.detect{ |v1| v.pk == v1.pk } })
+ cur.replace(list)
+
after_save_hook do
singular_name = association[:name].to_s.singularize
- send(association[:name]).each do |record|
- record.destroy
+
+ instance_variable_get("@_#{association[:name]}_remove").each do |record|
+ send("remove_#{singular_name}", record)
end
- instance_variable_get("@_queued_#{association[:name]}").each do |record|
+
+ instance_variable_get("@_#{association[:name]}_add").each do |record|
send("add_#{singular_name}", record)
end
end

0 comments on commit 3f11791

Please sign in to comment.