rmm5t-shoulda 2.0.2 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -43,7 +43,7 @@ spec = Gem::Specification.new do |s|
43
43
  s.homepage = "http://thoughtbot.com/projects/shoulda"
44
44
  s.rubyforge_project = "shoulda"
45
45
 
46
- s.files = FileList["[A-Z]*", "{bin,lib,test}/**/*"]
46
+ s.files = FileList["[A-Z]*", "{bin,lib,rails,test}/**/*"]
47
47
  s.executables = s.files.grep(/^bin/) { |f| File.basename(f) }
48
48
 
49
49
  s.has_rdoc = true
@@ -53,7 +53,7 @@ spec = Gem::Specification.new do |s|
53
53
  s.authors = ["Tammer Saleh"]
54
54
  s.email = "tsaleh@thoughtbot.com"
55
55
 
56
- s.add_dependency "activesupport", ">= 2.0"
56
+ s.add_dependency "activesupport", ">= 2.0.0"
57
57
  end
58
58
 
59
59
  Rake::GemPackageTask.new spec do |pkg|
@@ -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}"
@@ -66,7 +66,10 @@ module ThoughtBot # :nodoc:
66
66
  end
67
67
 
68
68
  def pretty_error_messages(obj)
69
- obj.errors.map { |a, m| "#{a} #{m} (#{obj.send(a).inspect})" }
69
+ obj.errors.map do |a, m|
70
+ msg = "#{a} #{m}"
71
+ msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
72
+ end
70
73
  end
71
74
 
72
75
  private
@@ -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
@@ -25,8 +25,10 @@ module ThoughtBot # :nodoc:
25
25
  collection = [collection] unless collection.is_a?(Array)
26
26
  msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
27
27
  case x
28
- when Regexp: assert(collection.detect { |e| e =~ x }, msg)
29
- else assert(collection.include?(x), msg)
28
+ when Regexp
29
+ assert(collection.detect { |e| e =~ x }, msg)
30
+ else
31
+ assert(collection.include?(x), msg)
30
32
  end
31
33
  end
32
34
 
@@ -36,8 +38,10 @@ module ThoughtBot # :nodoc:
36
38
  collection = [collection] unless collection.is_a?(Array)
37
39
  msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
38
40
  case x
39
- when Regexp: assert(!collection.detect { |e| e =~ x }, msg)
40
- else assert(!collection.include?(x), msg)
41
+ when Regexp
42
+ assert(!collection.detect { |e| e =~ x }, msg)
43
+ else
44
+ assert(!collection.include?(x), msg)
41
45
  end
42
46
  end
43
47
  end
@@ -1,6 +1,6 @@
1
1
  module Thoughtbot # :nodoc:
2
2
  module Shoulda
3
- VERSION = '2.0.2'
3
+ VERSION = '2.0.5'
4
4
 
5
5
  class << self
6
6
  attr_accessor :contexts
@@ -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
@@ -26,8 +26,8 @@ module ThoughtBot # :nodoc:
26
26
  # Combinations of <tt>:by</tt>, <tt>:from</tt>, and <tt>:to</tt> are allowed:
27
27
  #
28
28
  # should_change "@post.title" # => assert the value changed in some way
29
- # should_change "@post.title" :from => "old" # => assert the value changed to anything other than "old"
30
- # should_change "@post.title" :to => "new" # => assert the value changed from anything other than "new"
29
+ # should_change "@post.title", :from => "old" # => assert the value changed to anything other than "old"
30
+ # should_change "@post.title", :to => "new" # => assert the value changed from anything other than "new"
31
31
  def should_change(expression, options = {})
32
32
  by, from, to = get_options!([options], :by, :from, :to)
33
33
  stmt = "change #{expression.inspect}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmm5t-shoulda
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tammer Saleh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-20 00:00:00 -07:00
12
+ date: 2008-10-15 00:00:00 -07:00
13
13
  default_executable: convert_to_should_syntax
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: "2.0"
22
+ version: 2.0.0
23
23
  version:
24
24
  description:
25
25
  email: tsaleh@thoughtbot.com
@@ -64,7 +64,6 @@ files:
64
64
  - lib/shoulda/tasks/yaml_to_shoulda.rake
65
65
  - lib/shoulda/tasks.rb
66
66
  - lib/shoulda.rb
67
- - rails
68
67
  - rails/init.rb
69
68
  - test/fail_macros.rb
70
69
  - test/fixtures