From e7cdf704c97407d5a5f017271349932018f20c7c Mon Sep 17 00:00:00 2001 From: Justin French Date: Sat, 7 Feb 2009 15:48:59 +1100 Subject: [PATCH] Changed ActiveRecord's errors.on(:attribute) to return an Array instead of a String when there's only one error on the attribute, so that developers no longer need to handle the special case where it's a String. Still returns nil when there are no errors. --- activerecord/lib/active_record/validations.rb | 11 +- .../associations/belongs_to_associations_test.rb | 2 +- .../associations/has_one_associations_test.rb | 6 +- .../test/cases/autosave_association_test.rb | 2 +- activerecord/test/cases/validations_i18n_test.rb | 64 ++++---- activerecord/test/cases/validations_test.rb | 152 ++++++++++---------- 6 files changed, 117 insertions(+), 120 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 8f3c805..99e5998 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -124,23 +124,20 @@ module ActiveRecord !@errors[attribute.to_s].nil? end + # Returns an array of error messages associated with the specified +attribute+. # Returns +nil+, if no errors are associated with the specified +attribute+. - # Returns the error message, if one error is associated with the specified +attribute+. - # Returns an array of error messages, if more than one error is associated with the specified +attribute+. # # class Company < ActiveRecord::Base - # validates_presence_of :name, :address, :email + # validates_presence_of :name, :email, :address # validates_length_of :name, :in => 5..30 # end # # company = Company.create(:address => '123 First St.') # company.errors.on(:name) # => ["is too short (minimum is 5 characters)", "can't be blank"] - # company.errors.on(:email) # => "can't be blank" + # company.errors.on(:email) # => ["can't be blank"] # company.errors.on(:address) # => nil def on(attribute) - errors = @errors[attribute.to_s] - return nil if errors.nil? - errors.size == 1 ? errors.first : errors + @errors[attribute.to_s] end alias :[] :on diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 40a8503..f03a914 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -417,7 +417,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert !log.developer.valid? assert !log.valid? assert !log.save - assert_equal "is invalid", log.errors.on("developer") + assert_equal ["is invalid"], log.errors.on("developer") end def test_save_succeeds_for_invalid_belongs_to_with_validate_false diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 14032a6..7d52d3b 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -165,7 +165,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase account = firm.build_account assert !account.save - assert_equal "can't be empty", account.errors.on("credit_limit") + assert_equal ["can't be empty"], account.errors.on("credit_limit") end def test_build_association_twice_without_saving_affects_nothing @@ -223,7 +223,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_equal account, firm.account assert !account.save assert_equal account, firm.account - assert_equal "can't be empty", account.errors.on("credit_limit") + assert_equal ["can't be empty"], account.errors.on("credit_limit") end def test_create @@ -289,7 +289,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert !firm.account.valid? assert !firm.valid? assert !firm.save - assert_equal "is invalid", firm.errors.on("account") + assert_equal ["is invalid"], firm.errors.on("account") end diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 381249c..cd2daba 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -322,7 +322,7 @@ module AutosaveAssociationOnACollectionAssociationTests @pirate.send(@association_name).each { |child| child.name = '' } assert !@pirate.valid? - assert_equal "can't be blank", @pirate.errors.on("#{@association_name}_name") + assert_equal ["can't be blank"], @pirate.errors.on("#{@association_name}_name") assert @pirate.errors.on(@association_name).blank? end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 6698234..58cbfbd 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -429,7 +429,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_confirmation_of_finds_global_default_translation @@ -438,7 +438,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end # validates_acceptance_of w/o mocha @@ -449,7 +449,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? - assert_equal 'custom message', @topic.errors.on(:title) + assert_equal ['custom message'], @topic.errors.on(:title) end def test_validates_acceptance_of_finds_global_default_translation @@ -457,7 +457,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? - assert_equal 'global message', @topic.errors.on(:title) + assert_equal ['global message'], @topic.errors.on(:title) end # validates_presence_of w/o mocha @@ -468,7 +468,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_presence_of :title @topic.valid? - assert_equal 'custom message', @topic.errors.on(:title) + assert_equal ['custom message'], @topic.errors.on(:title) end def test_validates_presence_of_finds_global_default_translation @@ -476,7 +476,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_presence_of :title @topic.valid? - assert_equal 'global message', @topic.errors.on(:title) + assert_equal ['global message'], @topic.errors.on(:title) end # validates_length_of :within w/o mocha @@ -487,7 +487,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_length_of :title, :within => 3..5 @topic.valid? - assert_equal 'custom message', @topic.errors.on(:title) + assert_equal ['custom message'], @topic.errors.on(:title) end def test_validates_length_of_within_finds_global_default_translation @@ -495,7 +495,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_length_of :title, :within => 3..5 @topic.valid? - assert_equal 'global message', @topic.errors.on(:title) + assert_equal ['global message'], @topic.errors.on(:title) end # validates_length_of :is w/o mocha @@ -506,7 +506,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_length_of :title, :is => 5 @topic.valid? - assert_equal 'custom message', @topic.errors.on(:title) + assert_equal ['custom message'], @topic.errors.on(:title) end def test_validates_length_of_is_finds_global_default_translation @@ -514,7 +514,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_length_of :title, :is => 5 @topic.valid? - assert_equal 'global message', @topic.errors.on(:title) + assert_equal ['global message'], @topic.errors.on(:title) end # validates_uniqueness_of w/o mocha @@ -525,7 +525,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_length_of :title, :is => 5 @topic.valid? - assert_equal 'custom message', @topic.errors.on(:title) + assert_equal ['custom message'], @topic.errors.on(:title) end def test_validates_length_of_is_finds_global_default_translation @@ -533,7 +533,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_length_of :title, :is => 5 @topic.valid? - assert_equal 'global message', @topic.errors.on(:title) + assert_equal ['global message'], @topic.errors.on(:title) end @@ -545,7 +545,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_format_of_finds_global_default_translation @@ -553,7 +553,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end # validates_inclusion_of w/o mocha @@ -564,7 +564,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_inclusion_of_finds_global_default_translation @@ -572,7 +572,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end # validates_exclusion_of w/o mocha @@ -584,7 +584,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_exclusion_of_finds_global_default_translation @@ -593,7 +593,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end # validates_numericality_of without :only_integer w/o mocha @@ -605,7 +605,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_numericality_of :title @topic.title = 'a' @topic.valid? - assert_equal 'custom message', @topic.errors.on(:title) + assert_equal ['custom message'], @topic.errors.on(:title) end def test_validates_numericality_of_finds_global_default_translation @@ -614,7 +614,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end # validates_numericality_of with :only_integer w/o mocha @@ -626,7 +626,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_numericality_of_only_integer_finds_global_default_translation @@ -635,7 +635,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end # validates_numericality_of :odd w/o mocha @@ -647,7 +647,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_numericality_of_odd_finds_global_default_translation @@ -656,7 +656,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end # validates_numericality_of :less_than w/o mocha @@ -668,7 +668,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_numericality_of_less_than_finds_global_default_translation @@ -677,7 +677,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end @@ -689,7 +689,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_associated :replies replied_topic.valid? - assert_equal 'custom message', replied_topic.errors.on(:replies) + assert_equal ['custom message'], replied_topic.errors.on(:replies) end def test_validates_associated_finds_global_default_translation @@ -697,7 +697,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase Topic.validates_associated :replies replied_topic.valid? - assert_equal 'global message', replied_topic.errors.on(:replies) + assert_equal ['global message'], replied_topic.errors.on(:replies) end def test_validations_with_message_symbol_must_translate @@ -705,7 +705,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_with_message_symbol_must_translate_per_attribute @@ -713,7 +713,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(:title) end def test_validates_with_message_symbol_must_translate_per_model @@ -721,14 +721,14 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase 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.on(: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.on(:title) end end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index cbb1841..0b91e0b 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -74,7 +74,7 @@ class ValidationsTest < ActiveRecord::TestCase r.title = "There's no content!" assert !r.valid? assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid" - assert_equal "Empty", r.errors.on("content"), "A reply without content should contain an error" + assert_equal ["Empty"], r.errors.on("content"), "A reply without content should contain an error" assert_equal 1, r.errors.count end @@ -83,10 +83,10 @@ class ValidationsTest < ActiveRecord::TestCase assert !r.valid? assert r.errors.invalid?("title"), "A reply without title should mark that attribute as invalid" - assert_equal "Empty", r.errors.on("title"), "A reply without title should contain an error" + assert_equal ["Empty"], r.errors.on("title"), "A reply without title should contain an error" assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid" - assert_equal "Empty", r.errors.on("content"), "A reply without content should contain an error" + assert_equal ["Empty"], r.errors.on("content"), "A reply without content should contain an error" assert_equal 2, r.errors.count end @@ -96,7 +96,7 @@ class ValidationsTest < ActiveRecord::TestCase r.title = "Wrong Create" assert !r.valid? assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid" - assert_equal "is Wrong Create", r.errors.on("title"), "A reply with a bad content should contain an error" + assert_equal ["is Wrong Create"], r.errors.on("title"), "A reply with a bad content should contain an error" end def test_error_on_update @@ -109,7 +109,7 @@ class ValidationsTest < ActiveRecord::TestCase assert !r.save, "Second save should fail" assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid" - assert_equal "is Wrong Update", r.errors.on("title"), "A reply with a bad content should contain an error" + assert_equal ["is Wrong Update"], r.errors.on("title"), "A reply with a bad content should contain an error" end def test_invalid_record_exception @@ -205,7 +205,7 @@ class ValidationsTest < ActiveRecord::TestCase errors = [] r.errors.each_full { |error| errors << error } - assert_equal "Reply is not dignifying", r.errors.on_base + assert_equal ["Reply is not dignifying"], r.errors.on_base assert errors.include?("Title Empty") assert errors.include?("Reply is not dignifying") @@ -276,7 +276,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "We should be confirmed","terms_of_service" => "") assert !t.save - assert_equal "must be accepted", t.errors.on(:terms_of_service) + assert_equal ["must be accepted"], t.errors.on(:terms_of_service) t.terms_of_service = "1" assert t.save @@ -288,7 +288,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "We should be confirmed","eula" => "") assert !t.save - assert_equal "must be abided", t.errors.on(:eula) + assert_equal ["must be abided"], t.errors.on(:eula) t.eula = "1" assert t.save @@ -299,7 +299,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "") assert !t.save - assert_equal "must be accepted", t.errors.on(:terms_of_service) + assert_equal ["must be accepted"], t.errors.on(:terms_of_service) t.terms_of_service = "I agree." assert t.save @@ -327,14 +327,14 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create assert !t.save - assert_equal "can't be blank", t.errors.on(:title) - assert_equal "can't be blank", t.errors.on(:content) + assert_equal ["can't be blank"], t.errors.on(:title) + assert_equal ["can't be blank"], t.errors.on(:content) t.title = "something" t.content = " " assert !t.save - assert_equal "can't be blank", t.errors.on(:content) + assert_equal ["can't be blank"], t.errors.on(:content) t.content = "like stuff" @@ -353,7 +353,7 @@ class ValidationsTest < ActiveRecord::TestCase t2 = Topic.new("title" => "I'm unique!") assert !t2.valid?, "Shouldn't be valid" assert !t2.save, "Shouldn't save t2 as unique" - assert_equal "has already been taken", t2.errors.on(:title) + assert_equal ["has already been taken"], t2.errors.on(:title) t2.title = "Now Im really also unique" assert t2.save, "Should now save t2 as unique" @@ -442,7 +442,7 @@ class ValidationsTest < ActiveRecord::TestCase assert !t2.save, "Shouldn't save t2 as unique" assert t2.errors.on(:title) assert t2.errors.on(:parent_id) - assert_equal "has already been taken", t2.errors.on(:title) + assert_equal ["has already been taken"], t2.errors.on(:title) t2.title = "I'm truly UNIQUE!" assert !t2.valid?, "Shouldn't be valid" @@ -538,12 +538,12 @@ class ValidationsTest < ActiveRecord::TestCase w2 = IneptWizard.new(:name => "Rincewind", :city => "Quirm") assert !w2.valid?, "w2 shouldn't be valid" assert w2.errors.on(:name), "Should have errors for name" - assert_equal "has already been taken", w2.errors.on(:name), "Should have uniqueness message for name" + assert_equal ["has already been taken"], w2.errors.on(:name), "Should have uniqueness message for name" w3 = Conjurer.new(:name => "Rincewind", :city => "Quirm") assert !w3.valid?, "w3 shouldn't be valid" assert w3.errors.on(:name), "Should have errors for name" - assert_equal "has already been taken", w3.errors.on(:name), "Should have uniqueness message for name" + assert_equal ["has already been taken"], w3.errors.on(:name), "Should have uniqueness message for name" w4 = Conjurer.create(:name => "The Amazing Bonko", :city => "Quirm") assert w4.valid?, "Saving w4" @@ -551,12 +551,12 @@ class ValidationsTest < ActiveRecord::TestCase w5 = Thaumaturgist.new(:name => "The Amazing Bonko", :city => "Lancre") assert !w5.valid?, "w5 shouldn't be valid" assert w5.errors.on(:name), "Should have errors for name" - assert_equal "has already been taken", w5.errors.on(:name), "Should have uniqueness message for name" + assert_equal ["has already been taken"], w5.errors.on(:name), "Should have uniqueness message for name" w6 = Thaumaturgist.new(:name => "Mustrum Ridcully", :city => "Quirm") assert !w6.valid?, "w6 shouldn't be valid" assert w6.errors.on(:city), "Should have errors for city" - assert_equal "has already been taken", w6.errors.on(:city), "Should have uniqueness message for city" + assert_equal ["has already been taken"], w6.errors.on(:city), "Should have uniqueness message for city" end def test_validate_format @@ -565,7 +565,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!") assert !t.valid?, "Shouldn't be valid" assert !t.save, "Shouldn't save because it's invalid" - assert_equal "is bad data", t.errors.on(:title) + assert_equal ["is bad data"], t.errors.on(:title) assert_nil t.errors.on(:content) t.title = "Validation macros rule!" @@ -591,7 +591,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "72x", "content" => "6789") assert !t.valid?, "Shouldn't be valid" assert !t.save, "Shouldn't save because it's invalid" - assert_equal "is bad data", t.errors.on(:title) + assert_equal ["is bad data"], t.errors.on(:title) assert_nil t.errors.on(:content) t.title = "-11" @@ -615,7 +615,7 @@ class ValidationsTest < ActiveRecord::TestCase 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.on(:title) end def test_validates_inclusion_of @@ -630,7 +630,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "uhoh" assert !t.valid? assert t.errors.on(:title) - assert_equal "is not included in the list", t.errors["title"] + 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) } @@ -683,7 +683,7 @@ class ValidationsTest < ActiveRecord::TestCase 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["title"] + assert_equal ["option uhoh is not in the list"], t.errors["title"] end def test_numericality_with_allow_nil_and_getter_method @@ -710,7 +710,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "monkey") assert !t.valid? assert t.errors.on(:title) - assert_equal "option monkey is restricted", t.errors["title"] + assert_equal ["option monkey is restricted"], t.errors["title"] end def test_validates_length_of_using_minimum @@ -722,17 +722,17 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "not" assert !t.valid? assert t.errors.on(:title) - assert_equal "is too short (minimum is 5 characters)", t.errors["title"] + assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] t.title = "" assert !t.valid? assert t.errors.on(:title) - assert_equal "is too short (minimum is 5 characters)", t.errors["title"] + assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] t.title = nil assert !t.valid? assert t.errors.on(:title) - assert_equal "is too short (minimum is 5 characters)", t.errors["title"] + assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] end def test_optionally_validates_length_of_using_minimum @@ -754,7 +754,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "notvalid" assert !t.valid? assert t.errors.on(:title) - assert_equal "is too long (maximum is 5 characters)", t.errors["title"] + assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"] t.title = "" assert t.valid? @@ -778,14 +778,14 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long") assert !t.valid? - assert_equal "is too short (minimum is 3 characters)", t.errors.on(:title) - assert_equal "is too long (maximum is 5 characters)", t.errors.on(:content) + assert_equal ["is too short (minimum is 3 characters)"], t.errors.on(:title) + assert_equal ["is too long (maximum is 5 characters)"], t.errors.on(:content) t.title = nil t.content = nil assert !t.valid? - assert_equal "is too short (minimum is 3 characters)", t.errors.on(:title) - assert_equal "is too short (minimum is 3 characters)", t.errors.on(:content) + assert_equal ["is too short (minimum is 3 characters)"], t.errors.on(:title) + assert_equal ["is too short (minimum is 3 characters)"], t.errors.on(:content) t.title = "abe" t.content = "mad" @@ -808,7 +808,7 @@ class ValidationsTest < ActiveRecord::TestCase 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] + assert_equal ["my string is too long: 10"], t.errors[:title] t.title = "butthisis" assert t.save @@ -833,7 +833,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "not" assert !t.save assert t.errors.on(:title) - assert_equal "my string is too short: 5", t.errors[:title] + assert_equal ["my string is too short: 5"], t.errors[:title] t.title = "valid" t.content = "andthisistoolong" @@ -853,7 +853,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "notvalid" assert !t.valid? assert t.errors.on(:title) - assert_equal "is the wrong length (should be 5 characters)", t.errors["title"] + assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"] t.title = "" assert !t.valid? @@ -893,7 +893,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create(:title => 'too short') assert !t.valid? - assert_equal 'tu est trops petit hombre 10', t.errors['title'] + assert_equal ['tu est trops petit hombre 10'], t.errors['title'] end def test_validates_size_of_association @@ -939,7 +939,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "boo 5", t.errors["title"] + assert_equal ["boo 5"], t.errors["title"] end def test_validates_length_of_custom_errors_for_minimum_with_too_short @@ -947,7 +947,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_validates_length_of_custom_errors_for_maximum_with_message @@ -955,7 +955,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "boo 5", t.errors["title"] + assert_equal ["boo 5"], t.errors["title"] end def test_validates_length_of_custom_errors_for_in @@ -963,12 +963,12 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 10", t.errors["title"] + assert_equal ["hoo 10"], t.errors["title"] t = Topic.create("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 20", t.errors["title"] + assert_equal ["hoo 20"], t.errors["title"] end def test_validates_length_of_custom_errors_for_maximum_with_too_long @@ -976,7 +976,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_validates_length_of_custom_errors_for_is_with_message @@ -984,7 +984,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "boo 5", t.errors["title"] + assert_equal ["boo 5"], t.errors["title"] end def test_validates_length_of_custom_errors_for_is_with_wrong_length @@ -992,7 +992,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_validates_length_of_using_minimum_utf8 @@ -1005,7 +1005,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "一二三四" assert !t.valid? assert t.errors.on(:title) - assert_equal "is too short (minimum is 5 characters)", t.errors["title"] + assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] end end @@ -1019,7 +1019,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "一二34五六" assert !t.valid? assert t.errors.on(:title) - assert_equal "is too long (maximum is 5 characters)", t.errors["title"] + assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"] end end @@ -1029,8 +1029,8 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.new("title" => "一二", "content" => "12三四五六七") assert !t.valid? - assert_equal "is too short (minimum is 3 characters)", t.errors.on(:title) - assert_equal "is too long (maximum is 5 characters)", t.errors.on(:content) + assert_equal ["is too short (minimum is 3 characters)"], t.errors.on(:title) + assert_equal ["is too long (maximum is 5 characters)"], t.errors.on(:content) t.title = "一二三" t.content = "12三" assert t.valid? @@ -1059,7 +1059,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "一二三四五六七八九十A", "content" => "whatever") assert !t.save assert t.errors.on(:title) - assert_equal "長すぎます: 10", t.errors[:title] + assert_equal ["長すぎます: 10"], t.errors[:title] t.title = "一二三四五六七八九" assert t.save @@ -1086,7 +1086,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "1二三4" assert !t.save assert t.errors.on(:title) - assert_equal "短すぎます: 5", t.errors[:title] + assert_equal ["短すぎます: 5"], t.errors[:title] t.title = "一二三四五六七八九十A" assert !t.save @@ -1107,7 +1107,7 @@ class ValidationsTest < ActiveRecord::TestCase t.title = "一二345六" assert !t.valid? assert t.errors.on(:title) - assert_equal "is the wrong length (should be 5 characters)", t.errors["title"] + assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"] end end @@ -1120,7 +1120,7 @@ class ValidationsTest < ActiveRecord::TestCase t.content = "not long enough" assert !t.valid? assert t.errors.on(:content) - assert_equal "Your essay must be at least 5 words.", t.errors[:content] + assert_equal ["Your essay must be at least 5 words."], t.errors[:content] end def test_validates_size_of_association_utf8 @@ -1168,7 +1168,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "Title", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "will never be valid", t.errors["title"] + assert_equal ["will never be valid"], t.errors["title"] end def test_invalid_validator @@ -1200,7 +1200,7 @@ class ValidationsTest < ActiveRecord::TestCase d.name = "John" d.name_confirmation = "Johnny" assert !d.valid? - assert_equal "confirm 'single' and \"double\" quotes", d.errors.on(:name) + assert_equal ["confirm 'single' and \"double\" quotes"], d.errors.on(:name) end end @@ -1210,7 +1210,7 @@ class ValidationsTest < ActiveRecord::TestCase d = Developer.new d.name = d.name_confirmation = "John 32" assert !d.valid? - assert_equal "format 'single' and \"double\" quotes", d.errors.on(:name) + assert_equal ["format 'single' and \"double\" quotes"], d.errors.on(:name) end end @@ -1230,7 +1230,7 @@ class ValidationsTest < ActiveRecord::TestCase d = Developer.new d.name = "Jeffrey" assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors.on(:name) end end @@ -1240,7 +1240,7 @@ class ValidationsTest < ActiveRecord::TestCase d = Developer.new d.name = "Joe" assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors.on(:name) end end @@ -1250,7 +1250,7 @@ class ValidationsTest < ActiveRecord::TestCase d = Developer.new d.name = "Joe" assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors.on(:name) end end @@ -1260,7 +1260,7 @@ class ValidationsTest < ActiveRecord::TestCase d = Developer.new d.name = "Joe" assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent) + assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors.on(:non_existent) end end @@ -1270,7 +1270,7 @@ class ValidationsTest < ActiveRecord::TestCase d = Developer.new d.name = "David" assert !d.valid? - assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name) + assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors.on(:name) end end @@ -1281,7 +1281,7 @@ class ValidationsTest < ActiveRecord::TestCase r = Reply.create("title" => "A reply", "content" => "with content!") r.topic = Topic.create("title" => "uhohuhoh") assert !r.valid? - assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic) + assert_equal ["This string contains 'single' and \"double\" quotes"], r.errors.on(:topic) end end @@ -1291,7 +1291,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_unless_validation_using_method_true @@ -1316,7 +1316,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_if_validation_using_string_true @@ -1325,7 +1325,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_unless_validation_using_string_true @@ -1350,7 +1350,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_if_validation_using_block_true @@ -1360,7 +1360,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_unless_validation_using_block_true @@ -1388,7 +1388,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.create("title" => "uhohuhoh", "content" => "whatever") assert !t.valid? assert t.errors.on(:title) - assert_equal "hoo 5", t.errors["title"] + assert_equal ["hoo 5"], t.errors["title"] end def test_validates_associated_missing @@ -1504,49 +1504,49 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase def test_validates_numericality_with_greater_than Topic.validates_numericality_of :approved, :greater_than => 10 - invalid!([-10, 10], 'must be greater than 10') + invalid!([-10, 10], ['must be greater than 10']) valid!([11]) end def test_validates_numericality_with_greater_than_or_equal Topic.validates_numericality_of :approved, :greater_than_or_equal_to => 10 - invalid!([-9, 9], 'must be greater than or equal to 10') + invalid!([-9, 9], ['must be greater than or equal to 10']) valid!([10]) end def test_validates_numericality_with_equal_to Topic.validates_numericality_of :approved, :equal_to => 10 - invalid!([-10, 11] + INFINITY, 'must be equal to 10') + invalid!([-10, 11] + INFINITY, ['must be equal to 10']) valid!([10]) end def test_validates_numericality_with_less_than Topic.validates_numericality_of :approved, :less_than => 10 - invalid!([10], 'must be less than 10') + invalid!([10], ['must be less than 10']) valid!([-9, 9]) end def test_validates_numericality_with_less_than_or_equal_to Topic.validates_numericality_of :approved, :less_than_or_equal_to => 10 - invalid!([11], 'must be less than or equal to 10') + invalid!([11], ['must be less than or equal to 10']) valid!([-10, 10]) end def test_validates_numericality_with_odd Topic.validates_numericality_of :approved, :odd => true - invalid!([-2, 2], 'must be odd') + invalid!([-2, 2], ['must be odd']) valid!([-1, 1]) end def test_validates_numericality_with_even Topic.validates_numericality_of :approved, :even => true - invalid!([-1, 1], 'must be even') + invalid!([-1, 1], ['must be even']) valid!([-2, 2]) end @@ -1562,13 +1562,13 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase topic = Topic.new("title" => "numeric test", "approved" => 10) assert !topic.valid? - assert_equal "smaller than 4", topic.errors.on(:approved) + assert_equal ["smaller than 4"], topic.errors.on(:approved) Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than {{count}}" topic = Topic.new("title" => "numeric test", "approved" => 1) assert !topic.valid? - assert_equal "greater than 4", topic.errors.on(:approved) + assert_equal ["greater than 4"], topic.errors.on(:approved) end private -- 1.5.5.3