module Sequel::Plugins::NestedAttributes::InstanceMethods

Public Instance Methods

set_nested_attributes(assoc, obj, opts=OPTS) click to toggle source

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
159 def set_nested_attributes(assoc, obj, opts=OPTS)
160   raise(Error, "no association named #{assoc} for #{model.inspect}") unless ref = model.association_reflection(assoc)
161   raise(Error, "nested attributes are not enabled for association #{assoc} for #{model.inspect}") unless meta = ref[:nested_attributes]
162   meta = meta.merge(opts)
163   meta[:reflection] = ref
164   if ref.returns_array?
165     nested_attributes_list_setter(meta, obj)
166   else
167     nested_attributes_setter(meta, obj)
168   end
169 end

Private Instance Methods

nested_attributes_check_key_modifications(meta, obj) { || ... } click to toggle source

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
175 def nested_attributes_check_key_modifications(meta, obj)
176   reflection = meta[:reflection]
177   keys = reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
178   yield
179   unless keys == reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
180     raise(Error, "Modifying association dependent key(s) when updating associated objects is not allowed")
181   end
182 end
nested_attributes_create(meta, attributes) click to toggle source

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
187 def nested_attributes_create(meta, attributes)
188   reflection = meta[:reflection]
189   obj = reflection.associated_class.new
190   nested_attributes_set_attributes(meta, obj, attributes)
191   delay_validate_associated_object(reflection, obj)
192   if reflection.returns_array?
193     public_send(reflection[:name]) << obj
194     obj.skip_validation_on_next_save!
195     after_save_hook{public_send(reflection[:add_method], obj)}
196   else
197     associations[reflection[:name]] = obj
198 
199     # Because we are modifying the associations cache manually before the
200     # setter is called, we still want to run the setter code even though
201     # the cached value will be the same as the given value.
202     @set_associated_object_if_same = true
203 
204     # Don't need to validate the object twice if :validate association option is not false
205     # and don't want to validate it at all if it is false.
206     if reflection[:type] == :many_to_one 
207       before_save_hook{public_send(reflection[:setter_method], obj.save(:validate=>false))}
208     else
209       after_save_hook do
210         obj.skip_validation_on_next_save!
211         public_send(reflection[:setter_method], obj)
212       end
213     end
214   end
215   add_reciprocal_object(reflection, obj)
216   obj
217 end
nested_attributes_list_setter(meta, attributes_list) click to toggle source

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
223 def nested_attributes_list_setter(meta, attributes_list)
224   attributes_list = attributes_list.sort.map{|k,v| v} if attributes_list.is_a?(Hash)
225   if (limit = meta[:limit]) && attributes_list.length > limit
226     raise(Error, "number of nested attributes (#{attributes_list.length}) exceeds the limit (#{limit})")
227   end
228   attributes_list.each{|a| nested_attributes_setter(meta, a)}
229 end
nested_attributes_remove(meta, obj, opts=OPTS) click to toggle source

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
235 def nested_attributes_remove(meta, obj, opts=OPTS)
236   reflection = meta[:reflection]
237   if !opts[:destroy] || reflection.remove_before_destroy?
238     before_save_hook do
239       if reflection.returns_array?
240         public_send(reflection[:remove_method], obj)
241       else
242         public_send(reflection[:setter_method], nil)
243       end
244     end
245   end
246   after_save_hook{obj.destroy} if opts[:destroy]
247   if reflection.returns_array?
248     associations[reflection[:name]].delete(obj)
249   end
250   obj
251 end
nested_attributes_set_attributes(meta, obj, attributes) click to toggle source

Set the fields in the obj based on the association, only allowing specific :fields if configured.

    # File lib/sequel/plugins/nested_attributes.rb
255 def nested_attributes_set_attributes(meta, obj, attributes)
256   if fields = meta[:fields]
257     fields = fields.call(obj) if fields.respond_to?(:call)
258     obj.set_fields(attributes, fields, :missing=>:skip)
259   else
260     obj.set(attributes)
261   end
262 end
nested_attributes_setter(meta, attributes) click to toggle source

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
274 def nested_attributes_setter(meta, attributes)
275   if a = meta[:transform]
276     attributes = a.call(self, attributes)
277   end
278   return if (b = meta[:reject_if]) && b.call(attributes)
279   modified!
280   reflection = meta[:reflection]
281   klass = reflection.associated_class
282   sym_keys = Array(klass.primary_key)
283   str_keys = sym_keys.map(&:to_s)
284   if (pk = attributes.values_at(*sym_keys)).all? || (pk = attributes.values_at(*str_keys)).all?
285     pk = pk.map(&:to_s)
286     obj = Array(public_send(reflection[:name])).find{|x| Array(x.pk).map(&:to_s) == pk}
287   end
288   if obj
289     unless (require_modification = meta[:require_modification]).nil?
290       obj.require_modification = require_modification
291     end
292     attributes = attributes.dup.delete_if{|k,v| str_keys.include? k.to_s}
293     if meta[:destroy] && klass.db.send(:typecast_value_boolean, attributes.delete(:_delete) || attributes.delete('_delete'))
294       nested_attributes_remove(meta, obj, :destroy=>true)
295     elsif meta[:remove] && klass.db.send(:typecast_value_boolean, attributes.delete(:_remove) || attributes.delete('_remove'))
296       nested_attributes_remove(meta, obj)
297     else
298       nested_attributes_update(meta, obj, attributes)
299     end
300   elsif pk.all? && meta[:unmatched_pk] != :create
301     if meta[:unmatched_pk] == :raise
302       raise(Error, "no matching associated object with given primary key (association: #{reflection[:name]}, pk: #{pk})")
303     end
304   else
305     nested_attributes_create(meta, attributes)
306   end
307 end
nested_attributes_update(meta, obj, attributes) click to toggle source

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
312 def nested_attributes_update(meta, obj, attributes)
313   nested_attributes_update_attributes(meta, obj, attributes)
314   delay_validate_associated_object(meta[:reflection], obj)
315   # Don't need to validate the object twice if :validate association option is not false
316   # and don't want to validate it at all if it is false.
317   after_save_hook{obj.save_changes(:validate=>false)}
318   obj
319 end
nested_attributes_update_attributes(meta, obj, attributes) click to toggle source

Update the attributes for the given object related to the current object through the association.

    # File lib/sequel/plugins/nested_attributes.rb
322 def nested_attributes_update_attributes(meta, obj, attributes)
323   nested_attributes_check_key_modifications(meta, obj) do
324     nested_attributes_set_attributes(meta, obj, attributes)
325   end
326 end