Class: StoreModel::CombineErrorsStrategies::MergeErrorStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/store_model/combine_errors_strategies/merge_error_strategy.rb

Overview

MergeErrorStrategy copies errors from the StoreModel::Model to the parent record (for Rails < 6.1) or marks the attribute invalid (for Rails >= 6.1).

Instance Method Summary collapse

Instance Method Details

#call(attribute, base_errors, store_model_errors) ⇒ Object

Merges errors on attribute from the child model with parent errors.

attribute

Parameters:

  • attribute (String)

    name of the validated attribute

  • base_errors (ActiveModel::Errors)

    errors object of the parent record

  • store_model_errors (ActiveModel::Errors)

    errors object of the StoreModel::Model



14
15
16
17
18
19
20
21
22
23
# File 'lib/store_model/combine_errors_strategies/merge_error_strategy.rb', line 14

def call(attribute, base_errors, store_model_errors)
  if Rails::VERSION::MAJOR < 6 || Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR.zero?
    base_errors.delete(attribute)
    store_model_errors.each { |field, error| base_errors.add(field, error) }
  else
    store_model_errors.errors.each do |error|
      base_errors.add(attribute, :invalid, message: error.full_message)
    end
  end
end