Sha256: cb56b7b4281f1286e776efb8f980e5af2affc23160ccb3c03daba847f7de32a0

Contents?: true

Size: 1.07 KB

Versions: 27

Compression:

Stored size: 1.07 KB

Contents

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 if !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

27 entries across 27 versions & 4 rubygems

Version Path
simple_form-3.5.0 lib/simple_form/helpers/validators.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/simple_form-3.4.0/lib/simple_form/helpers/validators.rb
simple_form-3.4.0 lib/simple_form/helpers/validators.rb
simple_form-3.3.1 lib/simple_form/helpers/validators.rb
simple_form-3.3.0 lib/simple_form/helpers/validators.rb
simple_form-3.2.1 lib/simple_form/helpers/validators.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/simple_form-3.2.0/lib/simple_form/helpers/validators.rb
simple_form-3.2.0 lib/simple_form/helpers/validators.rb
simple_form-3.1.1 lib/simple_form/helpers/validators.rb
simple_form-3.0.4 lib/simple_form/helpers/validators.rb
simple_form-2.1.3 lib/simple_form/helpers/validators.rb
simple_form-2.1.2 lib/simple_form/helpers/validators.rb
simple_form-3.0.3 lib/simple_form/helpers/validators.rb
simple_form-3.1.0 lib/simple_form/helpers/validators.rb
simple_form-3.1.0.rc2 lib/simple_form/helpers/validators.rb
simple_form-3.1.0.rc1 lib/simple_form/helpers/validators.rb
simple_form-3.0.2 lib/simple_form/helpers/validators.rb
simple_form-3.0.1 lib/simple_form/helpers/validators.rb
simple_form-2.1.1 lib/simple_form/helpers/validators.rb
simple_form-3.0.0 lib/simple_form/helpers/validators.rb