module Sequel::Plugins::NestedAttributes::InstanceMethods
Public Instance Methods
Set the nested attributes for the given association. obj should be an enumerable of multiple objects for plural associations. The opts hash can be used to override any of the default options set by the class-level nested_attributes call.
# File lib/sequel/plugins/nested_attributes.rb 155 def set_nested_attributes(assoc, obj, opts=OPTS) 156 raise(Error, "no association named #{assoc} for #{model.inspect}") unless ref = model.association_reflection(assoc) 157 raise(Error, "nested attributes are not enabled for association #{assoc} for #{model.inspect}") unless meta = ref[:nested_attributes] 158 meta = meta.merge(opts) 159 meta[:reflection] = ref 160 if ref.returns_array? 161 nested_attributes_list_setter(meta, obj) 162 else 163 nested_attributes_setter(meta, obj) 164 end 165 end
Private Instance Methods
Check that the keys related to the association are not modified inside the block. Does not use an ensure block, so callers should be careful.
# File lib/sequel/plugins/nested_attributes.rb 171 def nested_attributes_check_key_modifications(meta, obj) 172 reflection = meta[:reflection] 173 keys = reflection.associated_object_keys.map{|x| obj.get_column_value(x)} 174 yield 175 unless keys == reflection.associated_object_keys.map{|x| obj.get_column_value(x)} 176 raise(Error, "Modifying association dependent key(s) when updating associated objects is not allowed") 177 end 178 end
Create a new associated object with the given attributes, validate it when the parent is validated, and save it when the object is saved. Returns the object created.
# File lib/sequel/plugins/nested_attributes.rb 183 def nested_attributes_create(meta, attributes) 184 reflection = meta[:reflection] 185 obj = reflection.associated_class.new 186 nested_attributes_set_attributes(meta, obj, attributes) 187 delay_validate_associated_object(reflection, obj) 188 if reflection.returns_array? 189 public_send(reflection[:name]) << obj 190 obj.skip_validation_on_next_save! 191 after_save_hook{public_send(reflection[:add_method], obj)} 192 else 193 associations[reflection[:name]] = obj 194 195 # Because we are modifying the associations cache manually before the 196 # setter is called, we still want to run the setter code even though 197 # the cached value will be the same as the given value. 198 @set_associated_object_if_same = true 199 200 # Don't need to validate the object twice if :validate association option is not false 201 # and don't want to validate it at all if it is false. 202 if reflection[:type] == :many_to_one 203 before_save_hook{public_send(reflection[:setter_method], obj.save(:validate=>false))} 204 else 205 after_save_hook do 206 obj.skip_validation_on_next_save! 207 public_send(reflection[:setter_method], obj) 208 end 209 end 210 end 211 add_reciprocal_object(reflection, obj) 212 obj 213 end
Take an array or hash of attribute hashes and set each one individually. If a hash is provided it, sort it by key and then use the values. If there is a limit on the nested attributes for this association, make sure the length of the attributes_list is not greater than the limit.
# File lib/sequel/plugins/nested_attributes.rb 219 def nested_attributes_list_setter(meta, attributes_list) 220 attributes_list = attributes_list.sort.map{|k,v| v} if attributes_list.is_a?(Hash) 221 if (limit = meta[:limit]) && attributes_list.length > limit 222 raise(Error, "number of nested attributes (#{attributes_list.length}) exceeds the limit (#{limit})") 223 end 224 attributes_list.each{|a| nested_attributes_setter(meta, a)} 225 end
Remove the given associated object from the current object. If the :destroy option is given, destroy the object after disassociating it (unless destroying the object would automatically disassociate it). Returns the object removed.
# File lib/sequel/plugins/nested_attributes.rb 231 def nested_attributes_remove(meta, obj, opts=OPTS) 232 reflection = meta[:reflection] 233 if !opts[:destroy] || reflection.remove_before_destroy? 234 before_save_hook do 235 if reflection.returns_array? 236 public_send(reflection[:remove_method], obj) 237 else 238 public_send(reflection[:setter_method], nil) 239 end 240 end 241 end 242 after_save_hook{obj.destroy} if opts[:destroy] 243 if reflection.returns_array? 244 associations[reflection[:name]].delete(obj) 245 end 246 obj 247 end
Set the fields in the obj based on the association, only allowing specific :fields if configured.
# File lib/sequel/plugins/nested_attributes.rb 251 def nested_attributes_set_attributes(meta, obj, attributes) 252 if fields = meta[:fields] 253 fields = fields.call(obj) if fields.respond_to?(:call) 254 obj.set_fields(attributes, fields, :missing=>:skip) 255 else 256 obj.set(attributes) 257 end 258 end
Modify the associated object based on the contents of the attributes hash:
-
If a :transform block was given to nested_attributes, use it to modify the attribute hash.
-
If a block was given to nested_attributes, call it with the attributes and return immediately if the block returns true.
-
If a primary key exists in the attributes hash and it matches an associated object:
** If _delete is a key in the hash and the :destroy option is used, destroy the matching associated object. ** If _remove is a key in the hash and the :remove option is used, disassociated the matching associated object. ** Otherwise, update the matching associated object with the contents of the hash.
-
If a primary key exists in the attributes hash but it does not match an associated object, either raise an error, create a new object or ignore the hash, depending on the :unmatched_pk option.
-
If no primary key exists in the attributes hash, create a new object.
# File lib/sequel/plugins/nested_attributes.rb 270 def nested_attributes_setter(meta, attributes) 271 if a = meta[:transform] 272 attributes = a.call(self, attributes) 273 end 274 return if (b = meta[:reject_if]) && b.call(attributes) 275 modified! 276 reflection = meta[:reflection] 277 klass = reflection.associated_class 278 sym_keys = Array(klass.primary_key) 279 str_keys = sym_keys.map(&:to_s) 280 if (pk = attributes.values_at(*sym_keys)).all? || (pk = attributes.values_at(*str_keys)).all? 281 pk = pk.map(&:to_s) 282 obj = Array(public_send(reflection[:name])).find{|x| Array(x.pk).map(&:to_s) == pk} 283 end 284 if obj 285 attributes = attributes.dup.delete_if{|k,v| str_keys.include? k.to_s} 286 if meta[:destroy] && klass.db.send(:typecast_value_boolean, attributes.delete(:_delete) || attributes.delete('_delete')) 287 nested_attributes_remove(meta, obj, :destroy=>true) 288 elsif meta[:remove] && klass.db.send(:typecast_value_boolean, attributes.delete(:_remove) || attributes.delete('_remove')) 289 nested_attributes_remove(meta, obj) 290 else 291 nested_attributes_update(meta, obj, attributes) 292 end 293 elsif pk.all? && meta[:unmatched_pk] != :create 294 if meta[:unmatched_pk] == :raise 295 raise(Error, "no matching associated object with given primary key (association: #{reflection[:name]}, pk: #{pk})") 296 end 297 else 298 nested_attributes_create(meta, attributes) 299 end 300 end
Update the given object with the attributes, validating it when the parent object is validated and saving it when the parent is saved. Returns the object updated.
# File lib/sequel/plugins/nested_attributes.rb 305 def nested_attributes_update(meta, obj, attributes) 306 nested_attributes_update_attributes(meta, obj, attributes) 307 delay_validate_associated_object(meta[:reflection], obj) 308 # Don't need to validate the object twice if :validate association option is not false 309 # and don't want to validate it at all if it is false. 310 after_save_hook{obj.save_changes(:validate=>false)} 311 obj 312 end
Update the attributes for the given object related to the current object through the association.
# File lib/sequel/plugins/nested_attributes.rb 315 def nested_attributes_update_attributes(meta, obj, attributes) 316 nested_attributes_check_key_modifications(meta, obj) do 317 nested_attributes_set_attributes(meta, obj, attributes) 318 end 319 end