Module: Sequel::Plugins::BulkAttributes
- Defined in:
- lib/sequel_bulk_attributes.rb
Defined Under Namespace
Modules: ClassMethods, DatasetMethods, InstanceMethods
Class Method Summary
collapse
Class Method Details
.apply(model, opts = {}, &block) ⇒ Object
4
5
6
|
# File 'lib/sequel_bulk_attributes.rb', line 4
def self.apply(model, opts={}, &block)
model.plugin(:instance_hooks)
end
|
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
34
|
# File 'lib/sequel_bulk_attributes.rb', line 8
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|
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
instance_variable_get("@_#{association[:name]}_remove").each do |record|
send("remove_#{singular_name}", record)
end
instance_variable_get("@_#{association[:name]}_add").each do |record|
send("add_#{singular_name}", record)
end
end
end
end
end
end
|