From 18280267037e287e3d75563eab8eff96cce6cb8b Mon Sep 17 00:00:00 2001 From: pivotal Date: Wed, 7 May 2008 11:38:38 -0700 Subject: [PATCH] nw/ch - ActiveRecord validation uses :attribute instead of assuming attribute in first word --- .../action_view/helpers/active_record_helper.rb | 3 ++- .../test/template/active_record_helper_test.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index f3f204c..2c2904f 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -105,7 +105,8 @@ module ActionView def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError") if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && (errors = obj.errors.on(method)) - content_tag("div", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class) + error = (errors.is_a?(Array) ? errors.first : errors).gsub(":attribute ", "") + content_tag("div", "#{prepend_text}#{error}#{append_text}", :class => css_class) else '' end diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb index dfc30e6..bb3aa09 100644 --- a/actionpack/test/template/active_record_helper_test.rb +++ b/actionpack/test/template/active_record_helper_test.rb @@ -26,7 +26,7 @@ class ActiveRecordHelperTest < ActionView::TestCase def on(field) case field.to_s when "author_name" - "can't be empty" + ":attribute can't be empty" when "body" true else -- 1.5.4.5 From 97b400a1b9484eee9cdf0372221b866088cc67e8 Mon Sep 17 00:00:00 2001 From: pivotal Date: Wed, 7 May 2008 12:03:59 -0700 Subject: [PATCH] nw/ch - the rest of the changes --- activerecord/lib/active_record/validations.rb | 40 ++++---- .../associations/has_one_associations_test.rb | 8 +- activerecord/test/cases/validations_test.rb | 96 ++++++++++---------- activerecord/test/models/reply.rb | 10 +- 4 files changed, 77 insertions(+), 77 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 50db327..49e65ef 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -24,25 +24,25 @@ module ActiveRecord end @@default_error_messages = { - :inclusion => "is not included in the list", - :exclusion => "is reserved", - :invalid => "is invalid", - :confirmation => "doesn't match confirmation", - :accepted => "must be accepted", - :empty => "can't be empty", - :blank => "can't be blank", - :too_long => "is too long (maximum is %d characters)", - :too_short => "is too short (minimum is %d characters)", - :wrong_length => "is the wrong length (should be %d characters)", - :taken => "has already been taken", - :not_a_number => "is not a number", - :greater_than => "must be greater than %d", - :greater_than_or_equal_to => "must be greater than or equal to %d", - :equal_to => "must be equal to %d", - :less_than => "must be less than %d", - :less_than_or_equal_to => "must be less than or equal to %d", - :odd => "must be odd", - :even => "must be even" + :inclusion => ":attribute is not included in the list", + :exclusion => ":attribute is reserved", + :invalid => ":attribute is invalid", + :confirmation => ":attribute doesn't match confirmation", + :accepted => ":attribute must be accepted", + :empty => ":attribute can't be empty", + :blank => ":attribute can't be blank", + :too_long => ":attribute is too long (maximum is %d characters)", + :too_short => ":attribute is too short (minimum is %d characters)", + :wrong_length => ":attribute is the wrong length (should be %d characters)", + :taken => ":attribute has already been taken", + :not_a_number => ":attribute is not a number", + :greater_than => ":attribute must be greater than %d", + :greater_than_or_equal_to => ":attribute must be greater than or equal to %d", + :equal_to => ":attribute must be equal to %d", + :less_than => ":attribute must be less than %d", + :less_than_or_equal_to => ":attribute must be less than or equal to %d", + :odd => ":attribute must be odd", + :even => ":attribute must be even" } # Holds a hash with all the default error messages that can be replaced by your own copy or localizations. @@ -176,7 +176,7 @@ module ActiveRecord if attr == "base" full_messages << msg else - full_messages << @base.class.human_attribute_name(attr) + " " + msg + full_messages << msg.gsub(":attribute", @base.class.human_attribute_name(attr)) end end end diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 9e99caa..5de9e5d 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -143,7 +143,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 ":attribute can't be empty", account.errors.on("credit_limit") end def test_build_association_twice_without_saving_affects_nothing @@ -193,7 +193,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert !account.new_record? end - def test_failing_build_association + def test_failing_build_association_second firm = Firm.new("name" => "GlobalMegaCorp") firm.save @@ -201,7 +201,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 ":attribute can't be empty", account.errors.on("credit_limit") end def test_create @@ -267,7 +267,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 ":attribute is invalid", firm.errors.on("account") end def test_assignment_before_either_saved diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index e3ca866..3424fac 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -77,19 +77,19 @@ 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 ":attribute Empty", r.errors.on("content"), "A reply without content should contain an error" assert_equal 1, r.errors.count end - def test_double_attr_validation_and_error_msg + def test_single_attr_validation_and_error_msg_validation_and_error_msg r = Reply.new 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 ":attribute 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 ":attribute Empty", r.errors.on("content"), "A reply without content should contain an error" assert_equal 2, r.errors.count end @@ -99,7 +99,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 ":attribute is Wrong Create", r.errors.on("title"), "A reply with a bad content should contain an error" end def test_error_on_update @@ -112,7 +112,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 ":attribute is Wrong Update", r.errors.on("title"), "A reply with a bad content should contain an error" end def test_invalid_record_exception @@ -181,8 +181,8 @@ class ValidationsTest < ActiveRecord::TestCase errors = [] r.errors.each { |attr, msg| errors << [attr, msg] } - assert errors.include?(["title", "Empty"]) - assert errors.include?(["content", "Empty"]) + assert errors.include?(["title", ":attribute Empty"]) + assert errors.include?(["content", ":attribute Empty"]) end def test_multiple_errors_per_attr_iteration_with_full_error_composition @@ -239,8 +239,8 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.new("title" => "valid", "content" => "whatever") assert !t.save assert_equal 4, hits - assert_equal %w(gotcha gotcha), t.errors.on(:title) - assert_equal %w(gotcha gotcha), t.errors.on(:content) + assert_equal %w{gotcha gotcha}, t.errors.on(:title) + assert_equal %w{gotcha gotcha}, t.errors.on(:content) ensure perform = false end @@ -284,7 +284,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 ":attribute must be accepted", t.errors.on(:terms_of_service) t.terms_of_service = "1" assert t.save @@ -292,11 +292,11 @@ class ValidationsTest < ActiveRecord::TestCase def test_eula - Topic.validates_acceptance_of(:eula, :message => "must be abided", :on => :create) + Topic.validates_acceptance_of(:eula, :message => "EULA must be abided", :on => :create) t = Topic.create("title" => "We should be confirmed","eula" => "") assert !t.save - assert_equal "must be abided", t.errors.on(:eula) + assert_equal "EULA must be abided", t.errors.on(:eula) t.eula = "1" assert t.save @@ -307,7 +307,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 ":attribute must be accepted", t.errors.on(:terms_of_service) t.terms_of_service = "I agree." assert t.save @@ -331,14 +331,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 ":attribute can't be blank", t.errors.on(:title) + assert_equal ":attribute 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 ":attribute can't be blank", t.errors.on(:content) t.content = "like stuff" @@ -357,7 +357,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 ":attribute has already been taken", t2.errors.on(:title) t2.title = "Now Im really also unique" assert t2.save, "Should now save t2 as unique" @@ -435,7 +435,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 ":attribute has already been taken", t2.errors.on(:title) t2.title = "I'm truly UNIQUE!" assert !t2.valid?, "Shouldn't be valid" @@ -466,14 +466,14 @@ class ValidationsTest < ActiveRecord::TestCase assert t2.save, "Should save t2 as unique" assert !t2.errors.on(:title) assert !t2.errors.on(:parent_id) - assert_not_equal "has already been taken", t2.errors.on(:title) + assert_not_equal ":attribute has already been taken", t2.errors.on(:title) t3 = Topic.new("title" => "I'M uNiQUe!") assert t3.valid?, "Should be valid" assert t3.save, "Should save t2 as unique" assert !t3.errors.on(:title) assert !t3.errors.on(:parent_id) - assert_not_equal "has already been taken", t3.errors.on(:title) + assert_not_equal ":attribute has already been taken", t3.errors.on(:title) end def test_validate_uniqueness_with_non_standard_table_names @@ -501,12 +501,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 ":attribute 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 ":attribute 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" @@ -514,12 +514,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 ":attribute 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 ":attribute has already been taken", w6.errors.on(:city), "Should have uniqueness message for city" end def test_validate_format @@ -587,7 +587,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 ":attribute 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) } @@ -675,17 +675,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 ":attribute 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 ":attribute 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 ":attribute is too short (minimum is 5 characters)", t.errors["title"] end def test_optionally_validates_length_of_using_minimum @@ -707,7 +707,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 ":attribute is too long (maximum is 5 characters)", t.errors["title"] t.title = "" assert t.valid? @@ -731,14 +731,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 ":attribute is too short (minimum is 3 characters)", t.errors.on(:title) + assert_equal ":attribute 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 ":attribute is too short (minimum is 3 characters)", t.errors.on(:title) + assert_equal ":attribute is too short (minimum is 3 characters)", t.errors.on(:content) t.title = "abe" t.content = "mad" @@ -806,7 +806,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 ":attribute is the wrong length (should be 5 characters)", t.errors["title"] t.title = "" assert !t.valid? @@ -939,7 +939,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 ":attribute is too short (minimum is 5 characters)", t.errors["title"] end end @@ -953,7 +953,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 ":attribute is too long (maximum is 5 characters)", t.errors["title"] end end @@ -963,8 +963,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 ":attribute is too short (minimum is 3 characters)", t.errors.on(:title) + assert_equal ":attribute is too long (maximum is 5 characters)", t.errors.on(:content) t.title = "一二三" t.content = "12三" assert t.valid? @@ -1041,7 +1041,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 ":attribute is the wrong length (should be 5 characters)", t.errors["title"] end end @@ -1314,7 +1314,7 @@ class ValidationsTest < ActiveRecord::TestCase t = Topic.new("title" => "") assert !t.valid? - assert_equal "can't be blank", t.errors.on("title").first + assert_equal ":attribute can't be blank", t.errors.on("title").first end # previous implementation of validates_presence_of eval'd the @@ -1403,49 +1403,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], ':attribute 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], ':attribute 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], 'must be equal to 10') + invalid!([-10, 11], ':attribute 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], ':attribute 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], ':attribute 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], ':attribute 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], ':attribute must be even') valid!([-2, 2]) end diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index 812bc1f..8151251 100755 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -12,25 +12,25 @@ class Reply < Topic attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read def validate - errors.add("title", "Empty") unless attribute_present? "title" + errors.add("title", ":attribute Empty") unless attribute_present? "title" end def errors_on_empty_content - errors.add("content", "Empty") unless attribute_present? "content" + errors.add("content", ":attribute Empty") unless attribute_present? "content" end def validate_on_create if attribute_present?("title") && attribute_present?("content") && content == "Mismatch" - errors.add("title", "is Content Mismatch") + errors.add("title", ":attribute is Content Mismatch") end end def title_is_wrong_create - errors.add("title", "is Wrong Create") if attribute_present?("title") && title == "Wrong Create" + errors.add("title", ":attribute is Wrong Create") if attribute_present?("title") && title == "Wrong Create" end def validate_on_update - errors.add("title", "is Wrong Update") if attribute_present?("title") && title == "Wrong Update" + errors.add("title", ":attribute is Wrong Update") if attribute_present?("title") && title == "Wrong Update" end end -- 1.5.4.5