Module: StoreModel::NestedAttributes

Defined in:
lib/store_model/nested_attributes.rb

Overview

Contains methods for working with nested StoreModel::Model attributes.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



6
7
8
# File 'lib/store_model/nested_attributes.rb', line 6

def self.included(base) # :nodoc:
  base.extend ClassMethods
end

Instance Method Details

#assign_nested_attributes_for_collection_association(association, attributes, options) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/store_model/nested_attributes.rb', line 91

def assign_nested_attributes_for_collection_association(association, attributes, options)
  attributes = attributes.values if attributes.is_a?(Hash)

  if options&.dig(:allow_destroy)
    attributes.reject! do |attribute|
      ActiveRecord::Type::Boolean.new.cast(attribute.stringify_keys.dig("_destroy"))
    end
  end

  attributes.reject! { |attribute| call_reject_if(attribute, options[:reject_if]) } if options&.dig(:reject_if)

  send("#{association}=", attributes)
end

#call_reject_if(attributes, callback) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/store_model/nested_attributes.rb', line 105

def call_reject_if(attributes, callback)
  callback = ActiveRecord::NestedAttributes::ClassMethods::REJECT_ALL_BLANK_PROC if callback == :all_blank

  case callback
  when Symbol
    method(callback).arity.zero? ? send(callback) : send(callback, attributes)
  when Proc
    callback.call(attributes)
  end
end