aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/validations.rb8
-rwxr-xr-xactiverecord/test/validations_test.rb42
3 files changed, 49 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 6052c30174..d70800b9ef 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed that validate_length_of lost :on option when :within was specified #1195 [jhosteny@mac.com]
+
* Added encoding and min_messages options for PostgreSQL #1205 [shugo]. Configuration example:
development:
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 9af4010a51..9987c0631d 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -373,15 +373,17 @@ module ActiveRecord
option_value = options[range_options.first]
# Declare different validations per option.
-
validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
case option
when :within, :in
raise ArgumentError, ':within must be a Range' unless option_value.is_a?(Range) # '
- validates_length_of attrs, :minimum => option_value.begin, :allow_nil => options[:allow_nil]
- validates_length_of attrs, :maximum => option_value.end, :allow_nil => options[:allow_nil]
+ (options_without_range = options.dup).delete(option)
+ (options_with_minimum = options_without_range.dup).store(:minimum, option_value.begin)
+ validates_length_of attrs, options_with_minimum
+ (options_with_maximum = options_without_range.dup).store(:maximum, option_value.end)
+ validates_length_of attrs, options_with_maximum
when :is, :minimum, :maximum
raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0 # '
message = options[:message] || options[message_options[option]]
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 89df29375c..4f5f5802ef 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -394,6 +394,48 @@ class ValidationsTest < Test::Unit::TestCase
assert t.valid?
end
+ def test_optionally_validates_length_of_using_within_on_create
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "my string is too long: %d"
+
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
+ assert !t.save
+ assert t.errors.on(:title)
+ assert_equal "my string is too long: 10", t.errors[:title]
+
+ t.title = "butthisis"
+ assert t.save
+
+ t.title = "few"
+ assert t.save
+
+ t.content = "andthisislong"
+ assert t.save
+
+ t.content = t.title = "iamfine"
+ assert t.save
+ end
+
+ def test_optionally_validates_length_of_using_within_on_update
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "my string is too short: %d"
+
+ t = Topic.create("title" => "vali", "content" => "whatever")
+ assert !t.save
+ assert t.errors.on(:title)
+
+ t.title = "not"
+ assert !t.save
+ assert t.errors.on(:title)
+ assert_equal "my string is too short: 5", t.errors[:title]
+
+ t.title = "valid"
+ t.content = "andthisistoolong"
+ assert !t.save
+ assert t.errors.on(:content)
+
+ t.content = "iamfine"
+ assert t.save
+ end
+
def test_validates_length_of_using_is
Topic.validates_length_of :title, :is => 5