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, line 137
def set_nested_attributes(assoc, obj, opts=OPTS)
  raise(Error, "no association named #{assoc} for #{model.inspect}") unless ref = model.association_reflection(assoc)
  raise(Error, "nested attributes are not enabled for association #{assoc} for #{model.inspect}") unless meta = ref[:nested_attributes]
  meta = meta.merge(opts)
  meta[:reflection] = ref
  if ref.returns_array?
    nested_attributes_list_setter(meta, obj)
  else
    nested_attributes_setter(meta, obj)
  end
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, line 153
def nested_attributes_check_key_modifications(meta, obj)
  reflection = meta[:reflection]
  keys = reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
  yield
  unless keys == reflection.associated_object_keys.map{|x| obj.get_column_value(x)}
    raise(Error, "Modifying association dependent key(s) when updating associated objects is not allowed")
  end
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, line 165
def nested_attributes_create(meta, attributes)
  reflection = meta[:reflection]
  obj = reflection.associated_class.new
  nested_attributes_set_attributes(meta, obj, attributes)
  after_validation_hook{validate_associated_object(meta, obj)}
  if reflection.returns_array?
    send(reflection[:name]) << obj
    after_save_hook{send(reflection.add_method, obj)}
  else
    associations[reflection[:name]] = obj

    # Because we are modifying the associations cache manually before the
    # setter is called, we still want to run the setter code even though
    # the cached value will be the same as the given value.
    @set_associated_object_if_same = true

    # Don't need to validate the object twice if :validate association option is not false
    # and don't want to validate it at all if it is false.
    if reflection[:type] == :many_to_one 
      before_save_hook{send(reflection.setter_method, obj.save(:validate=>false))}
    else
      after_save_hook{send(reflection.setter_method, obj)}
    end
  end
  add_reciprocal_object(reflection, obj)
  obj
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, line 197
def nested_attributes_list_setter(meta, attributes_list)
  attributes_list = attributes_list.sort_by{|x| x.to_s}.map{|k,v| v} if attributes_list.is_a?(Hash)
  if (limit = meta[:limit]) && attributes_list.length > limit
    raise(Error, "number of nested attributes (#{attributes_list.length}) exceeds the limit (#{limit})")
  end
  attributes_list.each{|a| nested_attributes_setter(meta, a)}
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, line 209
def nested_attributes_remove(meta, obj, opts=OPTS)
  reflection = meta[:reflection]
  if !opts[:destroy] || reflection.remove_before_destroy?
    before_save_hook do
      if reflection.returns_array?
        send(reflection.remove_method, obj)
      else
        send(reflection.setter_method, nil)
      end
    end
  end
  after_save_hook{obj.destroy} if opts[:destroy]
  if reflection.returns_array?
    associations[reflection[:name]].delete(obj)
  end
  obj
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, line 229
def nested_attributes_set_attributes(meta, obj, attributes)
  if fields = meta[:fields]
    fields = fields.call(obj) if fields.respond_to?(:call)
    obj.set_only(attributes, fields)
  else
    obj.set(attributes)
  end
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, line 248
def nested_attributes_setter(meta, attributes)
  if a = meta[:transform]
    attributes = a.call(self, attributes)
  end
  return if (b = meta[:reject_if]) && b.call(attributes)
  modified!
  reflection = meta[:reflection]
  klass = reflection.associated_class
  sym_keys = Array(klass.primary_key)
  str_keys = sym_keys.map{|k| k.to_s}
  if (pk = attributes.values_at(*sym_keys)).all? || (pk = attributes.values_at(*str_keys)).all?
    pk = pk.map{|k| k.to_s}
    obj = Array(send(reflection[:name])).find{|x| Array(x.pk).map{|k| k.to_s} == pk}
  end
  if obj
    attributes = attributes.dup.delete_if{|k,v| str_keys.include? k.to_s}
    if meta[:destroy] && klass.db.send(:typecast_value_boolean, attributes.delete(:_delete) || attributes.delete('_delete'))
      nested_attributes_remove(meta, obj, :destroy=>true)
    elsif meta[:remove] && klass.db.send(:typecast_value_boolean, attributes.delete(:_remove) || attributes.delete('_remove'))
      nested_attributes_remove(meta, obj)
    else
      nested_attributes_update(meta, obj, attributes)
    end
  elsif pk.all? && meta[:unmatched_pk] != :create
    if meta[:unmatched_pk] == :raise
      raise(Error, "no matching associated object with given primary key (association: #{reflection[:name]}, pk: #{pk})")
    end
  else
    nested_attributes_create(meta, attributes)
  end
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, line 283
def nested_attributes_update(meta, obj, attributes)
  nested_attributes_update_attributes(meta, obj, attributes)
  after_validation_hook{validate_associated_object(meta, obj)}
  # Don't need to validate the object twice if :validate association option is not false
  # and don't want to validate it at all if it is false.
  after_save_hook{obj.save_changes(:validate=>false)}
  obj
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, line 293
def nested_attributes_update_attributes(meta, obj, attributes)
  nested_attributes_check_key_modifications(meta, obj) do
    nested_attributes_set_attributes(meta, obj, attributes)
  end
end
validate_associated_object(meta, obj) click to toggle source

Validate the given associated object, adding any validation error messages from the given object to the parent object.

# File lib/sequel/plugins/nested_attributes.rb, line 301
def validate_associated_object(meta, obj)
  reflection = meta[:reflection]
  return if reflection[:validate] == false
  association = reflection[:name]
  if (reflection[:type] == :one_to_many || reflection[:type] == :one_to_one) && (key = reflection[:key]).is_a?(Symbol) && !(pk_val = obj.values[key])
    # There could be a presence validation on the foreign key in the associated model,
    # which will fail if we validate before saving the current object.  If there is
    # no value for the foreign key, set it to the current primary key value, or a dummy
    # value of 0 if we haven't saved the current object.
    p_key = pk unless pk.is_a?(Array)
    obj.values[key] = p_key || 0
    key = nil if p_key
  end
  obj.errors.full_messages.each{|m| errors.add(association, m)} unless obj.valid?
  if key && !pk_val
    # If we used a dummy value of 0, remove it so it doesn't accidently remain.
    obj.values.delete(key)
  end
end