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 219 def after_save 220 if assoc_pks = @_association_pks 221 assoc_pks.each do |name, pks| 222 # pks_setter_method is private 223 send(model.association_reflection(name)[:pks_setter_method], pks) 224 end 225 @_association_pks = nil 226 end 227 super 228 end
refresh()
click to toggle source
Clear the associated pks if explicitly refreshing.
Calls superclass method
# File lib/sequel/plugins/association_pks.rb 231 def refresh 232 @_association_pks = nil 233 super 234 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 241 def _association_pks_getter(opts, dynamic_opts=OPTS) 242 do_cache = opts[:cache_pks] 243 delay = opts.fetch(:delay_pks, true) 244 cache_or_delay = do_cache || delay 245 246 if dynamic_opts[:refresh] && @_association_pks 247 @_association_pks.delete(opts[:name]) 248 end 249 250 if new? && cache_or_delay 251 (@_association_pks ||= {})[opts[:name]] ||= [] 252 elsif cache_or_delay && @_association_pks && (objs = @_association_pks[opts[:name]]) 253 objs 254 elsif do_cache 255 # pks_getter_method is private 256 (@_association_pks ||= {})[opts[:name]] = send(opts[:pks_getter_method]) 257 else 258 # pks_getter_method is private 259 send(opts[:pks_getter_method]) 260 end 261 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 266 def _association_pks_setter(opts, pks) 267 if pks.nil? 268 case opts[:association_pks_nil] 269 when :remove 270 pks = [] 271 when :ignore 272 return 273 else 274 raise Error, "nil value given to association_pks setter" 275 end 276 end 277 278 pks = convert_pk_array(opts, pks) 279 280 if opts.fetch(:delay_pks, true) 281 modified! 282 (@_association_pks ||= {})[opts[:name]] = pks 283 else 284 # pks_setter_method is private 285 send(opts[:pks_setter_method], pks) 286 end 287 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 291 def convert_pk_array(opts, pks) 292 klass = opts.associated_class 293 primary_key = klass.primary_key 294 sch = klass.db_schema 295 296 if primary_key.is_a?(Array) 297 if (cols = sch.values_at(*klass.primary_key)).all? && (cols.map{|c| c[:type] == :integer}).all? 298 db = model.db 299 pks.map do |cpk| 300 cpk.map do |pk| 301 db.typecast_value(:integer, pk) 302 end 303 end 304 else 305 pks 306 end 307 elsif (col = sch[klass.primary_key]) && (col[:type] == :integer) 308 pks.map{|pk| model.db.typecast_value(:integer, pk)} 309 else 310 pks 311 end 312 end