module Sequel::Plugins::AssociationPks::InstanceMethods

Public Instance Methods

after_save() click to toggle source

After creating an object, if there are any saved association pks, call the related association pks setters.

Calls superclass method
    # File lib/sequel/plugins/association_pks.rb
188 def after_save
189   if assoc_pks = @_association_pks
190     assoc_pks.each do |name, pks|
191      # pks_setter_method is private
192       send(model.association_reflection(name)[:pks_setter_method], pks)
193     end
194     @_association_pks = nil
195   end
196   super
197 end
refresh() click to toggle source

Clear the associated pks if explicitly refreshing.

Calls superclass method
    # File lib/sequel/plugins/association_pks.rb
200 def refresh
201   @_association_pks = nil
202   super
203 end

Private Instance Methods

_association_pks_getter(opts) click to toggle source

Return the primary keys of the associated objects. If the receiver is a new object, return any saved pks, or an empty array if no pks have been saved.

    # File lib/sequel/plugins/association_pks.rb
210 def _association_pks_getter(opts)
211   delay = opts.fetch(:delay_pks, true)
212   if new? && delay
213     (@_association_pks ||= {})[opts[:name]] ||= []
214   elsif delay && @_association_pks && (objs = @_association_pks[opts[:name]])
215     objs
216   else
217    # pks_getter_method is private
218     send(opts[:pks_getter_method])
219   end
220 end
_association_pks_setter(opts, pks) click to toggle source

Update which objects are associated to the receiver. If the receiver is a new object, save the pks so the update can happen after the receiver has been saved.

    # File lib/sequel/plugins/association_pks.rb
225 def _association_pks_setter(opts, pks)
226   if pks.nil?
227     case opts[:association_pks_nil]
228     when :remove
229       pks = []
230     when :ignore
231       return
232     else
233       raise Error, "nil value given to association_pks setter"
234     end
235   end
236 
237   pks = convert_pk_array(opts, pks)
238 
239   if opts.fetch(:delay_pks, true)
240     modified!
241     (@_association_pks ||= {})[opts[:name]] = pks
242   else
243     # pks_setter_method is private
244     send(opts[:pks_setter_method], pks)
245   end
246 end
convert_pk_array(opts, pks) click to toggle source

If the associated class's primary key column type is integer, typecast all provided values to integer before using them.

    # File lib/sequel/plugins/association_pks.rb
250 def convert_pk_array(opts, pks)
251   klass = opts.associated_class
252   primary_key = klass.primary_key
253   sch = klass.db_schema
254 
255   if primary_key.is_a?(Array)
256     if (cols = sch.values_at(*klass.primary_key)).all? && (convs = cols.map{|c| c[:type] == :integer}).all?
257       pks.map do |cpk|
258         cpk.zip(convs).map do |pk, conv|
259           conv ? model.db.typecast_value(:integer, pk) : pk
260         end
261       end
262     else
263       pks
264     end
265   elsif (col = sch[klass.primary_key]) && (col[:type] == :integer)
266     pks.map{|pk| model.db.typecast_value(:integer, pk)}
267   else
268     pks
269   end
270 end