shoulda 2.0.2 → 2.0.3

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