Skip to content
This repository
Browse code

incorporate #translate usage with several default keys

(use first default key that resolves to a translation). this might, depending
on the backend implementation save some expensive lookups (like db lookups)
  • Loading branch information...
commit 8bfdabbd8b5137f91d8bcddc8c3d18961c8e316b 1 parent 428aa24
Sven Fuchs svenfuchs authored
19 activerecord/lib/active_record/validations.rb
@@ -66,9 +66,11 @@ def add_on_blank(attributes, custom_message = nil)
66 66 end
67 67
68 68 def generate_message(attr, key, options = {})
69   - scope = [:active_record, :error_messages]
70   - key.t(options.merge(:scope => scope + [:custom, @base.class.name.downcase, attr])) ||
71   - key.t(options.merge(:scope => scope))
  69 + msgs = base_classes(@base.class).map{|klass| :"custom.#{klass.name.underscore}.#{attr}.#{key}"}
  70 + msgs << options[:default] if options[:default]
  71 + msgs << key
  72 +
  73 + I18n.t options.merge(:default => msgs, :scope => [:active_record, :error_messages])
72 74 end
73 75
74 76 # Returns true if the specified +attribute+ has errors associated with it.
@@ -217,6 +219,17 @@ def to_xml(options={})
217 219 full_messages.each { |msg| e.error(msg) }
218 220 end
219 221 end
  222 +
  223 + protected
  224 +
  225 + # TODO maybe this should be on ActiveRecord::Base, maybe #self_and_descendents_from_active_record
  226 + def base_classes(klass)
  227 + classes = [klass]
  228 + while klass != klass.base_class
  229 + classes << klass = klass.superclass
  230 + end
  231 + classes
  232 + end
220 233 end
221 234
222 235
86 activerecord/test/cases/validations_i18n_test.rb
@@ -40,31 +40,15 @@ def test_errors_generate_message_translates_custom_model_attribute_key
40 40 global_scope = [:active_record, :error_messages]
41 41 custom_scope = global_scope + [:custom, 'topic', :title]
42 42
43   - I18n.expects(:translate).with(:invalid, :scope => custom_scope).returns 'translation'
44   - I18n.expects(:translate).with(:invalid, :scope => global_scope).never
45   -
46   - @topic.errors.generate_message :title, :invalid
  43 + I18n.expects(:t).with :scope => [:active_record, :error_messages], :default => [:"custom.topic.title.invalid", 'default from class def', :invalid]
  44 + @topic.errors.generate_message :title, :invalid, :default => 'default from class def'
47 45 end
48 46
49   - def test_errors_generate_message_given_a_custom_message_translates_custom_model_attribute_key_with_custom_message_as_default
  47 + def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
50 48 custom_scope = [:active_record, :error_messages, :custom, 'topic', :title]
51 49
52   - I18n.expects(:translate).with(:invalid, :scope => custom_scope, :default => 'default from class def').returns 'translation'
53   - @topic.errors.generate_message :title, :invalid, :default => 'default from class def'
54   - end
55   -
56   - def test_errors_generate_message_given_no_custom_message_falls_back_to_global_default_key_translation
57   - global_scope = [:active_record, :error_messages]
58   - custom_scope = global_scope + [:custom, 'topic', :title]
59   -
60   - I18n.stubs(:translate).with(:invalid, :scope => custom_scope).returns nil
61   - I18n.expects(:translate).with(:invalid, :scope => global_scope)
62   - @topic.errors.generate_message :title, :invalid
63   - end
64   -
65   - def test_errors_add_given_no_message_it_translates_invalid
66   - I18n.expects(:translate).with(:"active_record.error_messages.invalid")
67   - @topic.errors.add :title
  50 + I18n.expects(:t).with :scope => [:active_record, :error_messages], :default => [:"custom.reply.title.invalid", :"custom.topic.title.invalid", 'default from class def', :invalid]
  51 + Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
