Sha256: 60c56e77ae6d1fe5947ecd614ed17ea4f871dd83f5ddc35719d6ccd6bf2452f3

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

module ActiveModel
  module Validations
    module HelperMethods
      def attribute_required?(attribute)
        self.validators.grep(PresenceValidator).any? do |v|
          v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless]).empty?
        end
      end

      def attribute_maxlength(attribute)
        self.validators.grep(LengthValidator).select {|v|
          v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank, :tokenizer]).empty?
        }.map {|v| v.options.slice(:maximum, :is)}.map(&:values).flatten.max
      end

      def attribute_max(attribute)
        self.validators.grep(NumericalityValidator).select {|v|
          v.attributes.include?(attribute.to_sym) && (v.options.keys & [:less_than, :less_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty?
        }.map {|v| v.options.slice(:less_than, :less_than_or_equal_to)}.map(&:values).flatten.max
      end

      def attribute_min(attribute)
        self.validators.grep(NumericalityValidator).select {|v|
          v.attributes.include?(attribute.to_sym) && (v.options.keys & [:greater_than, :greater_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty?
        }.map {|v| v.options.slice(:greater_than, :greater_than_or_equal_to)}.map(&:values).flatten.min
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
html5_validators-1.2.2 lib/html5_validators/active_model/helper_methods.rb
html5_validators-1.2.1 lib/html5_validators/active_model/helper_methods.rb
html5_validators-1.2.0 lib/html5_validators/active_model/helper_methods.rb
html5_validators-1.1.3 lib/html5_validators/active_model/helper_methods.rb
html5_validators-1.1.2 lib/html5_validators/active_model/helper_methods.rb
html5_validators-1.1.1 lib/html5_validators/active_model/helper_methods.rb
html5_validators-1.1.0 lib/html5_validators/active_model/helper_methods.rb
html5_validators-1.0.0 lib/html5_validators/active_model/helper_methods.rb
html5_validators-0.2.0 lib/html5_validators/active_model/helper_methods.rb
html5_validators-0.1.0 lib/html5_validators/active_model/helper_methods.rb
html5_validators-0.0.2 lib/html5_validators/active_model/helper_methods.rb
html5_validators-0.0.1 lib/html5_validators/active_model/helper_methods.rb