2011-12-04 10:31:13 -05:00
|
|
|
module SimpleForm
|
|
|
|
module Components
|
|
|
|
module MinMax
|
2014-03-11 18:41:25 -04:00
|
|
|
def min_max(wrapper_options = nil)
|
2011-12-04 10:31:13 -05:00
|
|
|
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
|
2012-02-29 18:08:24 -05:00
|
|
|
nil
|
2011-12-04 10:31:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def integer?
|
|
|
|
input_type == :integer
|
|
|
|
end
|
|
|
|
|
|
|
|
def minimum_value(validator_options)
|
|
|
|
if integer? && validator_options.key?(:greater_than)
|
2012-01-01 09:32:08 -05:00
|
|
|
evaluate_numericality_validator_option(validator_options[:greater_than]) + 1
|
2011-12-04 10:31:13 -05:00
|
|
|
else
|
2012-01-01 09:32:08 -05:00
|
|
|
evaluate_numericality_validator_option(validator_options[:greater_than_or_equal_to])
|
2011-12-04 10:31:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def maximum_value(validator_options)
|
|
|
|
if integer? && validator_options.key?(:less_than)
|
2012-01-01 09:32:08 -05:00
|
|
|
evaluate_numericality_validator_option(validator_options[:less_than]) - 1
|
2011-12-04 10:31:13 -05:00
|
|
|
else
|
2012-01-01 09:32:08 -05:00
|
|
|
evaluate_numericality_validator_option(validator_options[:less_than_or_equal_to])
|
2011-12-04 10:31:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_numericality_validator
|
2012-10-02 23:05:47 -04:00
|
|
|
find_validator(:numericality)
|
2011-12-04 10:31:13 -05:00
|
|
|
end
|
|
|
|
|
2012-01-01 09:32:08 -05:00
|
|
|
def evaluate_numericality_validator_option(option)
|
2011-12-04 10:31:13 -05:00
|
|
|
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
|
2011-12-10 13:08:04 -05:00
|
|
|
end
|