Fixes numeric fields with validates_numericality_of with symbols and procs

This commit is contained in:
Rafael Mendonça França 2011-03-12 01:12:01 -03:00
parent baa1740f16
commit 1651dc04cf
4 changed files with 53 additions and 3 deletions

View File

@ -37,7 +37,7 @@ module SimpleForm
if integer? && validator_options.key?(:greater_than)
evaluate_validator_option(validator_options[:greater_than]) + 1
else
validator_options[:greater_than_or_equal_to]
evaluate_validator_option(validator_options[:greater_than_or_equal_to])
end
end
@ -45,7 +45,7 @@ module SimpleForm
if integer? && validator_options.key?(:less_than)
evaluate_validator_option(validator_options[:less_than]) - 1
else
validator_options[:less_than_or_equal_to]
evaluate_validator_option(validator_options[:less_than_or_equal_to])
end
end

View File

@ -212,6 +212,14 @@ class InputTest < ActionView::TestCase
assert_select 'input[min=11]'
end
test 'input should infer min value from integer attributes with greater than or equal to validation using symbol' do
with_input_for @validating_user, :attempts, :float
assert_select 'input[min=1]'
with_input_for @validating_user, :attempts, :integer
assert_select 'input[min=1]'
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]'
@ -220,6 +228,14 @@ class InputTest < ActionView::TestCase
assert_select 'input[min=20]'
end
test 'input should infer min value from integer attributes with greater than or equal to validation using proc' do
with_input_for @other_validating_user, :attempts, :float
assert_select 'input[min=19]'
with_input_for @other_validating_user, :attempts, :integer
assert_select 'input[min=19]'
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]'
@ -236,6 +252,14 @@ class InputTest < ActionView::TestCase
assert_select 'input[max=99]'
end
test 'input should infer max value from attributes with less than or equal to validation using symbol' do
with_input_for @validating_user, :attempts, :float
assert_select 'input[max=100]'
with_input_for @validating_user, :attempts, :integer
assert_select 'input[max=100]'
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]'
@ -244,6 +268,14 @@ class InputTest < ActionView::TestCase
assert_select 'input[max=118]'
end
test 'input should infer max value from attributes with less than or equal to validation using proc' do
with_input_for @other_validating_user, :attempts, :float
assert_select 'input[max=119]'
with_input_for @other_validating_user, :attempts, :integer
assert_select 'input[max=119]'
end
test 'input should infer step value only from integer attribute' do
with_input_for @validating_user, :age, :float
assert_no_select 'input[step]'

View File

@ -41,7 +41,7 @@ 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,
:amount
:amount, :attempts
def initialize(options={})
options.each do |key, value|
@ -74,6 +74,7 @@ class User
when :lock_version then :integer
when :home_picture then :string
when :amount then :integer
when :attempts then :integer
end
Column.new(attribute, column_type, limit)
end
@ -130,6 +131,10 @@ class ValidatingUser < User
:greater_than => :min_amount,
:less_than => :max_amount,
:only_integer => true
validates_numericality_of :attempts,
:greater_than_or_equal_to => :min_attempts,
:less_than_or_equal_to => :max_attempts,
:only_integer => true
def min_amount
10
@ -138,6 +143,14 @@ class ValidatingUser < User
def max_amount
100
end
def min_attempts
1
end
def max_attempts
100
end
end
class OtherValidatingUser < User
@ -150,4 +163,8 @@ class OtherValidatingUser < User
:greater_than => Proc.new { |user| user.age },
:less_than => Proc.new { |user| user.age + 100},
:only_integer => true
validates_numericality_of :attempts,
:greater_than_or_equal_to => Proc.new { |user| user.age },
:less_than_or_equal_to => Proc.new { |user| user.age + 100},
:only_integer => true
end

View File

@ -59,6 +59,7 @@ class ActionView::TestCase
:created_at => Time.now,
:age => 19,
:amount => 15,
:attempts => 1,
:company => 1
}.merge(options))