Skip to content

Commit 20d27f6

Browse files
author
David Heinemeier Hansson
committed
Fixed validates_numericality_of to work with overrided getter-method when :allow_nil is on #1316 [raidel@onemail.at]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1603 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
1 parent 9ac0277 commit 20d27f6

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

activerecord/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*SVN*
22

3+
* Fixed validates_numericality_of to work with overrided getter-method when :allow_nil is on #1316 [raidel@onemail.at]
4+
35
* Added roots, root, and siblings to the batch of methods added by acts_as_tree #1541 [michael@schuerig.de]
46

57
* Added support for limit/offset with the MS SQL Server driver so that pagination will now work #1569 [DeLynn Berry]

activerecord/lib/active_record/validations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ def validates_numericality_of(*attr_names)
619619
end
620620
else
621621
validates_each(attr_names,configuration) do |record, attr_name,value|
622+
next if configuration[:allow_nil] and record.send("#{attr_name}_before_type_cast").nil?
622623
begin
623624
Kernel.Float(record.send("#{attr_name}_before_type_cast").to_s)
624625
rescue ArgumentError, TypeError

activerecord/test/validations_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ def test_validates_inclusion_of_with_allow_nil
319319
assert Topic.create("title" => nil, "content" => "abc").valid?
320320
end
321321

322+
def test_numericality_with_allow_nil_and_getter_method
323+
Developer.validates_numericality_of( :salary, :allow_nil => true)
324+
developer = Developer.new("name" => "michael", "salary" => nil)
325+
developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end")
326+
assert developer.valid?
327+
end
328+
322329
def test_validates_exclusion_of
323330
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) )
324331

0 commit comments

Comments
 (0)