heartcombo--simple_form/lib/simple_form/inputs/numeric_input.rb

58 lines
1.5 KiB
Ruby
Raw Normal View History

module SimpleForm
module Inputs
class NumericInput < Base
def input
input_html_options[:type] ||= "number"
input_html_options[:size] ||= SimpleForm.default_input_size
input_html_options[:step] ||= 1 if integer?
infer_attributes_from_validations!
@builder.text_field(attribute_name, input_html_options)
end
def input_html_classes
super.unshift("numeric")
end
protected
def has_placeholder?
placeholder_present?
end
def infer_attributes_from_validations!
return unless has_validators?
numeric_validator = find_numericality_validator or return
validator_options = numeric_validator.options
input_html_options[:min] ||= minimum_value(validator_options)
input_html_options[:max] ||= maximum_value(validator_options)
end
def integer?
input_type == :integer
end
2010-09-05 15:54:00 +00:00
def minimum_value(validator_options)
if integer? && validator_options.key?(:greater_than)
validator_options[:greater_than] + 1
else
validator_options[:greater_than_or_equal_to]
end
2010-09-05 16:46:02 +00:00
end
def maximum_value(validator_options)
if integer? && validator_options.key?(:less_than)
validator_options[:less_than] - 1
else
validator_options[:less_than_or_equal_to]
end
2010-09-05 16:46:02 +00:00
end
def find_numericality_validator
attribute_validators.find { |v| ActiveModel::Validations::NumericalityValidator === v }
2010-09-05 15:54:00 +00:00
end
end
end
end