1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/lib/simple_form/components/min_max.rb

50 lines
1.4 KiB
Ruby
Raw Normal View History

2011-12-04 10:31:13 -05:00
module SimpleForm
module Components
module MinMax
def min_max
if numeric_validator = find_numericality_validator
validator_options = numeric_validator.options
input_html_options[:min] ||= minimum_value(validator_options)
input_html_options[:max] ||= maximum_value(validator_options)
end
end
private
def integer?
input_type == :integer
end
def minimum_value(validator_options)
if integer? && validator_options.key?(:greater_than)
evaluate_validator_option(validator_options[:greater_than]) + 1
else
evaluate_validator_option(validator_options[:greater_than_or_equal_to])
end
end
def maximum_value(validator_options)
if integer? && validator_options.key?(:less_than)
evaluate_validator_option(validator_options[:less_than]) - 1
else
evaluate_validator_option(validator_options[:less_than_or_equal_to])
end
end
def find_numericality_validator
find_validator(ActiveModel::Validations::NumericalityValidator)
end
def evaluate_validator_option(option)
if option.is_a?(Numeric)
option
elsif option.is_a?(Symbol)
object.send(option)
elsif option.respond_to?(:call)
option.call(object)
end
end
end
end
end