Make numeric fields work with validates_numericality_of using symbols and procs
This commit is contained in:
parent
1f58d9345a
commit
cdc4f20f30
|
@ -35,7 +35,7 @@ module SimpleForm
|
|||
|
||||
def minimum_value(validator_options)
|
||||
if integer? && validator_options.key?(:greater_than)
|
||||
validator_options[:greater_than] + 1
|
||||
sanitize_validator_option(validator_options[:greater_than]) + 1
|
||||
else
|
||||
validator_options[:greater_than_or_equal_to]
|
||||
end
|
||||
|
@ -43,7 +43,7 @@ module SimpleForm
|
|||
|
||||
def maximum_value(validator_options)
|
||||
if integer? && validator_options.key?(:less_than)
|
||||
validator_options[:less_than] - 1
|
||||
sanitize_validator_option(validator_options[:less_than]) - 1
|
||||
else
|
||||
validator_options[:less_than_or_equal_to]
|
||||
end
|
||||
|
@ -52,6 +52,14 @@ module SimpleForm
|
|||
def find_numericality_validator
|
||||
attribute_validators.find { |v| ActiveModel::Validations::NumericalityValidator === v }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sanitize_validator_option(option)
|
||||
return option if option.is_a?(Numeric)
|
||||
return object.send(option) if option.is_a?(Symbol)
|
||||
return option.call(object) if option.respond_to?(:call)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -204,6 +204,22 @@ class InputTest < ActionView::TestCase
|
|||
assert_select 'input[min=18]'
|
||||
end
|
||||
|
||||
test 'input should infer min value from integer attributes with greater than validation using symbol' do
|
||||
with_input_for @validating_user, :amount, :float
|
||||
assert_no_select 'input[min]'
|
||||
|
||||
with_input_for @validating_user, :amount, :integer
|
||||
assert_select 'input[min=11]'
|
||||
end
|
||||
|
||||
test 'input should infer min value from integer attributes with greater than validation using proc' do
|
||||
with_input_for @other_validating_user, :amount, :float
|
||||
assert_no_select 'input[min]'
|
||||
|
||||
with_input_for @other_validating_user, :amount, :integer
|
||||
assert_select 'input[min=20]'
|
||||
end
|
||||
|
||||
test 'input should infer max value from attributes with less than validation' do
|
||||
with_input_for @other_validating_user, :age, :float
|
||||
assert_no_select 'input[max]'
|
||||
|
@ -212,6 +228,22 @@ class InputTest < ActionView::TestCase
|
|||
assert_select 'input[max=99]'
|
||||
end
|
||||
|
||||
test 'input should infer max value from attributes with less than validation using symbol' do
|
||||
with_input_for @validating_user, :amount, :float
|
||||
assert_no_select 'input[max]'
|
||||
|
||||
with_input_for @validating_user, :amount, :integer
|
||||
assert_select 'input[max=99]'
|
||||
end
|
||||
|
||||
test 'input should infer max value from attributes with less than validation using proc' do
|
||||
with_input_for @other_validating_user, :amount, :float
|
||||
assert_no_select 'input[max]'
|
||||
|
||||
with_input_for @other_validating_user, :amount, :integer
|
||||
assert_select 'input[max=118]'
|
||||
end
|
||||
|
||||
test 'input should infer step value only from integer attribute' do
|
||||
with_input_for @validating_user, :age, :float
|
||||
assert_no_select 'input[step]'
|
||||
|
|
|
@ -40,7 +40,8 @@ class User
|
|||
|
||||
attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :description, :created_at, :updated_at,
|
||||
:credit_limit, :age, :password, :delivery_time, :born_at, :special_company_id, :country, :url, :tag_ids,
|
||||
:avatar, :home_picture, :email, :status, :residence_country, :phone_number, :post_count, :lock_version
|
||||
:avatar, :home_picture, :email, :status, :residence_country, :phone_number, :post_count, :lock_version,
|
||||
:amount
|
||||
|
||||
def initialize(options={})
|
||||
options.each do |key, value|
|
||||
|
@ -72,6 +73,7 @@ class User
|
|||
when :updated_at then :timestamp
|
||||
when :lock_version then :integer
|
||||
when :home_picture then :string
|
||||
when :amount then :integer
|
||||
end
|
||||
Column.new(attribute, column_type, limit)
|
||||
end
|
||||
|
@ -124,6 +126,18 @@ class ValidatingUser < User
|
|||
:greater_than_or_equal_to => 18,
|
||||
:less_than_or_equal_to => 99,
|
||||
:only_integer => true
|
||||
validates_numericality_of :amount,
|
||||
:greater_than => :min_amount,
|
||||
:less_than => :max_amount,
|
||||
:only_integer => true
|
||||
|
||||
def min_amount
|
||||
10
|
||||
end
|
||||
|
||||
def max_amount
|
||||
100
|
||||
end
|
||||
end
|
||||
|
||||
class OtherValidatingUser < User
|
||||
|
@ -132,4 +146,8 @@ class OtherValidatingUser < User
|
|||
:greater_than => 17,
|
||||
:less_than => 100,
|
||||
:only_integer => true
|
||||
validates_numericality_of :amount,
|
||||
:greater_than => Proc.new { |user| user.age },
|
||||
:less_than => Proc.new { |user| user.age + 100},
|
||||
:only_integer => true
|
||||
end
|
||||
|
|
|
@ -58,6 +58,7 @@ class ActionView::TestCase
|
|||
:description => 'Hello!',
|
||||
:created_at => Time.now,
|
||||
:age => 19,
|
||||
:amount => 15,
|
||||
:company => 1
|
||||
}.merge(options))
|
||||
|
||||
|
|
Loading…
Reference in New Issue