Skip to content
This repository
Browse code

Fix exclusive range patch to use begin/end instead of min/max. [#2981

…status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 5632b36701ad9514d596c558877cd74c14c1d54b 1 parent bee3e09
Adam Keys authored August 08, 2009 lifo committed August 08, 2009
12  activemodel/lib/active_model/validations/length.rb
@@ -66,10 +66,14 @@ def validates_length_of(*attrs)
66 66
 
67 67
           validates_each(attrs, options) do |record, attr, value|
68 68
             value = options[:tokenizer].call(value) if value.kind_of?(String)
69  
-            if value.nil? or value.size < option_value.begin
70  
-              record.errors.add(attr, :too_short, :default => custom_message || options[:too_short], :count => option_value.begin)
71  
-            elsif value.size > option_value.end
72  
-              record.errors.add(attr, :too_long, :default => custom_message || options[:too_long], :count => option_value.end)
  69
+
  70
+            min, max = option_value.begin, option_value.end
  71
+            max = max - 1 if option_value.exclude_end?
  72
+
  73
+            if value.nil? || value.size < min
  74
+              record.errors.add(attr, :too_short, :default => custom_message || options[:too_short], :count => min)
  75
+            elsif value.size > max
  76
+              record.errors.add(attr, :too_long, :default => custom_message || options[:too_long], :count => max)
73 77
             end
74 78
           end
75 79
         when :is, :minimum, :maximum
14  activemodel/test/cases/validations/length_validation_test.rb
@@ -112,6 +112,20 @@ def test_validates_length_of_using_within
112 112
     assert t.valid?
113 113
   end
114 114
 
  115
+  def test_validates_length_of_using_within_with_exclusive_range
  116
+    Topic.validates_length_of(:title, :within => 4...10)
  117
+
  118
+    t = Topic.new("title" => "9 chars!!")
  119
+    assert t.valid?
  120
+
  121
+    t.title = "Now I'm 10"
  122
+    assert !t.valid?
  123
+    assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title]
  124
+
  125
+    t.title = "Four"
  126
+    assert t.valid?
  127
+  end
  128
+
115 129
   def test_optionally_validates_length_of_using_within
116 130
     Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true
117 131
 

0 notes on commit 5632b36

Please sign in to comment.
Something went wrong with that request. Please try again.