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(*attributes) ⇒ Object

Enables handling of nested StoreModel::Model attributes

Alternatively, use the standard Rails syntax:

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.

:reject_if

Allows you to specify a Proc or a Symbol pointing to a method that checks whether a record should be built for a certain attribute hash. The hash is passed to the supplied Proc or the method and it should return either true or false. Passing :all_blank instead of a Proc will create a proc that will reject a record where all the attributes are blank excluding any value for _destroy.

See api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for

Parameters:

  • associations (Array)

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

  • associations (Array)

    list of associations and attributes to define getters and setters.

  • options (Hash)

    options not supported by StoreModel will still be passed to ActiveRecord.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/store_model/nested_attributes.rb', line 37

def accepts_nested_attributes_for(*attributes)
  options = attributes.extract_options!

  attributes.each do |attribute|
    case nested_attribute_type(attribute)
    when Types::OneBase, Types::ManyBase
      options.reverse_merge!(allow_destroy: false, update_only: false)
      define_store_model_attr_accessors(attribute, options)
    else
      super(*attribute, options)
    end
  end
end