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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/store_model/nested_attributes.rb', line 112

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.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



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

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