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
221 def after_save
222   if assoc_pks = @_association_pks
223     assoc_pks.each do |name, pks|
224      # pks_setter_method is private
225       send(model.association_reflection(name)[:pks_setter_method], pks)
226     end
227     @_association_pks = nil
228   end
229   super
230 end
refresh() click to toggle source

Clear the associated pks if explicitly refreshing.

Calls superclass method
    # File lib/sequel/plugins/association_pks.rb
233 def refresh
234   @_association_pks = nil
235   super
236 end

Private Instance Methods

_association_pks_getter(opts, dynamic_opts=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
243 def _association_pks_getter(opts, dynamic_opts=OPTS)
244   do_cache = opts[:cache_pks]
245   delay = opts.fetch(:delay_pks, true)
246   cache_or_delay = do_cache || delay
247 
248   if dynamic_opts[:refresh] && @_association_pks
249     @_association_pks.delete(opts[:name])
250   end
251 
252   if new? && cache_or_delay
253     (@_association_pks ||= {})[opts[:name]] ||= []
254   elsif cache_or_delay && @_association_pks && (objs = @_association_pks[opts[:name]])
255     objs
256   elsif do_cache
257    # pks_getter_method is private
258     (@_association_pks ||= {})[opts[:name]] = send(opts[:pks_getter_method])
259   else
260    # pks_getter_method is private
261     send(opts[:pks_getter_method])
262   end
263 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
268 def _association_pks_setter(opts, pks)
269   if pks.nil?
270     case opts[:association_pks_nil]
271     when :remove
272       pks = []
273     when :ignore
274       return
275     else
276       raise Error, "nil value given to association_pks setter"
277     end
278   end
279 
280   pks = convert_pk_array(opts, pks)
281 
282   if opts.fetch(:delay_pks, true)
283     modified!
284     (@_association_pks ||= {})[opts[:name]] = pks
285   else
286     # pks_setter_method is private
287     send(opts[:pks_setter_method], pks)
288   end
289 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
293 def convert_pk_array(opts, pks)
294   klass = opts.associated_class
295   primary_key = klass.primary_key
296   sch = klass.db_schema
297 
298   if primary_key.is_a?(Array)
299     if (cols = sch.values_at(*klass.primary_key)).all? && (cols.map{|c| c[:type] == :integer}).all?
300       db = model.db
301       pks.map do |cpk|
302         cpk.map do |pk|
303           db.typecast_value(:integer, pk)
304         end
305       end
306     else
307       pks
308     end
309   elsif (col = sch[klass.primary_key]) && (col[:type] == :integer)
310     pks.map{|pk| model.db.typecast_value(:integer, pk)}
311   else
312     pks
313   end
314 end