public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
Fix for recent I18n changes in edge Rails [#87] [DrMark, Carlos Antonio da 
Silva]
rmm5t (author)
Mon Oct 06 11:05:19 -0700 2008
commit  4c9e7ce2c3aba205cd1ebb9882814d810d72c4e8
tree    7d7ad5f3144adf5c214fd63b9261ea4b6456d1e7
parent  cfc75ea40a9837a0f264d6276b8240ceff99ced2
...
57
58
59
60
 
61
62
63
...
57
58
59
 
60
61
62
63
0
@@ -57,7 +57,7 @@ module ThoughtBot # :nodoc:
0
         # @product = Product.new(:tangible => true)
0
         # assert_bad_value(Product, :price, "0")
0
         def assert_bad_value(object_or_klass, attribute, value,
0
- error_message_to_expect = DEFAULT_ERROR_MESSAGES[:invalid])
0
+ error_message_to_expect = self.class.default_error_message(:invalid))
0
           object = get_instance_of(object_or_klass)
0
           object.send("#{attribute}=", value)
0
           assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
...
1
2
3
4
5
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
11
12
...
28
29
30
 
 
31
32
33
...
44
45
46
47
 
48
49
50
51
52
53
54
 
55
56
57
...
66
67
68
69
 
70
71
72
...
78
79
80
81
 
82
83
84
...
164
165
166
167
 
168
169
170
171
172
173
174
 
175
176
177
...
207
208
209
210
 
211
212
 
213
214
215
216
217
218
219
220
 
 
221
222
223
...
259
260
261
262
 
263
264
265
266
267
268
269
 
270
271
272
...
290
291
292
293
 
294
295
296
297
298
299
300
 
301
302
303
...
325
326
327
328
 
329
330
 
331
332
333
334
335
336
337
338
 
 
339
340
341
...
370
371
372
373
 
374
375
376
377
378
379
380
 
381
382
383
...
624
625
626
627
 
628
629
630
631
632
633
634
 
635
636
637
...
697
698
699
700
701
702
703
...
1
2
3
 
 
 
 
 
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
38
39
40
41
42
43
44
45
...
56
57
58
 
59
60
61
62
63
64
65
 
66
67
68
69
...
78
79
80
 
81
82
83
84
...
90
91
92
 
93
94
95
96
...
176
177
178
 
179
180
181
182
183
184
185
 
186
187
188
189
...
219
220
221
 
222
223
 
224
225
226
227
228
229
230
 
 
231
232
233
234
235
...
271
272
273
 
274
275
276
277
278
279
280
 
281
282
283
284
...
302
303
304
 
305
306
307
308
309
310
311
 
312
313
314
315
...
337
338
339
 
340
341
 
342
343
344
345
346
347
348
 
 
349
350
351
352
353
...
382
383
384
 
385
386
387
388
389
390
391
 
392
393
394
395
...
636
637
638
 
639
640
641
642
643
644
645
 
646
647
648
649
...
709
710
711
 
712
713
714
0
@@ -1,12 +1,22 @@
0
 module ThoughtBot # :nodoc:
0
   module Shoulda # :nodoc:
0
     module ActiveRecord # :nodoc:
0
- DEFAULT_ERROR_MESSAGES =
0
- if Object.const_defined?(:I18n)
0
- I18n.translate('activerecord.errors.messages')
0
- else
0
- ::ActiveRecord::Errors.default_error_messages
0
+ module MacroHelpers # :nodoc:
0
+ # Helper method that determines the default error message used by Active
0
+ # Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
0
+ # introduced I18n module used for localization.
0
+ #
0
+ # default_error_message(:blank)
0
+ # default_error_message(:too_short, :count => 5)
0
+ # default_error_message(:too_long, :count => 60)
0
+ def default_error_message(key, values = {})
0
+ if Object.const_defined?(:I18n) # Rails >= 2.2
0
+ I18n.translate("activerecord.errors.messages.#{key}", values)
0
+ else # Rails <= 2.1.x
0
+ ::ActiveRecord::Errors.default_error_messages[key] % values[:count]
0
+ end
0
         end
0
+ end
0
 
0
       # = Macro test helpers for your active record models
0
       #
0
@@ -28,6 +38,8 @@ module ThoughtBot # :nodoc:
0
       # For all of these helpers, the last parameter may be a hash of options.
0
       #
0
       module Macros
0
+ include MacroHelpers
0
+
0
         # <b>DEPRECATED:</b> Use <tt>fixtures :all</tt> instead
0
         #
0
         # Loads all fixture files (<tt>test/fixtures/*.yml</tt>)
0
@@ -44,14 +56,14 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:blank]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.blank')</tt>
0
         #
0
         # Example:
0
         # should_require_attributes :name, :phone_number
0
         #
0
         def should_require_attributes(*attributes)
0
           message = get_options!(attributes, :message)
0
- message ||= DEFAULT_ERROR_MESSAGES[:blank]
0
+ message ||= default_error_message(:blank)
0
           klass = model_class
0
 
