Module: GenericFormFor::Inputs::Base::Validations

Included in:
GenericFormFor::Inputs::Base
Defined in:
lib/generic_form_for/inputs/base/validations.rb

Instance Method Summary collapse

Instance Method Details

#decimalsObject



33
34
35
36
37
# File 'lib/generic_form_for/inputs/base/validations.rb', line 33

def decimals
  return 1 if column.try(:type) == :integer
  scale = column.try(:scale).to_f
  scale == 0 ? nil : "0.#{"0"*(scale-1)}1"
end

#limitObject



29
30
31
# File 'lib/generic_form_for/inputs/base/validations.rb', line 29

def limit
  column.try(:limit)
end

#limits_from_validationsObject



14
15
16
# File 'lib/generic_form_for/inputs/base/validations.rb', line 14

def limits_from_validations
  @limits_from_validations ||= object.class.validators_on(method).map { |v| v.options if v.kind_of? ActiveModel::Validations::NumericalityValidator }.compact.first if object.class.respond_to?(:validators_on)
end

#maximal_numberObject



24
25
26
27
# File 'lib/generic_form_for/inputs/base/validations.rb', line 24

def maximal_number
  return nil unless max = limits_from_validations
  max[:less_than_or_equal_to] || (max[:less_than] ?  max[:less_than] - decimals.to_f : nil)
end

#minimal_numberObject



18
19
20
21
# File 'lib/generic_form_for/inputs/base/validations.rb', line 18

def minimal_number
  return nil unless min = limits_from_validations
  min[:greater_than_or_equal_to] || (min[:greater_than] ?  min[:greater_than] + decimals.to_f : nil)
end

#required?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/generic_form_for/inputs/base/validations.rb', line 6

def required?
  options.key?(:required) ? options[:required] : required_from_validations?
end

#required_from_validations?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/generic_form_for/inputs/base/validations.rb', line 10

def required_from_validations?
  @required_from_validations ||= object.class.validators_on(method).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator } if object.class.respond_to?(:validators_on)
end