Skip to content
This repository
Browse code

Use all possible i18n error messages for ensure_length_of_matcher.

  • Loading branch information...
commit 0a8e6522cf73f3781fb642bbd996eb3ceef1de7d 1 parent 63b679c
Aaron Gibralter authored February 17, 2012 gabebw committed April 20, 2012
4  lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb
@@ -96,11 +96,15 @@ def matches?(subject)
96 96
         def translate_messages!
97 97
           if Symbol === @short_message
98 98
             @short_message = default_error_message(@short_message,
  99
+                                                   :model_name => @subject.class.to_s.underscore,
  100
+                                                   :attribute => @attribute,
99 101
                                                    :count => @minimum)
100 102
           end
101 103
 
102 104
           if Symbol === @long_message
103 105
             @long_message = default_error_message(@long_message,
  106
+                                                  :model_name => @subject.class.to_s.underscore,
  107
+                                                  :attribute => @attribute,
104 108
                                                   :count => @maximum)
105 109
           end
106 110
         end
41  spec/shoulda/active_model/helpers_spec.rb
@@ -26,19 +26,23 @@ def store_translations(options = {:without => []})
26 26
   }
27 27
 
28 28
   unless options[:without].include?(:model_attribute)
29  
-    translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = 'Don’t you do that to me!'
  29
+    translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = "Don't you do that to me!"
  30
+    translations[:activerecord][:errors][:models][:example][:attributes][:attr][:wrong_length] = "Don't you do that to me!"
30 31
   end
31 32
 
32 33
   unless options[:without].include?(:model)
33 34
     translations[:activerecord][:errors][:models][:example][:blank] = 'Give it one more try!'
  35
+    translations[:activerecord][:errors][:models][:example][:wrong_length] = 'Give it one more try!'
34 36
   end
35 37
 
36 38
   unless options[:without].include?(:message)
37 39
     translations[:activerecord][:errors][:messages][:blank] = 'Oh no!'
  40
+    translations[:activerecord][:errors][:messages][:wrong_length] = 'Oh no!'
38 41
   end
39 42
 
40 43
   unless options[:without].include?(:attribute)
41 44
     translations[:errors][:attributes][:attr][:blank] = 'Seriously?'
  45
+    translations[:errors][:attributes][:attr][:wrong_length] = 'Seriously?'
42 46
   end
43 47
 
44 48
   I18n.backend.store_translations(:en, translations)
@@ -51,6 +55,7 @@ def store_translations(options = {:without => []})
51 55
     before do
52 56
       define_model :example, :attr => :string do
53 57
         validates_presence_of :attr
  58
+        validates_length_of :attr, :is => 40, :allow_blank => true
54 59
       end
55 60
       @model = Example.new
56 61
     end
@@ -58,40 +63,64 @@ def store_translations(options = {:without => []})
58 63
     after { I18n.backend.reload! }
59 64
 
60 65
     context "if the translation for the model attribute’s error exists" do
61  
-      it "provides the right error message" do
  66
+      it "provides the right error message for validate_presence_of" do
62 67
         store_translations
63 68
         @model.should validate_presence_of(:attr)
64 69
       end
  70
+
  71
+      it "provides the right error message for validates_length_of" do
  72
+        store_translations
  73
+        @model.should ensure_length_of(:attr).is_equal_to(40)
  74
+      end
65 75
     end
66 76
 
67 77
     context "if no translation for the model attribute’s error exists" do
68 78
       context "and the translation for the model’s error exists" do
69  
-        it "provides the right error message" do
  79
+        it "provides the right error message for validate_presence_of" do
70 80
           store_translations(:without => :model_attribute)
71 81
           @model.should validate_presence_of(:attr)
72 82
         end
  83
+
  84
+        it "provides the right error message for validates_length_of" do
  85
+          store_translations(:without => :model_attribute)
  86
+          @model.should ensure_length_of(:attr).is_equal_to(40)
  87
+        end
73 88
       end
74 89
 
75 90
       context "and no translation for the model’s error exists" do
76 91
         context "and the translation for the message exists" do
77  
-          it "provides the right error message" do
  92
+          it "provides the right error message for validate_presence_of" do
78 93
             store_translations(:without => [:model_attribute, :model])
79 94
             @model.should validate_presence_of(:attr)
80 95
           end
  96
+
  97
+          it "provides the right error message for validates_length_of" do
  98
+            store_translations(:without => [:model_attribute, :model])
  99
+            @model.should ensure_length_of(:attr).is_equal_to(40)
  100
+          end
81 101
         end
82 102
 
83 103
         context "and no translation for the message exists" do
84 104
           context "and the translation for the attribute exists" do
85  
-            it "provides the right error message" do
  105
+            it "provides the right error message for validate_presence_of" do
86 106
               store_translations(:without => [:model_attribute, :model, :message])
87 107
               @model.should validate_presence_of(:attr)
88 108
             end
  109
+
  110
+            it "provides the right error message for validates_length_of" do
  111
+              store_translations(:without => [:model_attribute, :model, :message])
  112
+              @model.should ensure_length_of(:attr).is_equal_to(40)
  113
+            end
89 114
           end
90 115
 
91 116
           context "and no translation for the attribute exists" do
92  
-            it "provides the general error message" do
  117
+            it "provides the general error message for validate_presence_of" do
93 118
               @model.should validate_presence_of(:attr)
94 119
             end
  120
+
  121
+            it "provides the general error message for validates_length_of" do
  122
+              @model.should ensure_length_of(:attr).is_equal_to(40)
  123
+            end
95 124
           end
96 125
         end
97 126
       end

0 notes on commit 0a8e652

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