Sha256: 0564188b9e1a588b8bbf02427356c04a121b71eedea75854420b0f6b2840904b

Contents?: true

Size: 1.64 KB

Versions: 17

Compression:

Stored size: 1.64 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(attribute, record.errors, value)
        when :array, :polymorphic_array
          call_array_strategy(attribute, record.errors, value)
        end
      end

      private

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

      def call_array_strategy(attribute, record_errors, value)
        array_strategy.call(attribute, record_errors, value) if value.select(&:invalid?).present?
      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

17 entries across 17 versions & 1 rubygems

Version Path
store_model-1.6.0 lib/active_model/validations/store_model_validator.rb
store_model-1.5.1 lib/active_model/validations/store_model_validator.rb
store_model-1.5.0 lib/active_model/validations/store_model_validator.rb
store_model-1.4.0 lib/active_model/validations/store_model_validator.rb
store_model-1.3.0 lib/active_model/validations/store_model_validator.rb
store_model-1.2.0 lib/active_model/validations/store_model_validator.rb
store_model-1.1.0 lib/active_model/validations/store_model_validator.rb
store_model-1.0.0 lib/active_model/validations/store_model_validator.rb
store_model-0.13.0 lib/active_model/validations/store_model_validator.rb
store_model-0.12.0 lib/active_model/validations/store_model_validator.rb
store_model-0.11.1 lib/active_model/validations/store_model_validator.rb
store_model-0.11.0 lib/active_model/validations/store_model_validator.rb
store_model-0.10.0 lib/active_model/validations/store_model_validator.rb
store_model-0.9.0 lib/active_model/validations/store_model_validator.rb
store_model-0.8.2 lib/active_model/validations/store_model_validator.rb
store_model-0.8.1 lib/active_model/validations/store_model_validator.rb
store_model-0.8.0 lib/active_model/validations/store_model_validator.rb