Define a association_pks method using the block for the association reflection
# File lib/sequel/plugins/association_pks.rb, line 41 41: def def_association_pks_getter(opts, &block) 42: association_module_def(:"#{singularize(opts[:name])}_pks", opts, &block) 43: end
Define a association_pks= method using the block for the association reflection, if the association is not read only.
# File lib/sequel/plugins/association_pks.rb, line 47 47: def def_association_pks_setter(opts, &block) 48: association_module_def(:"#{singularize(opts[:name])}_pks=", opts, &block) unless opts[:read_only] 49: end
Add a getter that checks the join table for matching records and a setter that deletes from or inserts into the join table.
# File lib/sequel/plugins/association_pks.rb, line 53 53: def def_many_to_many(opts) 54: super 55: def_association_pks_getter(opts) do 56: _join_table_dataset(opts).filter(opts[:left_key]=>send(opts[:left_primary_key])).select_map(opts[:right_key]) 57: end 58: def_association_pks_setter(opts) do |pks| 59: pks = convert_pk_array(opts, pks) 60: checked_transaction do 61: ds = _join_table_dataset(opts).filter(opts[:left_key]=>send(opts[:left_primary_key])) 62: ds.exclude(opts[:right_key]=>pks).delete 63: pks -= ds.select_map(opts[:right_key]) 64: pks.each{|pk| ds.insert(opts[:left_key]=>send(opts[:left_primary_key]), opts[:right_key]=>pk)} 65: end 66: end 67: end
Add a getter that checks the association dataset and a setter that updates the associated table.
# File lib/sequel/plugins/association_pks.rb, line 71 71: def def_one_to_many(opts) 72: super 73: return if opts[:type] == :one_to_one 74: def_association_pks_getter(opts) do 75: send(opts.dataset_method).select_map(opts.associated_class.primary_key) 76: end 77: def_association_pks_setter(opts) do |pks| 78: pks = convert_pk_array(opts, pks) 79: checked_transaction do 80: ds = send(opts.dataset_method) 81: primary_key = opts.associated_class.primary_key 82: key = opts[:key] 83: ds.unfiltered.filter(primary_key=>pks).update(key=>pk) 84: ds.exclude(primary_key=>pks).update(key=>nil) 85: end 86: end 87: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.