Module: ActiveModel::Validations::HelperMethods

Defined in:
lib/html5_validators/active_model/helper_methods.rb

Instance Method Summary collapse

Instance Method Details

#attribute_max(attribute) ⇒ Object



16
17
18
19
20
# File 'lib/html5_validators/active_model/helper_methods.rb', line 16

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

#attribute_maxlength(attribute) ⇒ Object



10
11
12
13
14
# File 'lib/html5_validators/active_model/helper_methods.rb', line 10

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

#attribute_min(attribute) ⇒ Object



22
23
24
25
26
# File 'lib/html5_validators/active_model/helper_methods.rb', line 22

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

#attribute_required?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
# File 'lib/html5_validators/active_model/helper_methods.rb', line 4

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