68 52 end
69 53
70 54 def test_errors_add_on_empty_generates_message
@@ -115,7 +99,7 @@ def test_validates_confirmation_of_generates_message_with_custom_default_message
115 99 def test_validates_confirmation_of_finds_custom_model_key_translation
116 100 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:confirmation => 'custom message'}}}}}
117 101 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:confirmation => 'global message'}}
118   -
  102 +
119 103 Topic.validates_confirmation_of :title
120 104 @topic.title_confirmation = 'foo'
121 105 @topic.valid?
@@ -124,7 +108,7 @@ def test_validates_confirmation_of_finds_custom_model_key_translation
124 108
125 109 def test_validates_confirmation_of_finds_global_default_translation
126 110 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:confirmation => 'global message'}}
127   -
  111 +
128 112 Topic.validates_confirmation_of :title
129 113 @topic.title_confirmation = 'foo'
130 114 @topic.valid?
@@ -133,13 +117,13 @@ def test_validates_confirmation_of_finds_global_default_translation
133 117
134 118
135 119 # validates_acceptance_of
136   -
  120 +
137 121 def test_validates_acceptance_of_generates_message
138 122 Topic.validates_acceptance_of :title, :allow_nil => false
139 123 @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => nil})
140 124 @topic.valid?
141 125 end
142   -
  126 +
143 127 def test_validates_acceptance_of_generates_message_with_custom_default_message
144 128 Topic.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
145 129 @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
@@ -149,7 +133,7 @@ def test_validates_acceptance_of_generates_message_with_custom_default_message
149 133 def test_validates_acceptance_of_finds_custom_model_key_translation
150 134 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:accepted => 'custom message'}}}}}
151 135 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:accepted => 'global message'}}
152   -
  136 +
153 137 Topic.validates_acceptance_of :title, :allow_nil => false
154 138 @topic.valid?
155 139 assert_equal 'custom message', @topic.errors.on(:title)
@@ -157,7 +141,7 @@ def test_validates_acceptance_of_finds_custom_model_key_translation
157 141
158 142 def test_validates_acceptance_of_finds_global_default_translation
159 143 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:accepted => 'global message'}}
160   -
  144 +
161 145 Topic.validates_acceptance_of :title, :allow_nil => false
162 146 @topic.valid?
163 147 assert_equal 'global message', @topic.errors.on(:title)
@@ -165,7 +149,7 @@ def test_validates_acceptance_of_finds_global_default_translation
165 149
166 150
167 151 # validates_presence_of
168   -
  152 +
169 153 def test_validates_presence_of_generates_message
170 154 Topic.validates_presence_of :title
171 155 @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
@@ -181,7 +165,7 @@ def test_validates_presence_of_generates_message_with_custom_default_message
181 165 def test_validates_presence_of_finds_custom_model_key_translation
182 166 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:blank => 'custom message'}}}}}
183 167 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:blank => 'global message'}}
184   -
  168 +
185 169 Topic.validates_presence_of :title
186 170 @topic.valid?
187 171 assert_equal 'custom message', @topic.errors.on(:title)
@@ -189,7 +173,7 @@ def test_validates_presence_of_finds_custom_model_key_translation
189 173
190 174 def test_validates_presence_of_finds_global_default_translation
191 175 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:blank => 'global message'}}
192   -
  176 +
193 177 Topic.validates_presence_of :title
194 178 @topic.valid?
195 179 assert_equal 'global message', @topic.errors.on(:title)
@@ -197,7 +181,7 @@ def test_validates_presence_of_finds_global_default_translation
197 181
198 182
199 183 # validates_length_of :within
200   -
  184 +
201 185 def test_validates_length_of_within_generates_message
202 186 Topic.validates_length_of :title, :within => 3..5
203 187 @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
@@ -213,7 +197,7 @@ def test_validates_length_of_within_generates_message_with_custom_default_messag
213 197 def test_validates_length_of_within_finds_custom_model_key_translation
214 198 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:too_short => 'custom message'}}}}}
215 199 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:too_short => 'global message'}}
216   -
  200 +
