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
# 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.copy!(store_model_errors)
  else
    store_model_errors.errors.each do |error|
      base_errors.add(:configuration, :invalid, message: error.full_message)
    end
  end
end