Skip to content
This repository
Browse code

Added :value as interpolation variable available to error messages

  • Loading branch information...
commit fc0358ae16d5631dd1d996b2cf95a8987f9d0c8d 1 parent e43cdb9
authored August 16, 2008 svenfuchs committed August 20, 2008
18  activerecord/lib/active_record/validations.rb
@@ -92,17 +92,19 @@ def generate_message(attribute, message = :invalid, options = {})
92 92
           :"custom.#{klass.name.underscore}.#{message}" ]
93 93
       end
94 94
       
95  
-      defaults << options[:default] if options[:default]
96  
-      defaults.flatten! << message
  95
+      defaults << options.delete(:default)
  96
+      defaults = defaults.compact.flatten << message
97 97
 
98 98
       model_name = @base.class.name
99 99
       key = defaults.shift
100  
-
101  
-      options.merge!({ 
102  
-          :default => defaults,
103  
-          :model => @base.class.human_name,
104  
-          :attribute => @base.class.human_attribute_name(attribute.to_s),
105  
-          :scope => [:activerecord, :errors, :messages] })
  100
+      value = @base.send(attribute) if @base.respond_to?(attribute)
  101
+
  102
+      options = { :default => defaults,
  103
+        :model => @base.class.human_name,
  104
+        :attribute => @base.class.human_attribute_name(attribute.to_s),
  105
+        :value => value,
  106
+        :scope => [:activerecord, :errors, :messages]
  107
+      }.merge(options)
106 108
 
107 109
       I18n.translate(key, options)
108 110
     end
4  activerecord/test/cases/validations_i18n_test.rb
@@ -48,7 +48,7 @@ def test_errors_generate_message_translates_custom_model_attribute_key
48 48
 
49 49
       I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic')
50 50
       I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title')
51  
-      I18n.expects(:translate).with(:"custom.topic.title.invalid", :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')
  51
+      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')
52 52
       @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1'
53 53
     end
54 54
 
@@ -56,7 +56,7 @@ def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
56 56
       custom_scope = [:activerecord, :errors, :custom, 'topic', :title]
57 57
       I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply')
58 58
       I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title')
59  
-      I18n.expects(:translate).with(:"custom.reply.title.invalid", :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")
  59
+      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")
60 60
       Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
61 61
     end
62 62
 

0 notes on commit fc0358a

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