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
144 def set_nested_attributes(assoc, obj, opts=OPTS)
145   raise(Error, "no association named #{assoc} for #{model.inspect}") unless ref = model.association_reflection(assoc)
146   raise(Error, "nested attributes are not enabled for association #{assoc} for #{model.inspect}") unless meta = ref[:nested_attributes]
147   meta = Hash[meta].merge!(opts)
148   meta[:reflection] = ref
149   if ref.returns_array?
150     nested_attributes_list_setter(meta, obj)
151   else
152     nested_attributes_setter(meta, obj)
153   end
154 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
160 def nested_attributes_check_key_modifications(meta, obj)
161   reflection = meta[:reflection]
162   keys = reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
163   yield
164   unless keys == reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
165     raise(Error, "Modifying association dependent key(s) when updating associated objects is not allowed")
166   end
167 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
172 def nested_attributes_create(meta, attributes)
173   reflection = meta[:reflection]
174   obj = reflection.associated_class.new
175   nested_attributes_set_attributes(meta, obj, attributes)
176   delay_validate_associated_object(reflection, obj)
177   if reflection.returns_array?
178     public_send(reflection[:name]) << obj
179     after_save_hook{public_send(reflection[:add_method], obj)}
180   else
181     associations[reflection[:name]] = obj
182 
183     # Because we are modifying the associations cache manually before the
184     # setter is called, we still want to run the setter code even though
185     # the cached value will be the same as the given value.
186     @set_associated_object_if_same = true
187 
188     # Don't need to validate the object twice if :validate association option is not false
189     # and don't want to validate it at all if it is false.
190     if reflection[:type] == :many_to_one 
191       before_save_hook{public_send(reflection[:setter_method], obj.save(:validate=>false))}
192     else
193       after_save_hook{public_send(reflection[:setter_method], obj)}
194     end
195   end
196   add_reciprocal_object(reflection, obj)
197   obj
198 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
204 def nested_attributes_list_setter(meta, attributes_list)
205   attributes_list = attributes_list.sort.map{|k,v| v} if attributes_list.is_a?(Hash)
206   if (limit = meta[:limit]) && attributes_list.length > limit
207     raise(Error, "number of nested attributes (#{attributes_list.length}) exceeds the limit (#{limit})")
208   end
209   attributes_list.each{|a| nested_attributes_setter(meta, a)}
210 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
216 def nested_attributes_remove(meta, obj, opts=OPTS)
217   reflection = meta[:reflection]
218   if !opts[:destroy] || reflection.remove_before_destroy?
219     before_save_hook do
220       if reflection.returns_array?
221         public_send(reflection[:remove_method], obj)
222       else
223         public_send(reflection[:setter_method], nil)
224       end
225     end
226   end
227   after_save_hook{obj.destroy} if opts[:destroy]
228   if reflection.returns_array?
229     associations[reflection[:name]].delete(obj)
230   end
231   obj
232 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
236 def nested_attributes_set_attributes(meta, obj, attributes)
237   if fields = meta[:fields]
238     fields = fields.call(obj) if fields.respond_to?(:call)
239     obj.set_fields(attributes, fields, :missing=>:skip)
240   else
241     obj.set(attributes)
242   end
243 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
255 def nested_attributes_setter(meta, attributes)
256   if a = meta[:transform]
257     attributes = a.call(self, attributes)
258   end
259   return if (b = meta[:reject_if]) && b.call(attributes)
260   modified!
261   reflection = meta[:reflection]
262   klass = reflection.associated_class
263   sym_keys = Array(klass.primary_key)
264   str_keys = sym_keys.map(&:to_s)
265   if (pk = attributes.values_at(*sym_keys)).all? || (pk = attributes.values_at(*str_keys)).all?
266     pk = pk.map(&:to_s)
267     obj = Array(public_send(reflection[:name])).find{|x| Array(x.pk).map(&:to_s) == pk}
268   end
269   if obj
270     attributes = attributes.dup.delete_if{|k,v| str_keys.include? k.to_s}
271     if meta[:destroy] && klass.db.send(:typecast_value_boolean, attributes.delete(:_delete) || attributes.delete('_delete'))
272       nested_attributes_remove(meta, obj, :destroy=>true)
273     elsif meta[:remove] && klass.db.send(:typecast_value_boolean, attributes.delete(:_remove) || attributes.delete('_remove'))
274       nested_attributes_remove(meta, obj)
275     else
276       nested_attributes_update(meta, obj, attributes)
277     end
278   elsif pk.all? && meta[:unmatched_pk] != :create
279     if meta[:unmatched_pk] == :raise
280       raise(Error, "no matching associated object with given primary key (association: #{reflection[:name]}, pk: #{pk})")
281     end
282   else
283     nested_attributes_create(meta, attributes)
284   end
285 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
290 def nested_attributes_update(meta, obj, attributes)
291   nested_attributes_update_attributes(meta, obj, attributes)
292   delay_validate_associated_object(meta[:reflection], obj)
293   # Don't need to validate the object twice if :validate association option is not false
294   # and don't want to validate it at all if it is false.
295   after_save_hook{obj.save_changes(:validate=>false)}
296   obj
297 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
300 def nested_attributes_update_attributes(meta, obj, attributes)
301   nested_attributes_check_key_modifications(meta, obj) do
302     nested_attributes_set_attributes(meta, obj, attributes)
303   end
304 end