Module: Sequel::Plugins::AssociationMultiAddRemove::ClassMethods
- Defined in:
- lib/sequel/plugins/association_multi_add_remove.rb
Instance Method Summary collapse
-
#def_association_instance_methods(opts) ⇒ Object
Define the methods use to add/remove/set multiple associated objects in a single method call.
Instance Method Details
#def_association_instance_methods(opts) ⇒ Object
Define the methods use to add/remove/set multiple associated objects in a single method call.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sequel/plugins/association_multi_add_remove.rb', line 44 def def_association_instance_methods(opts) super if opts[:adder] add_method = opts[:add_method] multi_add_method = opts[:multi_add_method] || :"add_#{opts[:name]}" multi_add_method = nil if add_method == multi_add_method if multi_add_method association_module_def(multi_add_method, opts) do |objs, *args| db.transaction(:server=>@server){objs.map{|obj| send(add_method, obj, *args)}.compact} end end end if opts[:remover] remove_method = opts[:remove_method] multi_remove_method = opts[:multi_remove_method] || :"remove_#{opts[:name]}" multi_remove_method = nil if remove_method == multi_remove_method if multi_remove_method association_module_def(multi_remove_method, opts) do |objs, *args| db.transaction(:server=>@server){objs.map{|obj| send(remove_method, obj, *args)}.compact} end end end if multi_add_method && multi_remove_method association_module_def(:"#{opts[:name]}=", opts) do |objs, *args| db.transaction(:server=>@server) do existing_objs = send(opts.association_method) send(multi_remove_method, (existing_objs - objs), *args) send(multi_add_method, (objs - existing_objs), *args) nil end end end end |