Sha256: 5e8d60d99e3b5c1359204a6cca6ec8ea8c062cb48ceced1596bd95925da552a1

Contents?: true

Size: 1.1 KB

Versions: 11

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true
module SimpleForm
  module Helpers
    module Validators
      def has_validators?
        @has_validators ||= attribute_name && object.class.respond_to?(:validators_on)
      end

      private

      def attribute_validators
        object.class.validators_on(attribute_name)
      end

      def reflection_validators
        reflection ? object.class.validators_on(reflection.name) : []
      end

      def valid_validator?(validator)
        !conditional_validators?(validator) && action_validator_match?(validator)
      end

      def conditional_validators?(validator)
        validator.options.include?(:if) || validator.options.include?(:unless)
      end

      def action_validator_match?(validator)
        return true unless validator.options.include?(:on)

        case validator.options[:on]
        when :save
          true
        when :create
          !object.persisted?
        when :update
          object.persisted?
        end
      end

      def find_validator(kind)
        attribute_validators.find { |v| v.kind == kind } if has_validators?
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
simple_form-5.3.0 lib/simple_form/helpers/validators.rb
simple_form-5.2.0 lib/simple_form/helpers/validators.rb
simple_form-5.1.0 lib/simple_form/helpers/validators.rb
simple_form-5.0.3 lib/simple_form/helpers/validators.rb
simple_form-5.0.2 lib/simple_form/helpers/validators.rb
simple_form-5.0.1 lib/simple_form/helpers/validators.rb
simple_form-5.0.0 lib/simple_form/helpers/validators.rb
simple_form-4.1.0 lib/simple_form/helpers/validators.rb
simple_form-4.0.1 lib/simple_form/helpers/validators.rb
simple_form-4.0.0 lib/simple_form/helpers/validators.rb
simple_form-3.5.1 lib/simple_form/helpers/validators.rb