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
161 def set_nested_attributes(assoc, obj, opts=OPTS)
162   raise(Error, "no association named #{assoc} for #{model.inspect}") unless ref = model.association_reflection(assoc)
163   raise(Error, "nested attributes are not enabled for association #{assoc} for #{model.inspect}") unless meta = ref[:nested_attributes]
164   meta = meta.merge(opts)
165   meta[:reflection] = ref
166   if ref.returns_array?
167     nested_attributes_list_setter(meta, obj)
168   else
169     nested_attributes_setter(meta, obj)
170   end
171 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
177 def nested_attributes_check_key_modifications(meta, obj)
178   reflection = meta[:reflection]
179   keys = reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
180   yield
181   unless keys == reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
182     raise(Error, "Modifying association dependent key(s) when updating associated objects is not allowed")
183   end
184 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
189 def nested_attributes_create(meta, attributes)
190   reflection = meta[:reflection]
191   obj = reflection.associated_class.new
192   nested_attributes_set_attributes(meta, obj, attributes)
193   delay_validate_associated_object(reflection, obj)
194   if reflection.returns_array?
195     public_send(reflection[:name]) << obj
196     obj.skip_validation_on_next_save!
197     after_save_hook{public_send(reflection[:add_method], obj)}
198   else
199     associations[reflection[:name]] = obj
200 
201     # Because we are modifying the associations cache manually before the
202     # setter is called, we still want to run the setter code even though
203     # the cached value will be the same as the given value.
204     @set_associated_object_if_same = true
205 
206     # Don't need to validate the object twice if :validate association option is not false
207     # and don't want to validate it at all if it is false.
208     if reflection[:type] == :many_to_one 
209       before_save_hook{public_send(reflection[:setter_method], obj.save(:validate=>false))}
210     else
211       after_save_hook do
212         obj.skip_validation_on_next_save!
213         public_send(reflection[:setter_method], obj)
214       end
215     end
216   end
217   add_reciprocal_object(reflection, obj)
218   obj
219 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
225 def nested_attributes_list_setter(meta, attributes_list)
226   attributes_list = attributes_list.sort.map{|k,v| v} if attributes_list.is_a?(Hash)
227   if (limit = meta[:limit]) && attributes_list.length > limit
228     raise(Error, "number of nested attributes (#{attributes_list.length}) exceeds the limit (#{limit})")
229   end
230   attributes_list.each{|a| nested_attributes_setter(meta, a)}
231 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
237 def nested_attributes_remove(meta, obj, opts=OPTS)
238   reflection = meta[:reflection]
239   if !opts[:destroy] || reflection.remove_before_destroy?
240     before_save_hook do
241       if reflection.returns_array?
242         public_send(reflection[:remove_method], obj)
243       else
244         public_send(reflection[:setter_method], nil)
245       end
246     end
247   end
248   after_save_hook{obj.destroy} if opts[:destroy]
249   if reflection.returns_array?
250     associations[reflection[:name]].delete(obj)
251   end
252   obj
253 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
257 def nested_attributes_set_attributes(meta, obj, attributes)
258   if fields = meta[:fields]
259     fields = fields.call(obj) if fields.respond_to?(:call)
260     obj.set_fields(attributes, fields, :missing=>:skip)
261   else
262     obj.set(attributes)
263   end
264 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
276 def nested_attributes_setter(meta, attributes)
277   if a = meta[:transform]
278     attributes = a.call(self, attributes)
279   end
280   return if (b = meta[:reject_if]) && b.call(attributes)
281   modified!
282   reflection = meta[:reflection]
283   klass = reflection.associated_class
284   sym_keys = Array(klass.primary_key)
285   str_keys = sym_keys.map(&:to_s)
286   if (pk = attributes.values_at(*sym_keys)).all? || (pk = attributes.values_at(*str_keys)).all?
287     pk = pk.map(&:to_s)
288     obj = Array(public_send(reflection[:name])).find{|x| Array(x.pk).map(&:to_s) == pk}
289   end
290   if obj
291     unless (require_modification = meta[:require_modification]).nil?
292       obj.require_modification = require_modification
293     end
294     attributes = attributes.dup.delete_if{|k,v| str_keys.include? k.to_s}
295     if meta[:destroy] && klass.db.send(:typecast_value_boolean, attributes.delete(:_delete) || attributes.delete('_delete'))
296       nested_attributes_remove(meta, obj, :destroy=>true)
297     elsif meta[:remove] && klass.db.send(:typecast_value_boolean, attributes.delete(:_remove) || attributes.delete('_remove'))
298       nested_attributes_remove(meta, obj)
299     else
300       nested_attributes_update(meta, obj, attributes)
301     end
302   elsif pk.all? && meta[:unmatched_pk] != :create
303     if meta[:unmatched_pk] == :raise
304       raise(Error, "no matching associated object with given primary key (association: #{reflection[:name]}, pk: #{pk})")
305     end
306   else
307     nested_attributes_create(meta, attributes)
308   end
309 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
314 def nested_attributes_update(meta, obj, attributes)
315   nested_attributes_update_attributes(meta, obj, attributes)
316   delay_validate_associated_object(meta[:reflection], obj)
317   # Don't need to validate the object twice if :validate association option is not false
318   # and don't want to validate it at all if it is false.
319   after_save_hook{obj.save_changes(:validate=>false)}
320   obj
321 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
324 def nested_attributes_update_attributes(meta, obj, attributes)
325   nested_attributes_check_key_modifications(meta, obj) do
326     nested_attributes_set_attributes(meta, obj, attributes)
327   end
328 end