public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Custom error messages scope improved
iain (author)
Wed Aug 20 14:43:46 -0700 2008
svenfuchs (committer)
Thu Aug 21 09:40:44 -0700 2008
commit  cf6840773b4f5046151f4d28c286069aabaafa10
tree    74edf74d2dfd816eff0c56ec63b5d02df55b92b4
parent  c324ef2785f82f03bb0e703da27ba2b8d97a2da1
...
68
69
70
71
72
73
74
 
 
 
 
75
76
77
78
79
80
81
82
83
84
 
 
 
 
85
86
 
87
88
89
90
91
92
 
 
93
94
95
96
 
97
98
99
100
 
101
102
103
104
105
106
 
107
108
109
...
68
69
70
 
 
 
 
71
72
73
74
75
76
77
78
79
80
 
 
 
 
81
82
83
84
85
 
86
87
88
89
90
 
 
91
92
93
94
95
 
96
97
98
99
 
100
101
102
103
104
105
 
106
107
108
109
0
@@ -68,42 +68,42 @@ module ActiveRecord
0
     end
0
     
0
     # Translates an error message in it's default scope (<tt>activerecord.errrors.messages</tt>).
0
-    # Error messages are first looked up in <tt>custom.MODEL.ATTRIBUTE.MESSAGE</tt>, if it's not there, it's looked up
0
-    # in <tt>custom.MODEL.ATTRIBUTE</tt> and if that is not there it returns the translation of the default message
0
-    # (e.g. <tt>activerecord.errors.messages.MESSAGE</tt>). Both the model name and the attribute name are available for
0
-    # interpolation.
0
+    # Error messages are first looked up in <tt>models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>, if it's not there, 
0
+    # it's looked up in <tt>models.MODEL.MESSAGE</tt> and if that is not there it returns the translation of the 
0
+    # default message (e.g. <tt>activerecord.errors.messages.MESSAGE</tt>). The translated model name, 
0
+    # translated attribute name and the value are available for interpolation.
0
     #
0
     # When using inheritence in your models, it will check all the inherited models too, but only if the model itself
0
     # hasn't been found. Say you have <tt>class Admin < User; end</tt> and you wanted the translation for the <tt>:blank</tt>
0
     # error +message+ for the <tt>title</tt> +attribute+, it looks for these translations:
0
     # 
0
     # <ol>
0
-    # <li><tt>activerecord.errors.messages.custom.admin.title.blank</tt></li>
0
-    # <li><tt>activerecord.errors.messages.custom.admin.blank</tt></li>
0
-    # <li><tt>activerecord.errors.messages.custom.user.title.blank</tt></li>
0
-    # <li><tt>activerecord.errors.messages.custom.user.blank</tt></li>
0
+    # <li><tt>activerecord.errors.models.admin.attributes.title.blank</tt></li>
0
+    # <li><tt>activerecord.errors.models.admin.blank</tt></li>
0
+    # <li><tt>activerecord.errors.models.user.attributes.title.blank</tt></li>
0
+    # <li><tt>activerecord.errors.models.user.blank</tt></li>
0
     # <li><tt>activerecord.errors.messages.blank</tt></li>
0
-    # <li>any default you provided through the +options+ hash</li>
0
+    # <li>any default you provided through the +options+ hash (in the activerecord.errors scope)</li>
0
     # </ol>
0
     def generate_message(attribute, message = :invalid, options = {})
0
 
0
       defaults = @base.class.self_and_descendents_from_active_record.map do |klass| 
0
-        [ :"custom.#{klass.name.underscore}.#{attribute}.#{message}", 
0
-          :"custom.#{klass.name.underscore}.#{message}" ]
0
+        [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", 
0
+          :"models.#{klass.name.underscore}.#{message}" ]
0
       end
0
       
0
       defaults << options.delete(:default)
0
-      defaults = defaults.compact.flatten << message
0
+      defaults = defaults.compact.flatten << :"messages.#{message}"
0
 
0
       model_name = @base.class.name
