Sha256: f081b5e18afa457bc233d6ba59f174c08644a6b039297746ad10229afadaf48c

Contents?: true

Size: 1.08 KB

Versions: 31

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module StoreModel
  module CombineErrorsStrategies
    # +MergeErrorStrategy+ copies errors from the StoreModel::Model to the parent
    # record (for Rails < 6.1) or marks the attribute invalid (for Rails >= 6.1).
    class MergeErrorStrategy
      # Merges errors on +attribute+ from the child model with parent errors.
      #
      # @param attribute [String] name of the validated attribute
      # @param base_errors [ActiveModel::Errors] errors object of the parent record
      # @param store_model_errors [ActiveModel::Errors] errors object of the StoreModel::Model
      # attribute
      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
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
store_model-4.2.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-4.1.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-4.0.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-3.0.2 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-3.0.1 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-3.0.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.4.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.3.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.2.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.1.2 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.1.1 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.1.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.0.1 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-2.0.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-1.6.2 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-1.6.1 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-1.6.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-1.5.1 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-1.5.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb
store_model-1.4.0 lib/store_model/combine_errors_strategies/merge_error_strategy.rb