Skip to content

Commit

Permalink
Merge [5589] from trunk.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5590 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Nov 20, 2006
1 parent 17921ba commit f2fd22c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*1.15.0 RC1* (November 19th, 2006)

* validates_numericality_of uses \A \Z to ensure the entire string matches rather than ^ $ which may match one valid line of a multiline string. #5716 [Andreas Schwarz]

* Oracle: automatically detect the primary key. #6594 [vesaria, Michael Schoen]

* Oracle: to increase performance, prefetch 100 rows and enable similar cursor sharing. Both are configurable in database.yml. #6607 [philbogle@gmail.com, ray.fortna@jobster.com, Michael Schoen]
Expand Down
8 changes: 5 additions & 3 deletions activerecord/lib/active_record/validations.rb
Expand Up @@ -559,9 +559,11 @@ def validates_uniqueness_of(*attr_names)
# provided.
#
# class Person < ActiveRecord::Base
# validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create
# validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
# end
#
# Note: use \A and \Z to match the start and end of the string, ^ and $ match the start/end of a line.
#
# A regular expression must be provided or else an exception will be raised.
#
# Configuration options:
Expand Down Expand Up @@ -675,7 +677,7 @@ def validates_associated(*attr_names)

# Validates whether the value of the specified attribute is numeric by trying to convert it to
# a float with Kernel.Float (if <tt>integer</tt> is false) or applying it to the regular expression
# <tt>/^[\+\-]?\d+$/</tt> (if <tt>integer</tt> is set to true).
# <tt>/\A[\+\-]?\d+\Z/</tt> (if <tt>integer</tt> is set to true).
#
# class Person < ActiveRecord::Base
# validates_numericality_of :value, :on => :create
Expand All @@ -696,7 +698,7 @@ def validates_numericality_of(*attr_names)

if configuration[:only_integer]
validates_each(attr_names,configuration) do |record, attr_name,value|
record.errors.add(attr_name, configuration[:message]) unless record.send("#{attr_name}_before_type_cast").to_s =~ /^[+-]?\d+$/
record.errors.add(attr_name, configuration[:message]) unless record.send("#{attr_name}_before_type_cast").to_s =~ /\A[+-]?\d+\Z/
end
else
validates_each(attr_names,configuration) do |record, attr_name,value|
Expand Down
31 changes: 19 additions & 12 deletions activerecord/test/validations_test.rb
Expand Up @@ -1028,15 +1028,16 @@ def test_errors_to_xml
end


class ValidatesNumericalityTest
NIL = [nil, "", " ", " \t \r \n"]
class ValidatesNumericalityTest < Test::Unit::TestCase
NIL = [nil]
BLANK = ["", " ", " \t \r \n"]
BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significent digits
FLOAT_STRINGS = %w(0.0 +0.0 -0.0 10.0 10.5 -10.5 -0.0001 -090.1 90.1e1 -90.1e5 -90.1e-5 90e-5)
INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090)
FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
INTEGERS = [0, 10, -10] + INTEGER_STRINGS
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12"]
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]

def setup
Topic.write_inheritable_attribute(:validate, nil)
Expand All @@ -1047,44 +1048,50 @@ def setup
def test_default_validates_numericality_of
Topic.validates_numericality_of :approved

invalid!(NIL + JUNK)
invalid!(NIL + BLANK + JUNK)
valid!(FLOATS + INTEGERS + BIGDECIMAL)
end

def test_validates_numericality_of_with_nil_allowed
Topic.validates_numericality_of :approved, :allow_nil => true

invalid!(JUNK)
invalid!(BLANK + JUNK)
valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL)
end

def test_validates_numericality_of_with_integer_only
Topic.validates_numericality_of :approved, :only_integer => true

invalid!(NIL + JUNK + FLOATS + BIGDECIMAL)
invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL)
valid!(INTEGERS)
end

def test_validates_numericality_of_with_integer_only_and_nil_allowed
Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true

invalid!(JUNK + FLOATS + BIGDECIMAL)
invalid!(BLANK + JUNK + FLOATS + BIGDECIMAL)
valid!(NIL + INTEGERS)
end

private
def invalid!(values)
values.each do |value|
topic = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => value)
assert !topic.valid?, "#{value} not rejected as a number"
with_each_topic_approved_value(values) do |topic, value|
assert !topic.valid?, "#{value.inspect} not rejected as a number"
assert topic.errors.on(:approved)
end
end

def valid!(values)
with_each_topic_approved_value(values) do |topic, value|
assert topic.valid?, "#{value.inspect} not accepted as a number"
end
end

def with_each_topic_approved_value(values)
topic = Topic.new("title" => "numeric test", "content" => "whatever")
values.each do |value|
topic = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => value)
assert topic.valid?, "#{value} not accepted as a number"
topic.approved = value
yield topic, value
end
end
end

0 comments on commit f2fd22c

Please sign in to comment.