Skip to content
This repository
Browse code

Ensure validation errors to be ordered in declared order

[#2301 state:committed milestone:2.3.5]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 0990a13500d036f9b8cf4c11eb1056069357fca7 1 parent 68b2b73
Akira Matsuda authored March 19, 2009 jeremy committed September 11, 2009
3  activemodel/lib/active_model/errors.rb
... ...
@@ -1,7 +1,8 @@
1 1
 require 'active_support/core_ext/string/inflections'
  2
+require 'active_support/ordered_hash'
2 3
 
3 4
 module ActiveModel
4  
-  class Errors < Hash
  5
+  class Errors < ActiveSupport::OrderedHash
5 6
     include DeprecatedErrorMethods
6 7
 
7 8
     def initialize(base)
16  activemodel/test/cases/validations_test.rb
@@ -141,6 +141,22 @@ def test_validation_order
141 141
      t = Topic.new("title" => "")
142 142
      assert !t.valid?
143 143
      assert_equal "can't be blank", t.errors["title"].first
  144
+    Topic.validates_presence_of :title, :author_name
  145
+    Topic.validate {|topic| topic.errors.add('author_email_address', 'will never be valid')}
  146
+    Topic.validates_length_of :title, :content, :minimum => 2
  147
+
  148
+    t = Topic.new :title => ''
  149
+    assert !t.valid?
  150
+
  151
+    assert_equal :title, key = t.errors.keys.first
  152
+    assert_equal "can't be blank", t.errors[key].first
  153
+    assert_equal 'is too short (minimum is 2 characters)', t.errors[key].second
  154
+    assert_equal :author_name, key = t.errors.keys.second
  155
+    assert_equal "can't be blank", t.errors[key].first
  156
+    assert_equal :author_email_address, key = t.errors.keys.third
  157
+    assert_equal 'will never be valid', t.errors[key].first
  158
+    assert_equal :content, key = t.errors.keys.fourth
  159
+    assert_equal 'is too short (minimum is 2 characters)', t.errors[key].first
144 160
   end
145 161
 
146 162
   def test_invalid_should_be_the_opposite_of_valid

0 notes on commit 0990a13

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