217 201 Topic.validates_length_of :title, :within => 3..5
218 202 @topic.valid?
219 203 assert_equal 'custom message', @topic.errors.on(:title)
@@ -221,7 +205,7 @@ def test_validates_length_of_within_finds_custom_model_key_translation
221 205
222 206 def test_validates_length_of_within_finds_global_default_translation
223 207 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:too_short => 'global message'}}
224   -
  208 +
225 209 Topic.validates_length_of :title, :within => 3..5
226 210 @topic.valid?
227 211 assert_equal 'global message', @topic.errors.on(:title)
@@ -245,7 +229,7 @@ def test_validates_length_of_is_generates_message_with_custom_default_message
245 229 def test_validates_length_of_within_finds_custom_model_key_translation
246 230 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}
247 231 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
248   -
  232 +
249 233 Topic.validates_length_of :title, :is => 5
250 234 @topic.valid?
251 235 assert_equal 'custom message', @topic.errors.on(:title)
@@ -253,7 +237,7 @@ def test_validates_length_of_within_finds_custom_model_key_translation
253 237
254 238 def test_validates_length_of_within_finds_global_default_translation
255 239 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
256   -
  240 +
257 241 Topic.validates_length_of :title, :is => 5
258 242 @topic.valid?
259 243 assert_equal 'global message', @topic.errors.on(:title)
@@ -279,7 +263,7 @@ def test_validates_uniqueness_of_generates_message_with_custom_default_message
279 263 def test_validates_length_of_within_finds_custom_model_key_translation
280 264 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}
281 265 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
282   -
  266 +
283 267 Topic.validates_length_of :title, :is => 5
284 268 @topic.valid?
285 269 assert_equal 'custom message', @topic.errors.on(:title)
@@ -287,7 +271,7 @@ def test_validates_length_of_within_finds_custom_model_key_translation
287 271
288 272 def test_validates_length_of_within_finds_global_default_translation
289 273 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}}
290   -
  274 +
291 275 Topic.validates_length_of :title, :is => 5
292 276 @topic.valid?
293 277 assert_equal 'global message', @topic.errors.on(:title)
@@ -313,7 +297,7 @@ def test_validates_format_of_generates_message_with_custom_default_message
313 297 def test_validates_format_of_finds_custom_model_key_translation
314 298 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:invalid => 'custom message'}}}}}
315 299 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
316   -
  300 +
317 301 Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
318 302 @topic.valid?
319 303 assert_equal 'custom message', @topic.errors.on(:title)
@@ -321,7 +305,7 @@ def test_validates_format_of_finds_custom_model_key_translation
321 305
322 306 def test_validates_format_of_finds_global_default_translation
323 307 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
324   -
  308 +
325 309 Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
326 310 @topic.valid?
327 311 assert_equal 'global message', @topic.errors.on(:title)
@@ -347,7 +331,7 @@ def test_validates_inclusion_of_generates_message_with_custom_default_message
347 331 def test_validates_inclusion_of_finds_custom_model_key_translation
348 332 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:inclusion => 'custom message'}}}}}
349 333 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:inclusion => 'global message'}}
350   -
  334 +
351 335 Topic.validates_inclusion_of :title, :in => %w(a b c)
352 336 @topic.valid?
353 337 assert_equal 'custom message', @topic.errors.on(:title)
@@ -355,7 +339,7 @@ def test_validates_inclusion_of_finds_custom_model_key_translation
355 339
356 340 def test_validates_inclusion_of_finds_global_default_translation
357 341 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:inclusion => 'global message'}}
358   -
  342 +
359 343 Topic.validates_inclusion_of :title, :in => %w(a b c)
360 344 @topic.valid?
361 345 assert_equal 'global message', @topic.errors.on(:title)
@@ -381,7 +365,7 @@ def test_validates_exclusion_of_generates_message_with_custom_default_message
381 365 def test_validates_exclusion_of_finds_custom_model_key_translation
382 366 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:exclusion => 'custom message'}}}}}
383 367 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:exclusion => 'global message'}}
384   -
  368 +
