Module: GenericFormFor::Inputs::Base::Validations
- Included in:
- GenericFormFor::Inputs::Base
- Defined in:
- lib/generic_form_for/inputs/base/validations.rb
Instance Method Summary collapse
- #decimals ⇒ Object
- #limit ⇒ Object
- #limits_from_validations ⇒ Object
- #maximal_number ⇒ Object
- #minimal_number ⇒ Object
- #required? ⇒ Boolean
- #required_from_validations? ⇒ Boolean
Instance Method Details
#decimals ⇒ Object
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 |
#limit ⇒ Object
29 30 31 |
# File 'lib/generic_form_for/inputs/base/validations.rb', line 29 def limit column.try(:limit) end |
#limits_from_validations ⇒ Object
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. if v.kind_of? ActiveModel::Validations::NumericalityValidator }.compact.first if object.class.respond_to?(:validators_on) end |
#maximal_number ⇒ Object
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_number ⇒ Object
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
6 7 8 |
# File 'lib/generic_form_for/inputs/base/validations.rb', line 6 def required? .key?(:required) ? [:required] : required_from_validations? end |
#required_from_validations? ⇒ 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 |