Permalink
Browse files

Deprecate Error#on(attribute) in favour of Errors#[attribute]

  • Loading branch information...
1 parent 4367f39 commit 37283a6aaec244cb484e24b3e9ff165e89eadd64 @lifo lifo committed Mar 20, 2009
@@ -1,10 +1,10 @@
module ActiveModel
module DeprecatedErrorMethods
def on(attribute)
- # message = "Errors#on have been deprecated, use Errors#[] instead.\n"
- # message << "Also note that the behaviour of Errors#[] has changed. Errors#[] now always returns an Array and an empty Array when "
- # message << "there are not errors on the specified attribute."
- # ActiveSupport::Deprecation.warn(message)
+ message = "Errors#on have been deprecated, use Errors#[] instead.\n"
+ message << "Also note that the behaviour of Errors#[] has changed. Errors#[] now always returns an Array and an empty Array when "
+ message << "there are not errors on the specified attribute."
+ ActiveSupport::Deprecation.warn(message)
errors = self[attribute]
errors.size < 2 ? errors.first : errors
@@ -24,7 +24,7 @@ def test_validates_exclusion_of_with_formatted_message
t = Topic.create("title" => "monkey")
assert !t.valid?
- assert t.errors.on(:title)
- assert_equal "option monkey is restricted", t.errors.on(:title)
+ assert t.errors[:title].any?
+ assert_equal ["option monkey is restricted"], t.errors[:title]
end
end
@@ -66,7 +66,7 @@ def test_validate_format_numeric
def test_validate_format_with_formatted_message
Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be {{value}}")
t = Topic.create(:title => 'Invalid title')
- assert_equal "can't be Invalid title", t.errors.on(:title)
+ assert_equal ["can't be Invalid title"], t.errors[:title]
end
def test_validates_format_of_with_custom_error_using_quotes
@@ -389,7 +389,7 @@ def test_validates_confirmation_of_finds_custom_model_key_translation
Topic.validates_confirmation_of :title
@topic.title_confirmation = 'foo'
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_confirmation_of_finds_global_default_translation
@@ -398,7 +398,7 @@ def test_validates_confirmation_of_finds_global_default_translation
Topic.validates_confirmation_of :title
@topic.title_confirmation = 'foo'
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_acceptance_of w/o mocha
@@ -409,15 +409,15 @@ def test_validates_acceptance_of_finds_custom_model_key_translation
Topic.validates_acceptance_of :title, :allow_nil => false
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_acceptance_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}}
Topic.validates_acceptance_of :title, :allow_nil => false
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_presence_of w/o mocha
@@ -428,15 +428,15 @@ def test_validates_presence_of_finds_custom_model_key_translation
Topic.validates_presence_of :title
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_presence_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}}
Topic.validates_presence_of :title
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_length_of :within w/o mocha
@@ -447,15 +447,15 @@ def test_validates_length_of_within_finds_custom_model_key_translation
Topic.validates_length_of :title, :within => 3..5
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_length_of_within_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}}
Topic.validates_length_of :title, :within => 3..5
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_length_of :is w/o mocha
@@ -466,15 +466,15 @@ def test_validates_length_of_is_finds_custom_model_key_translation
Topic.validates_length_of :title, :is => 5
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_length_of_is_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
Topic.validates_length_of :title, :is => 5
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
def test_validates_length_of_is_finds_custom_model_key_translation
@@ -483,15 +483,15 @@ def test_validates_length_of_is_finds_custom_model_key_translation
Topic.validates_length_of :title, :is => 5
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_length_of_is_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
Topic.validates_length_of :title, :is => 5
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
@@ -503,15 +503,15 @@ def test_validates_format_of_finds_custom_model_key_translation
Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_format_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_inclusion_of w/o mocha
@@ -522,15 +522,15 @@ def test_validates_inclusion_of_finds_custom_model_key_translation
Topic.validates_inclusion_of :title, :in => %w(a b c)
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_inclusion_of_finds_global_default_translation
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}}
Topic.validates_inclusion_of :title, :in => %w(a b c)
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_exclusion_of w/o mocha
@@ -542,7 +542,7 @@ def test_validates_exclusion_of_finds_custom_model_key_translation
Topic.validates_exclusion_of :title, :in => %w(a b c)
@topic.title = 'a'
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_exclusion_of_finds_global_default_translation
@@ -551,7 +551,7 @@ def test_validates_exclusion_of_finds_global_default_translation
Topic.validates_exclusion_of :title, :in => %w(a b c)
@topic.title = 'a'
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_numericality_of without :only_integer w/o mocha
@@ -563,7 +563,7 @@ def test_validates_numericality_of_finds_custom_model_key_translation
Topic.validates_numericality_of :title
@topic.title = 'a'
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_numericality_of_finds_global_default_translation
@@ -572,7 +572,7 @@ def test_validates_numericality_of_finds_global_default_translation
Topic.validates_numericality_of :title, :only_integer => true
@topic.title = 'a'
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_numericality_of with :only_integer w/o mocha
@@ -584,7 +584,7 @@ def test_validates_numericality_of_only_integer_finds_custom_model_key_translati
Topic.validates_numericality_of :title, :only_integer => true
@topic.title = 'a'
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_numericality_of_only_integer_finds_global_default_translation
@@ -593,7 +593,7 @@ def test_validates_numericality_of_only_integer_finds_global_default_translation
Topic.validates_numericality_of :title, :only_integer => true
@topic.title = 'a'
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_numericality_of :odd w/o mocha
@@ -605,7 +605,7 @@ def test_validates_numericality_of_odd_finds_custom_model_key_translation
Topic.validates_numericality_of :title, :only_integer => true, :odd => true
@topic.title = 0
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_numericality_of_odd_finds_global_default_translation
@@ -614,7 +614,7 @@ def test_validates_numericality_of_odd_finds_global_default_translation
Topic.validates_numericality_of :title, :only_integer => true, :odd => true
@topic.title = 0
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
# validates_numericality_of :less_than w/o mocha
@@ -626,7 +626,7 @@ def test_validates_numericality_of_less_than_finds_custom_model_key_translation
Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
@topic.title = 1
@topic.valid?
- assert_equal 'custom message', @topic.errors.on(:title)
+ assert_equal ['custom message'], @topic.errors[:title]
end
def test_validates_numericality_of_less_than_finds_global_default_translation
@@ -635,38 +635,38 @@ def test_validates_numericality_of_less_than_finds_global_default_translation
Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
@topic.title = 1
@topic.valid?
- assert_equal 'global message', @topic.errors.on(:title)
+ assert_equal ['global message'], @topic.errors[:title]
end
def test_validations_with_message_symbol_must_translate
I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}}
Topic.validates_presence_of :title, :message => :custom_error
@topic.title = nil
@topic.valid?
- assert_equal "I am a custom error", @topic.errors.on(:title)
+ assert_equal ["I am a custom error"], @topic.errors[:title]
end
def test_validates_with_message_symbol_must_translate_per_attribute
I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}}
Topic.validates_presence_of :title, :message => :custom_error
@topic.title = nil
@topic.valid?
- assert_equal "I am a custom error", @topic.errors.on(:title)
+ assert_equal ["I am a custom error"], @topic.errors[:title]
end
def test_validates_with_message_symbol_must_translate_per_model
I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}}
Topic.validates_presence_of :title, :message => :custom_error
@topic.title = nil
@topic.valid?
- assert_equal "I am a custom error", @topic.errors.on(:title)
+ assert_equal ["I am a custom error"], @topic.errors[:title]
end
def test_validates_with_message_string
Topic.validates_presence_of :title, :message => "I am a custom error"
@topic.title = nil
@topic.valid?
- assert_equal "I am a custom error", @topic.errors.on(:title)
+ assert_equal ["I am a custom error"], @topic.errors[:title]
end
end
@@ -21,8 +21,8 @@ def test_validates_inclusion_of
assert t.valid?
t.title = "uhoh"
assert !t.valid?
- assert t.errors.on(:title)
- assert_equal "is not included in the list", t.errors.on(:title)
+ assert t.errors[:title].any?
+ assert_equal ["is not included in the list"], t.errors[:title]
assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => nil ) }
assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => 0) }
@@ -47,8 +47,8 @@ def test_validates_inclusion_of_with_formatted_message
t = Topic.create("title" => "uhoh", "content" => "abc")
assert !t.valid?
- assert t.errors.on(:title)
- assert_equal "option uhoh is not in the list", t.errors.on(:title)
+ assert t.errors[:title].any?
+ assert_equal ["option uhoh is not in the list"], t.errors[:title]
end
def test_validates_inclusion_of_with_custom_error_using_quotes
Oops, something went wrong.

0 comments on commit 37283a6

Please sign in to comment.