385 369 Topic.validates_exclusion_of :title, :in => %w(a b c)
386 370 @topic.title = 'a'
387 371 @topic.valid?
@@ -390,7 +374,7 @@ def test_validates_exclusion_of_finds_custom_model_key_translation
390 374
391 375 def test_validates_exclusion_of_finds_global_default_translation
392 376 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:exclusion => 'global message'}}
393   -
  377 +
394 378 Topic.validates_exclusion_of :title, :in => %w(a b c)
395 379 @topic.title = 'a'
396 380 @topic.valid?
@@ -417,7 +401,7 @@ def test_validates_numericality_of_only_integer_generates_message_with_custom_de
417 401 def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
418 402 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}
419 403 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}}
420   -
  404 +
421 405 Topic.validates_numericality_of :title, :only_integer => true
422 406 @topic.title = 'a'
423 407 @topic.valid?
@@ -426,7 +410,7 @@ def test_validates_numericality_of_only_integer_finds_custom_model_key_translati
426 410
427 411 def test_validates_numericality_of_only_integer_finds_global_default_translation
428 412 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}}
429   -
  413 +
430 414 Topic.validates_numericality_of :title, :only_integer => true
431 415 @topic.title = 'a'
432 416 @topic.valid?
@@ -453,7 +437,7 @@ def test_validates_numericality_of_odd_generates_message_with_custom_default_mes
453 437 def test_validates_numericality_of_odd_finds_custom_model_key_translation
454 438 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:odd => 'custom message'}}}}}
455 439 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:odd => 'global message'}}
456   -
  440 +
457 441 Topic.validates_numericality_of :title, :only_integer => true, :odd => true
458 442 @topic.title = 0
459 443 @topic.valid?
@@ -462,7 +446,7 @@ def test_validates_numericality_of_odd_finds_custom_model_key_translation
462 446
463 447 def test_validates_numericality_of_odd_finds_global_default_translation
464 448 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:odd => 'global message'}}
465   -
  449 +
466 450 Topic.validates_numericality_of :title, :only_integer => true, :odd => true
467 451 @topic.title = 0
468 452 @topic.valid?
@@ -489,7 +473,7 @@ def test_validates_numericality_of_odd_generates_message_with_custom_default_mes
489 473 def test_validates_numericality_of_less_than_finds_custom_model_key_translation
490 474 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:less_than => 'custom message'}}}}}
491 475 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:less_than => 'global message'}}
492   -
  476 +
493 477 Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
494 478 @topic.title = 1
495 479 @topic.valid?
@@ -498,7 +482,7 @@ def test_validates_numericality_of_less_than_finds_custom_model_key_translation
498 482
499 483 def test_validates_numericality_of_less_than_finds_global_default_translation
500 484 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:less_than => 'global message'}}
501   -
  485 +
502 486 Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
503 487 @topic.title = 1
504 488 @topic.valid?
@@ -523,7 +507,7 @@ def test_validates_associated_generates_message_with_custom_default_message
523 507 def test_validates_associated_finds_custom_model_key_translation
524 508 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:replies => {:invalid => 'custom message'}}}}}
525 509 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
526   -
  510 +
527 511 Topic.validates_associated :replies
528 512 replied_topic.valid?
529 513 assert_equal 'custom message', replied_topic.errors.on(:replies)
@@ -531,7 +515,7 @@ def test_validates_associated_finds_custom_model_key_translation
531 515
532 516 def test_validates_associated_finds_global_default_translation
533 517 I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}}
534   -
  518 +
535 519 Topic.validates_associated :replies
536 520 replied_topic.valid?
537 521 assert_equal 'global message', replied_topic.errors.on(:replies)
2  activesupport/lib/active_support/vendor/i18n-0.0.1
... ... @@ -1 +1 @@
1   -Subproject commit 8e43afa38aa007d1de6d6acf44d43143c403d13f
  1 +Subproject commit 20c331666b3b6a21791d4cded53c3d8654fba714

0 comments on commit 8bfdabb

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