Module: DataMapper::NestedAttributes::Resource

Defined in:
lib/dm-accepts_nested_attributes/resource.rb

Overview

Extensions and customizations for a Resource that are needed if the Resource wants to accept nested attributes for any given relationship. Basically, this module provides functionality that allows either assignment or marking for destruction of related parent and child associations, based on the given attributes and what kind of relationship should be altered.

Constant Summary collapse

TRUE_VALUES =

Truthy values for the :_delete flag.

[true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set

Instance Method Summary collapse

Instance Method Details

#sanitize_nested_attributes(attributes) ⇒ Hash

Can be used to remove ambiguities from the passed attributes. Consider a situation with a belongs_to association where both a valid value for the foreign_key attribute and nested_attributes for a new record are present (i.e. item_type_id and item_type_attributes are present). Also see is.gd/sz2d on the rails-core ml for a discussion on this. The basic idea is, that there should be a well defined behavior for what exactly happens when such a situation occurs. I’m currently in favor for using the foreign_key if it is present, but this probably needs more thinking. For now, this method basically is a no-op, but at least it provides a hook where everyone can perform it’s own sanitization by overwriting this method.

Parameters:

  • attributes (Hash)

    The attributes to sanitize.

Returns:

  • (Hash)

    The sanitized attributes.



34
35
36
# File 'lib/dm-accepts_nested_attributes/resource.rb', line 34

def sanitize_nested_attributes(attributes)
  attributes # noop
end

#saveObject

Saves the resource and destroys nested resources marked for destruction.



40
41
42
43
44
# File 'lib/dm-accepts_nested_attributes/resource.rb', line 40

def save(*)
  saved = super
  remove_destroyables
  saved
end