0
           attributes.each do |attribute|
0
@@ -66,7 +78,7 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:taken]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.taken')</tt>
0
         # * <tt>:scoped_to</tt> - field(s) to scope the uniqueness to.
0
         #
0
         # Examples:
0
@@ -78,7 +90,7 @@ module ThoughtBot # :nodoc:
0
         def should_require_unique_attributes(*attributes)
0
           message, scope = get_options!(attributes, :message, :scoped_to)
0
           scope = [*scope].compact
0
- message ||= DEFAULT_ERROR_MESSAGES[:taken]
0
+ message ||= default_error_message(:taken)
0
 
0
           klass = model_class
0
           attributes.each do |attribute|
0
@@ -164,14 +176,14 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:invalid]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.invalid')</tt>
0
         #
0
         # Example:
0
         # should_not_allow_values_for :isbn, "bad 1", "bad 2"
0
         #
0
         def should_not_allow_values_for(attribute, *bad_values)
0
           message = get_options!(bad_values, :message)
0
- message ||= DEFAULT_ERROR_MESSAGES[:invalid]
0
+ message ||= default_error_message(:invalid)
0
           klass = model_class
0
           bad_values.each do |v|
0
             should "not allow #{attribute} to be set to #{v.inspect}" do
0
@@ -207,17 +219,17 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:too_short] % range.first</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % range.first</tt>
0
         # * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:too_long] % range.last</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_long') % range.last</tt>
0
         #
0
         # Example:
0
         # should_ensure_length_in_range :password, (6..20)
0
         #
0
         def should_ensure_length_in_range(attribute, range, opts = {})
0
           short_message, long_message = get_options!([opts], :short_message, :long_message)
0
- short_message ||= DEFAULT_ERROR_MESSAGES[:too_short] % range.first
0
- long_message ||= DEFAULT_ERROR_MESSAGES[:too_long] % range.last
0
+ short_message ||= default_error_message(:too_short, :count => range.first)
0
+ long_message ||= default_error_message(:too_long, :count => range.last)
0
 
0
           klass = model_class
0
           min_length = range.first
0
@@ -259,14 +271,14 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:too_short] % min_length</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % min_length</tt>
0
         #
0
         # Example:
0
         # should_ensure_length_at_least :name, 3
0
         #
0
         def should_ensure_length_at_least(attribute, min_length, opts = {})
0
           short_message = get_options!([opts], :short_message)
0
- short_message ||= DEFAULT_ERROR_MESSAGES[:too_short] % min_length
0
+ short_message ||= default_error_message(:too_short, :count => min_length)
0
 
0
           klass = model_class
0
 
0
@@ -290,14 +302,14 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:wrong_length] % length</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % length</tt>
0
         #
0
         # Example:
0
         # should_ensure_length_is :ssn, 9
0
         #
0
         def should_ensure_length_is(attribute, length, opts = {})
0
           message = get_options!([opts], :message)
0
- message ||= DEFAULT_ERROR_MESSAGES[:wrong_length] % length
0
+ message ||= default_error_message(:wrong_length, :count => length)
0
 
0
           klass = model_class
0
 
0
@@ -325,17 +337,17 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:inclusion]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
0
         # * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:inclusion]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
0
         #
0
         # Example:
0
         # should_ensure_value_in_range :age, (0..100)
0
         #
0
         def should_ensure_value_in_range(attribute, range, opts = {})
0
           low_message, high_message = get_options!([opts], :low_message, :high_message)
0
- low_message ||= DEFAULT_ERROR_MESSAGES[:inclusion]
0
- high_message ||= DEFAULT_ERROR_MESSAGES[:inclusion]
0
+ low_message ||= default_error_message(:inclusion)
0
+ high_message ||= default_error_message(:inclusion)
0
 
0
           klass = model_class
0
           min = range.first
0
@@ -370,14 +382,14 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:not_a_number]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.not_a_number')</tt>
0
         #
0
         # Example:
0
         # should_only_allow_numeric_values_for :age
0
         #
0
         def should_only_allow_numeric_values_for(*attributes)
0
           message = get_options!(attributes, :message)
0
- message ||= DEFAULT_ERROR_MESSAGES[:not_a_number]
0
+ message ||= default_error_message(:not_a_number)
0
           klass = model_class
0
           attributes.each do |attribute|
0
             attribute = attribute.to_sym
0
@@ -624,14 +636,14 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:accepted]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.accepted')</tt>
0
         #
0
         # Example:
0
         # should_require_acceptance_of :eula
0
         #
0
         def should_require_acceptance_of(*attributes)
0
           message = get_options!(attributes, :message)
0
- message ||= DEFAULT_ERROR_MESSAGES[:accepted]
0
+ message ||= default_error_message(:accepted)
0
           klass = model_class
0
 
0
           attributes.each do |attribute|
0
@@ -697,7 +709,6 @@ module ThoughtBot # :nodoc:
0
             end
0
           end
0
         end
0
-
0
       end
0
     end
0
   end

Comments

    No one has commented yet.