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 = nil) ⇒ Object

Base



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/store_model/nested_attributes.rb', line 125

def assign_nested_attributes_for_collection_association(association, attributes, options = nil)
  return super(association, attributes) unless 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["_destroy"])
    end
  end

  attributes.reject! { call_store_model_reject_if(_1, options[:reject_if]) } if options&.dig(:reject_if)

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

#call_store_model_reject_if(attributes, callback) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/store_model/nested_attributes.rb', line 141

def call_store_model_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