Module: StoreModel::NestedAttributes::ClassMethods

Defined in:
lib/store_model/nested_attributes.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributes_for(*associations) ⇒ Object

Enables handling of nested StoreModel::Model attributes

Supported options:

:allow_destroy

If true, destroys any members from the attributes hash with a _destroy key and a value that evaluates to true (e.g. 1, ‘1’, true, or ‘true’). This option is off by default.

Parameters:

  • associations (Array)

    list of associations and options to define attributes, for example: accepts_nested_attributes_for [:suppliers, allow_destroy: true]



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/store_model/nested_attributes.rb', line 21

def accepts_nested_attributes_for(*associations)
  associations.each do |association, options|
    case attribute_types[association.to_s]
    when Types::One
      define_association_setter_for_single(association, options)
      alias_method "#{association}_attributes=", "#{association}="
    when Types::Many
      define_association_setter_for_many(association, options)
    end

    define_attr_accessor_for_destroy(association, options)
  end
end