0
       key = defaults.shift
0
-      value = @base.send(attribute) if @base.respond_to?(attribute)
0
+      value = @base.respond_to?(attribute) ? @base.send(attribute) : nil
0
 
0
       options = { :default => defaults,
0
         :model => @base.class.human_name,
0
         :attribute => @base.class.human_attribute_name(attribute.to_s),
0
         :value => value,
0
-        :scope => [:activerecord, :errors, :messages]
0
+        :scope => [:activerecord, :errors]
0
       }.merge(options)
0
 
0
       I18n.translate(key, options)
...
42
43
44
 
45
46
47
48
49
50
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
53
54
55
56
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
62
63
...
347
348
349
350
 
351
352
353
...
368
369
370
371
 
372
373
374
...
387
388
389
390
 
391
392
393
...
406
407
408
409
 
410
411
412
...
425
426
427
428
 
429
430
431
...
444
445
446
447
 
448
449
450
...
464
465
466
467
 
468
469
470
...
483
484
485
486
 
487
488
489
...
502
503
504
505
 
506
507
508
...
523
524
525
526
 
527
528
529
...
544
545
546
547
 
548
549
550
...
565
566
567
568
 
569
570
571
...
586
587
588
589
 
590
591
592
...
608
609
610
611
 
612
613
614
...
42
43
44
45
46
 
 
47
 
 
 
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
 
 
 
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
399
400
401
 
402
403
404
405
...
420
421
422
 
423
424
425
426
...
439
440
441
 
442
443
444
445
...
458
459
460
 
461
462
463
464
...
477
478
479
 
480
481
482
483
...
496
497
498
 
499
500
501
502
...
516
517
518
 
519
520
521
522
...
535
536
537
 
538
539
540
541
...
554
555
556
 
557
558
559
560
...
575
576
577
 
578
579
580
581
...
596
597
598
 
599
600
601
602
...
617
618
619
 
620
621
622
623
...
638
639
640
 
641
642
643
644
...
660
661
662
 
663
664
665
666
0
@@ -42,22 +42,74 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   
0
   # ActiveRecord::Errors
0
   uses_mocha 'ActiveRecord::Errors' do
0
+
0
     def test_errors_generate_message_translates_custom_model_attribute_key
0
-      global_scope = [:activerecord, :errors, :messages]
0
-      custom_scope = global_scope + [:custom, 'topic', :title]
0
 
0
-      I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic')
0
-      I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title')
0
-      I18n.expects(:translate).with(:"custom.topic.title.invalid", :value => nil, :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1')
0
+      I18n.expects(:translate).with(
0
+        :topic, 
0
+        { :count => 1, 
0
+          :default => ['Topic'], 
0
+          :scope => [:activerecord, :models]
0
+        }
0
+      ).returns('Topic')
0
+
0
+      I18n.expects(:translate).with(
0
+        :"topic.title", 
0
+        { :count => 1, 
0
+          :default => ['Title'], 
0
+          :scope => [:activerecord, :attributes]
0
+        }
0
+      ).returns('Title')
0
+
0
+      I18n.expects(:translate).with(
0
+        :"models.topic.attributes.title.invalid", 
0
+        :value => nil, 
0
+        :scope => [:activerecord, :errors], 
0
+        :default => [
0
+          :"models.topic.invalid", 
0
+          'default from class def error 1', 
0
+          :"messages.invalid"], 
0
+        :attribute => "Title", 
0
+        :model => "Topic"
0
+      ).returns('default from class def error 1')
0
+
0
       @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1'
0
     end
0
 
0
     def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
0
-      custom_scope = [:activerecord, :errors, :custom, 'topic', :title]
0
-      I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply')
0
-      I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title')
0
-      I18n.expects(:translate).with(:"custom.reply.title.invalid", :value => nil, :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def")
0
+
0
+      I18n.expects(:translate).with(
0
+        :reply, 
0
+        { :count => 1, 
0
+          :default => [:topic, 'Reply'], 
0
+          :scope => [:activerecord, :models]
0
+        }
0
+      ).returns('Reply')
0
+
0
+      I18n.expects(:translate).with(
0
+        :"reply.title", 
0
+        { :count => 1, 
0
+          :default => [:'topic.title', 'Title'], 
0
+          :scope => [:activerecord, :attributes]
0
+        }
0
+      ).returns('Title')
0
+      
0
+      I18n.expects(:translate).with(
0
+        :"models.reply.attributes.title.invalid", 
0
+        :value => nil, 
0
+        :scope => [:activerecord, :errors], 
0
+        :default => [
0
+          :"models.reply.invalid", 
0
+          :"models.topic.attributes.title.invalid", 
0
+          :"models.topic.invalid", 
0
+          'default from class def', 
0
+          :"messages.invalid"], 
0
+        :model => 'Reply', 
0
+        :attribute => 'Title'
0
+      ).returns("default from class def")
0
+      
0
       Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
0
+    
0
     end
0
 
0
     def test_errors_add_on_empty_generates_message
0
@@ -347,7 +399,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_confirmation_of w/o mocha
0
   
0
   def test_validates_confirmation_of_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:confirmation => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}}
