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.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/store_model/nested_attributes.rb', line 48

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

  attributes.each do |attribute|
    if nested_attribute_type(attribute).is_a?(Types::Base)
      options.reverse_merge!(allow_destroy: false, update_only: false)
      define_store_model_attr_accessors(attribute, options)
    else
      super(*attribute, options)
    end
  end
end

#attribute(name, type = nil) ⇒ Object

add storemodel type of attribute if it is storemodel type



17
18
19
20
# File 'lib/store_model/nested_attributes.rb', line 17

def attribute(name, type = nil, **)
  store_model_attribute_types[name.to_s] = type if type.is_a?(Types::Base)
  super
end

#store_model_attribute_typesObject

gather storemodel attribute types on the class-level



12
13
14
# File 'lib/store_model/nested_attributes.rb', line 12

def store_model_attribute_types
  @store_model_attribute_types ||= {}
end