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 181 def after_save 182 if assoc_pks = @_association_pks 183 assoc_pks.each do |name, pks| 184 instance_exec(pks, &model.association_reflection(name)[:pks_setter]) 185 end 186 @_association_pks = nil 187 end 188 super 189 end
refresh()
click to toggle source
Clear the associated pks if explicitly refreshing.
Calls superclass method
# File lib/sequel/plugins/association_pks.rb 192 def refresh 193 @_association_pks = nil 194 super 195 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 202 def _association_pks_getter(opts) 203 delay = opts.fetch(:delay_pks, true) 204 if new? && delay 205 (@_association_pks ||= {})[opts[:name]] ||= [] 206 elsif delay && @_association_pks && (objs = @_association_pks[opts[:name]]) 207 objs 208 else 209 instance_exec(&opts[:pks_getter]) 210 end 211 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 216 def _association_pks_setter(opts, pks) 217 if pks.nil? 218 case opts[:association_pks_nil] 219 when :remove 220 pks = [] 221 when :ignore 222 return 223 else 224 raise Error, "nil value given to association_pks setter" 225 end 226 end 227 228 pks = convert_pk_array(opts, pks) 229 230 if opts.fetch(:delay_pks, true) 231 modified! 232 (@_association_pks ||= {})[opts[:name]] = pks 233 else 234 instance_exec(pks, &opts[:pks_setter]) 235 end 236 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 240 def convert_pk_array(opts, pks) 241 klass = opts.associated_class 242 primary_key = klass.primary_key 243 sch = klass.db_schema 244 245 if primary_key.is_a?(Array) 246 if (cols = sch.values_at(*klass.primary_key)).all? && (convs = cols.map{|c| c[:type] == :integer}).all? 247 pks.map do |cpk| 248 cpk.zip(convs).map do |pk, conv| 249 conv ? model.db.typecast_value(:integer, pk) : pk 250 end 251 end 252 else 253 pks 254 end 255 elsif (col = sch[klass.primary_key]) && (col[:type] == :integer) 256 pks.map{|pk| model.db.typecast_value(:integer, pk)} 257 else 258 pks 259 end 260 end