Sha256: ed8cc48377f662ded3185a672a2e57faea7993955abd9e223c92943a00c387ef

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require "active_record"
require "store_model/combine_errors_strategies"

module ActiveModel
  module Validations
    # +StoreModelValidator+ is a subclass of ActiveModel::EachValidator for
    # checking StoreModel::Model attributes.
    class StoreModelValidator < ActiveModel::EachValidator
      # Validates _json_ attribute using the configured strategy or
      # invalidates _array_ attribute when at least one element is invalid.
      #
      # @param record [ApplicationRecord] object to validate
      # @param attribute [String] name of the validated attribute
      # @param value [Object] value of the validated attribute
      def validate_each(record, attribute, value)
        if value.nil?
          record.errors.add(attribute, :blank)
          return
        end

        case record.type_for_attribute(attribute).type
        when :json, :polymorphic
          call_json_strategy(record, attribute, value)
        when :array, :polymorphic_array
          call_array_strategy(record, attribute, value)
        end
      end

      private

      def call_json_strategy(record, attribute, value)
        strategy.call(attribute, record.errors, value.errors) if value.invalid?(record.validation_context)
      end

      def call_array_strategy(record, attribute, value)
        any_invalid = value.select { |v| v.invalid?(record.validation_context) }.present?
        array_strategy.call(attribute, record.errors, value) if any_invalid
      end

      def strategy
        @strategy ||= StoreModel::CombineErrorsStrategies.configure(options)
      end

      def array_strategy
        @array_strategy ||= StoreModel::CombineErrorsStrategies.configure_array(options)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
store_model-4.2.0 lib/active_model/validations/store_model_validator.rb
store_model-4.1.0 lib/active_model/validations/store_model_validator.rb
store_model-4.0.0 lib/active_model/validations/store_model_validator.rb
store_model-3.0.2 lib/active_model/validations/store_model_validator.rb
store_model-3.0.1 lib/active_model/validations/store_model_validator.rb
store_model-3.0.0 lib/active_model/validations/store_model_validator.rb
store_model-2.4.0 lib/active_model/validations/store_model_validator.rb
store_model-2.3.0 lib/active_model/validations/store_model_validator.rb
store_model-2.2.0 lib/active_model/validations/store_model_validator.rb
store_model-2.1.2 lib/active_model/validations/store_model_validator.rb
store_model-2.1.1 lib/active_model/validations/store_model_validator.rb
store_model-2.1.0 lib/active_model/validations/store_model_validator.rb
store_model-2.0.1 lib/active_model/validations/store_model_validator.rb
store_model-2.0.0 lib/active_model/validations/store_model_validator.rb
store_model-1.6.2 lib/active_model/validations/store_model_validator.rb
store_model-1.6.1 lib/active_model/validations/store_model_validator.rb