module GenericFormFor module Inputs module Base module Validations def required? options.key?(:required) ? options[:required] : required_from_validations? end 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 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 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 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 def limit column.try(:limit) end def decimals return 1 if column.try(:type) == :integer scale = column.try(:scale).to_f scale == 0 ? nil : "0.#{"0"*(scale-1)}1" end end end end end