0
   
0
     Topic.validates_confirmation_of :title
0
@@ -368,7 +420,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_acceptance_of w/o mocha
0
   
0
   def test_validates_acceptance_of_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:accepted => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}}
0
   
0
     Topic.validates_acceptance_of :title, :allow_nil => false
0
@@ -387,7 +439,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_presence_of w/o mocha
0
     
0
   def test_validates_presence_of_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:blank => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}}
0
   
0
     Topic.validates_presence_of :title
0
@@ -406,7 +458,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_length_of :within w/o mocha
0
   
0
   def test_validates_length_of_within_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:too_short => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}}
0
   
0
     Topic.validates_length_of :title, :within => 3..5
0
@@ -425,7 +477,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_length_of :is w/o mocha
0
   
0
   def test_validates_length_of_within_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
0
   
0
     Topic.validates_length_of :title, :is => 5
0
@@ -444,7 +496,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_uniqueness_of w/o mocha
0
   
0
   def test_validates_length_of_within_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
0
   
0
     Topic.validates_length_of :title, :is => 5
0
@@ -464,7 +516,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_format_of w/o mocha
0
   
0
   def test_validates_format_of_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:invalid => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
0
   
0
     Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
0
@@ -483,7 +535,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_inclusion_of w/o mocha
0
   
0
   def test_validates_inclusion_of_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:inclusion => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}}
0
   
0
     Topic.validates_inclusion_of :title, :in => %w(a b c)
0
@@ -502,7 +554,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_exclusion_of w/o mocha
0
   
0
   def test_validates_exclusion_of_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:exclusion => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}}
0
   
0
     Topic.validates_exclusion_of :title, :in => %w(a b c)
0
@@ -523,7 +575,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_numericality_of without :only_integer w/o mocha
0
   
0
   def test_validates_numericality_of_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
0
   
0
     Topic.validates_numericality_of :title
0
@@ -544,7 +596,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_numericality_of with :only_integer w/o mocha
0
   
0
   def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
0
   
0
     Topic.validates_numericality_of :title, :only_integer => true
0
@@ -565,7 +617,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_numericality_of :odd w/o mocha
0
   
0
   def test_validates_numericality_of_odd_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:odd => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}}
0
   
0
     Topic.validates_numericality_of :title, :only_integer => true, :odd => true
0
@@ -586,7 +638,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_numericality_of :less_than w/o mocha
0
   
0
   def test_validates_numericality_of_less_than_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:less_than => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}}
0
   
0
     Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
0
@@ -608,7 +660,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
0
   # validates_associated w/o mocha
0
   
0
   def test_validates_associated_finds_custom_model_key_translation
0
-    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:replies => {:invalid => 'custom message'}}}}}}
0
+    I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}}
0
     I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
0
   
0
     Topic.validates_associated :replies

Comments