Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

improve update logic

  • Loading branch information...
commit 3f11791e1da5122b2c8f9c4b3861d6d240caf451 1 parent 66122f4
netskin-ci netskin-ci authored

Showing 1 changed file with 11 additions and 4 deletions. Show diff stats Hide diff stats

  1. +11 4 lib/sequel_bulk_attributes.rb
15 lib/sequel_bulk_attributes.rb
@@ -9,15 +9,22 @@ def self.configure(model, opts={}, &block)
9 9 model.associations.each do |association|
10 10 association = model.association_reflection(association)
11 11 next unless [:one_to_many].include?(association[:type])
  12 +
12 13 model.class_eval do
13 14 define_method("#{association[:name]}=") do |list|
14   - instance_variable_set("@_queued_#{association[:name]}", list)
  15 + cur = send(association[:name])
  16 + instance_variable_set("@_#{association[:name]}_add", list.reject{ |v| cur.detect{ |v1| v.pk == v1.pk } })
  17 + instance_variable_set("@_#{association[:name]}_remove", cur.reject{ |v| list.detect{ |v1| v.pk == v1.pk } })
  18 + cur.replace(list)
  19 +
15 20 after_save_hook do
16 21 singular_name = association[:name].to_s.singularize
17   - send(association[:name]).each do |record|
18   - record.destroy
  22 +
  23 + instance_variable_get("@_#{association[:name]}_remove").each do |record|
  24 + send("remove_#{singular_name}", record)
19 25 end
20   - instance_variable_get("@_queued_#{association[:name]}").each do |record|
  26 +
  27 + instance_variable_get("@_#{association[:name]}_add").each do |record|
21 28 send("add_#{singular_name}", record)
22 29 end
23 30 end

0 comments on commit 3f11791

Please sign in to comment.
Something went wrong with that request. Please try again.