Class: ActsAsTable::RecordModelClassMethods::FindOrInitializeBy

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/acts_as_table/record_model_class_methods.rb

Overview

ActsAsTable "destructive" traversal (creates new records if they are not found and updates persisted records).

Direct Known Subclasses

GetValue, SetValue

Instance Method Summary collapse

Instance Method Details

#arityInteger

Returns the number of mandatory arguments.

Returns:

  • (Integer)


159
160
161
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 159

def arity
  1
end

#call(record_model, base = nil) ⇒ ActsAsTable::ValueProvider::WrappedValue

Invokes the traversal.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.



169
170
171
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 169

def call(record_model, base = nil)
  raise ::NotImplementedError.new("#{self.class}#call")
end

#inject(acc, record_model, base = nil) {|acc, path| ... } ⇒ Object

Combine all ActsAsTable paths using an associative binary operation.

Parameters:

Yield Parameters:

Yield Returns:

  • (Object)

Returns:

  • (Object)

Raises:

  • (ArgumentError)

    If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 183

def inject(acc, record_model, base = nil)
  # @return [Class]
  klass = record_model.class_name.constantize

  # @return [ActsAsTable::Path]
  path = ActsAsTable::Path.new(klass, nil, data: {
    source_record_model: nil,
    source_record: nil,
    target_record_model: record_model,
    target_record: base,
  })

  _inject(acc, path, **{
    find_or_initialize_by: ::Proc.new { |*args, &block|
      ActsAsTable.adapter.find_or_initialize_by_for(record_model, klass, :find_by!, *args, &block)
    },
    new: ::Proc.new { |*args, &block|
      ActsAsTable.adapter.new_for(record_model, klass, :new, *args, &block)
    },
  })
end