From 0a8e6522cf73f3781fb642bbd996eb3ceef1de7d Mon Sep 17 00:00:00 2001 From: Aaron Gibralter Date: Fri, 17 Feb 2012 15:36:17 -0500 Subject: [PATCH] Use all possible i18n error messages for ensure_length_of_matcher. --- .../active_model/ensure_length_of_matcher.rb | 4 ++ spec/shoulda/active_model/helpers_spec.rb | 41 ++++++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb b/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb index 86b5848d..3791e022 100644 --- a/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb @@ -96,11 +96,15 @@ module Shoulda # :nodoc: def translate_messages! if Symbol === @short_message @short_message = default_error_message(@short_message, + :model_name => @subject.class.to_s.underscore, + :attribute => @attribute, :count => @minimum) end if Symbol === @long_message @long_message = default_error_message(@long_message, + :model_name => @subject.class.to_s.underscore, + :attribute => @attribute, :count => @maximum) end end diff --git a/spec/shoulda/active_model/helpers_spec.rb b/spec/shoulda/active_model/helpers_spec.rb index 394bdbbd..70c3753c 100644 --- a/spec/shoulda/active_model/helpers_spec.rb +++ b/spec/shoulda/active_model/helpers_spec.rb @@ -26,19 +26,23 @@ def store_translations(options = {:without => []}) } unless options[:without].include?(:model_attribute) - translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = 'Don’t you do that to me!' + translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = "Don't you do that to me!" + translations[:activerecord][:errors][:models][:example][:attributes][:attr][:wrong_length] = "Don't you do that to me!" end unless options[:without].include?(:model) translations[:activerecord][:errors][:models][:example][:blank] = 'Give it one more try!' + translations[:activerecord][:errors][:models][:example][:wrong_length] = 'Give it one more try!' end unless options[:without].include?(:message) translations[:activerecord][:errors][:messages][:blank] = 'Oh no!' + translations[:activerecord][:errors][:messages][:wrong_length] = 'Oh no!' end unless options[:without].include?(:attribute) translations[:errors][:attributes][:attr][:blank] = 'Seriously?' + translations[:errors][:attributes][:attr][:wrong_length] = 'Seriously?' end I18n.backend.store_translations(:en, translations) @@ -51,6 +55,7 @@ describe Shoulda::Matchers::ActiveModel::Helpers do before do define_model :example, :attr => :string do validates_presence_of :attr + validates_length_of :attr, :is => 40, :allow_blank => true end @model = Example.new end @@ -58,40 +63,64 @@ describe Shoulda::Matchers::ActiveModel::Helpers do after { I18n.backend.reload! } context "if the translation for the model attribute’s error exists" do - it "provides the right error message" do + it "provides the right error message for validate_presence_of" do store_translations @model.should validate_presence_of(:attr) end + + it "provides the right error message for validates_length_of" do + store_translations + @model.should ensure_length_of(:attr).is_equal_to(40) + end end context "if no translation for the model attribute’s error exists" do context "and the translation for the model’s error exists" do - it "provides the right error message" do + it "provides the right error message for validate_presence_of" do store_translations(:without => :model_attribute) @model.should validate_presence_of(:attr) end + + it "provides the right error message for validates_length_of" do + store_translations(:without => :model_attribute) + @model.should ensure_length_of(:attr).is_equal_to(40) + end end context "and no translation for the model’s error exists" do context "and the translation for the message exists" do - it "provides the right error message" do + it "provides the right error message for validate_presence_of" do store_translations(:without => [:model_attribute, :model]) @model.should validate_presence_of(:attr) end + + it "provides the right error message for validates_length_of" do + store_translations(:without => [:model_attribute, :model]) + @model.should ensure_length_of(:attr).is_equal_to(40) + end end context "and no translation for the message exists" do context "and the translation for the attribute exists" do - it "provides the right error message" do + it "provides the right error message for validate_presence_of" do store_translations(:without => [:model_attribute, :model, :message]) @model.should validate_presence_of(:attr) end + + it "provides the right error message for validates_length_of" do + store_translations(:without => [:model_attribute, :model, :message]) + @model.should ensure_length_of(:attr).is_equal_to(40) + end end context "and no translation for the attribute exists" do - it "provides the general error message" do + it "provides the general error message for validate_presence_of" do @model.should validate_presence_of(:attr) end + + it "provides the general error message for validates_length_of" do + @model.should ensure_length_of(:attr).is_equal_to(40) + end end end end