Skip to content
This repository
Browse code

Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an opt…

…ions hash and make various Validators pass their (filtered) options.

This makes it possible to pass additional options through Validators to message
generation. E.g. plugin authors want to add validates_presence_of :foo, :format
=> "some format".

Also, cleanup the :default vs :message options confusion in ActiveModel
validation message generation.

Also, deprecate ActiveModel::Errors#add_on_blank(attributes, custom_message) in
favor of ActiveModel::Errors#add_on_blank(attributes, options).

Original patch by Sven Fuchs, some minor changes and has been changed to be applicable to master again

[#4057 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit bc1c8d58ec45593acba614d1d0fecb49adef08ff 1 parent 47c9a35
Jeroen van Dijk authored May 15, 2010 jeremy committed May 15, 2010
54  activemodel/lib/active_model/errors.rb
@@ -179,25 +179,45 @@ def to_xml(options={})
179 179
     # If +message+ is a Proc, it will be called, allowing for things like Time.now to be used within an error
180 180
     def add(attribute, message = nil, options = {})
181 181
       message ||= :invalid
182  
-      message = generate_message(attribute, message, options) if message.is_a?(Symbol)
  182
+
  183
+      reserved = [:minimum, :maximum, :is, :within , :in, :allow_nil, :allow_blank, :case_sensitive,
  184
+                  :too_long, :too_short, :wrong_length, :on, :if, :unless , :tokenizer, :invalid,
  185
+                  :only_integer, :odd, :even, :less_than, :with, :accept]
  186
+
  187
+      message = generate_message(attribute, message, options.except(*reserved)) if message.is_a?(Symbol)
  188
+
183 189
       message = message.call if message.is_a?(Proc)
184 190
       self[attribute] << message
185 191
     end
186 192
 
187 193
     # Will add an error message to each of the attributes in +attributes+ that is empty.
188  
-    def add_on_empty(attributes, custom_message = nil)
  194
+    def add_on_empty(attributes, options = {})
  195
+      if options && !options.is_a?(Hash)
  196
+        options = { :message => options }
  197
+        ActiveSupport::Deprecation.warn \
  198
+          "ActiveModel::Errors#add_on_empty(attributes, custom_message) has been deprecated.\n" +
  199
+          "Instead of passing a custom_message pass an options Hash { :message => custom_message }."
  200
+      end
  201
+
189 202
       [attributes].flatten.each do |attribute|
190 203
         value = @base.send(:read_attribute_for_validation, attribute)
191 204
         is_empty = value.respond_to?(:empty?) ? value.empty? : false
192  
-        add(attribute, :empty, :default => custom_message) unless !value.nil? && !is_empty
  205
+        add(attribute, :empty, options) if value.nil? || is_empty
193 206
       end
194 207
     end
195 208
 
196 209
     # Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).
197  
-    def add_on_blank(attributes, custom_message = nil)
  210
+    def add_on_blank(attributes, options = {})
  211
+      if options && !options.is_a?(Hash)
  212
+        options = { :message => options }
  213
+        ActiveSupport::Deprecation.warn \
  214
+          "ActiveModel::Errors#add_on_blank(attributes, custom_message) has been deprecated.\n" +
  215
+          "Instead of passing a custom_message pass an options Hash { :message => custom_message }."
  216
+      end
  217
+
198 218
       [attributes].flatten.each do |attribute|
199 219
         value = @base.send(:read_attribute_for_validation, attribute)
200  
-        add(attribute, :blank, :default => custom_message) if value.blank?
  220
+        add(attribute, :blank, options) if value.blank?
201 221
       end
202 222
     end
203 223
 
@@ -254,18 +274,26 @@ def full_messages
254 274
     # <li><tt>errors.attributes.title.blank</tt></li>
255 275
     # <li><tt>errors.messages.blank</tt></li>
256 276
     # </ol>
257  
-    def generate_message(attribute, message = :invalid, options = {})
258  
-      message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
  277
+
  278
+    def generate_message(attribute, type = :invalid, options = {})
  279
+      type = options.delete(:message) if options[:message].is_a?(Symbol)
  280
+
  281
+      if options[:default]
  282
+        ActiveSupport::Deprecation.warn \
  283
+          "ActiveModel::Errors#generate_message(attributes, custom_message) has been deprecated.\n" +
  284
+          "Use ActiveModel::Errors#generate_message(attributes, :message => 'your message') instead."
  285
+        options[:message] = options.delete(:default)
  286
+      end
259 287
 
260 288
       defaults = @base.class.lookup_ancestors.map do |klass|
261  
-        [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{message}",
262  
-          :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{message}" ]
  289
+        [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{type}",
  290
+          :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ]
263 291
       end
264 292
 
265  
-      defaults << options.delete(:default)
266  
-      defaults << :"#{@base.class.i18n_scope}.errors.messages.#{message}"
267  
-      defaults << :"errors.attributes.#{attribute}.#{message}"
268  
-      defaults << :"errors.messages.#{message}"
  293
+      defaults << options.delete(:message)
  294
+      defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}"
  295
+      defaults << :"errors.attributes.#{attribute}.#{type}"
  296
+      defaults << :"errors.messages.#{type}"
269 297
 
270 298
       defaults.compact!
271 299
       defaults.flatten!
2  activemodel/lib/active_model/validations/acceptance.rb
@@ -7,7 +7,7 @@ def initialize(options)
7 7
 
8 8
       def validate_each(record, attribute, value)
9 9
         unless value == options[:accept]
10  
-          record.errors.add(attribute, :accepted, :default => options[:message])
  10
+          record.errors.add(attribute, :accepted, options)
11 11
         end
12 12
       end
13 13
       
2  activemodel/lib/active_model/validations/confirmation.rb
@@ -4,7 +4,7 @@ class ConfirmationValidator < EachValidator
4 4
       def validate_each(record, attribute, value)
5 5
         confirmed = record.send(:"#{attribute}_confirmation")
6 6
         return if confirmed.nil? || value == confirmed
7  
-        record.errors.add(attribute, :confirmation, :default => options[:message])
  7
+        record.errors.add(attribute, :confirmation, options)
8 8
       end
9 9
       
10 10
       def setup(klass)
2  activemodel/lib/active_model/validations/exclusion.rb
@@ -8,7 +8,7 @@ def check_validity!
8 8
 
9 9
       def validate_each(record, attribute, value)
10 10
         return unless options[:in].include?(value)
11  
-        record.errors.add(attribute, :exclusion, :default => options[:message], :value => value)
  11
+        record.errors.add(attribute, :exclusion, options.merge(:value => value))
12 12
       end
13 13
     end
14 14
 
4  activemodel/lib/active_model/validations/format.rb
@@ -3,9 +3,9 @@ module Validations
3 3
     class FormatValidator < EachValidator
4 4
       def validate_each(record, attribute, value)
5 5
         if options[:with] && value.to_s !~ options[:with]
6  
-          record.errors.add(attribute, :invalid, :default => options[:message], :value => value)
  6
+          record.errors.add(attribute, :invalid, options.merge(:value => value))
7 7
         elsif options[:without] && value.to_s =~ options[:without]
8  
-          record.errors.add(attribute, :invalid, :default => options[:message], :value => value)
  8
+          record.errors.add(attribute, :invalid, options.merge(:value => value))
9 9
         end
10 10
       end
11 11
       
2  activemodel/lib/active_model/validations/inclusion.rb
@@ -8,7 +8,7 @@ def check_validity!
8 8
 
9 9
       def validate_each(record, attribute, value)
10 10
         return if options[:in].include?(value)
11  
-        record.errors.add(attribute, :inclusion, :default => options[:message], :value => value)
  11
+        record.errors.add(attribute, :inclusion, options.merge(:value => value))
12 12
       end
13 13
     end
14 14
 
4  activemodel/lib/active_model/validations/length.rb
@@ -37,7 +37,7 @@ def validate_each(record, attribute, value)
37 37
 
38 38
         CHECKS.each do |key, validity_check|
39 39
           next unless check_value = options[key]
40  
-          custom_message = options[:message] || options[MESSAGES[key]]
  40
+          options[:message] ||= options[MESSAGES[key]] if options[MESSAGES[key]]
41 41
 
42 42
           valid_value = if key == :maximum
43 43
             value.nil? || value.size.send(validity_check, check_value)
@@ -46,7 +46,7 @@ def validate_each(record, attribute, value)
46 46
           end
47 47
 
48 48
           next if valid_value
49  
-          record.errors.add(attribute, MESSAGES[key], :default => custom_message, :count => check_value)
  49
+          record.errors.add(attribute, MESSAGES[key], options.merge(:count => check_value))
50 50
         end
51 51
       end
52 52
     end
8  activemodel/lib/active_model/validations/numericality.rb
@@ -26,13 +26,13 @@ def validate_each(record, attr_name, value)
26 26
         return if options[:allow_nil] && raw_value.nil?
27 27
 
28 28
         unless value = parse_raw_value_as_a_number(raw_value)
29  
-          record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => options[:message])
  29
+          record.errors.add(attr_name, :not_a_number, options.merge(:value => raw_value))
30 30
           return
31 31
         end
32 32
 
33 33
         if options[:only_integer]
34 34
           unless value = parse_raw_value_as_an_integer(raw_value)
35  
-            record.errors.add(attr_name, :not_an_integer, :value => raw_value, :default => options[:message])
  35
+            record.errors.add(attr_name, :not_an_integer, options.merge(:value => raw_value))
36 36
             return
37 37
           end
38 38
         end
@@ -41,14 +41,14 @@ def validate_each(record, attr_name, value)
41 41
           case option
42 42
           when :odd, :even
43 43
             unless value.to_i.send(CHECKS[option])
44  
-              record.errors.add(attr_name, option, :value => value, :default => options[:message])
  44
+              record.errors.add(attr_name, option, options.merge(:value => value))
45 45
             end
46 46
           else
47 47
             option_value = option_value.call(record) if option_value.is_a?(Proc)
48 48
             option_value = record.send(option_value) if option_value.is_a?(Symbol)
49 49
 
50 50
             unless value.send(CHECKS[option], option_value)
51  
-              record.errors.add(attr_name, option, :default => options[:message], :value => value, :count => option_value)
  51
+              record.errors.add(attr_name, option, options.merge(:value => value, :count => option_value))
52 52
             end
53 53
           end
54 54
         end
2  activemodel/lib/active_model/validations/presence.rb
@@ -4,7 +4,7 @@ module ActiveModel
4 4
   module Validations
5 5
     class PresenceValidator < EachValidator
6 6
       def validate(record)
7  
-        record.errors.add_on_blank(attributes, options[:message])
  7
+        record.errors.add_on_blank(attributes, options)
8 8
       end
9 9
     end
10 10
 
82  activemodel/test/cases/validations/i18n_generate_message_validation_test.rb
@@ -8,131 +8,131 @@ def setup
8 8
     @person = Person.new
9 9
   end
10 10
 
11  
-  # validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value)
  11
+  # validates_inclusion_of: generate_message(attr_name, :inclusion, :message => custom_message, :value => value)
12 12
   def test_generate_message_inclusion_with_default_message
13  
-    assert_equal 'is not included in the list', @person.errors.generate_message(:title, :inclusion, :default => nil, :value => 'title')
  13
+    assert_equal 'is not included in the list', @person.errors.generate_message(:title, :inclusion, :value => 'title')
14 14
   end
15 15
 
16 16
   def test_generate_message_inclusion_with_custom_message
17  
-    assert_equal 'custom message title', @person.errors.generate_message(:title, :inclusion, :default => 'custom message %{value}', :value => 'title')
  17
+    assert_equal 'custom message title', @person.errors.generate_message(:title, :inclusion, :message => 'custom message %{value}', :value => 'title')
18 18
   end
19 19
 
20  
-  # validates_exclusion_of: generate_message(attr_name, :exclusion, :default => configuration[:message], :value => value)
  20
+  # validates_exclusion_of: generate_message(attr_name, :exclusion, :message => custom_message, :value => value)
21 21
   def test_generate_message_exclusion_with_default_message
22  
-    assert_equal 'is reserved', @person.errors.generate_message(:title, :exclusion, :default => nil, :value => 'title')
  22
+    assert_equal 'is reserved', @person.errors.generate_message(:title, :exclusion, :value => 'title')
23 23
   end
24 24
 
25 25
   def test_generate_message_exclusion_with_custom_message
26  
-    assert_equal 'custom message title', @person.errors.generate_message(:title, :exclusion, :default => 'custom message %{value}', :value => 'title')
  26
+    assert_equal 'custom message title', @person.errors.generate_message(:title, :exclusion, :message => 'custom message %{value}', :value => 'title')
27 27
   end
28 28
 
29  
-  # validates_format_of:  generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
  29
+  # validates_format_of:  generate_message(attr_name, :invalid, :message => custom_message, :value => value)
30 30
   def test_generate_message_invalid_with_default_message
31  
-    assert_equal 'is invalid', @person.errors.generate_message(:title, :invalid, :default => nil, :value => 'title')
  31
+    assert_equal 'is invalid', @person.errors.generate_message(:title, :invalid, :value => 'title')
32 32
   end
33 33
 
34 34
   def test_generate_message_invalid_with_custom_message
35  
-    assert_equal 'custom message title', @person.errors.generate_message(:title, :invalid, :default => 'custom message %{value}', :value => 'title')
  35
+    assert_equal 'custom message title', @person.errors.generate_message(:title, :invalid, :message => 'custom message %{value}', :value => 'title')
36 36
   end
37 37
 
38  
-  # validates_confirmation_of: generate_message(attr_name, :confirmation, :default => configuration[:message])
  38
+  # validates_confirmation_of: generate_message(attr_name, :confirmation, :message => custom_message)
39 39
   def test_generate_message_confirmation_with_default_message
40  
-    assert_equal "doesn't match confirmation", @person.errors.generate_message(:title, :confirmation, :default => nil)
  40
+    assert_equal "doesn't match confirmation", @person.errors.generate_message(:title, :confirmation)
41 41
   end
42 42
 
43 43
   def test_generate_message_confirmation_with_custom_message
44  
-    assert_equal 'custom message', @person.errors.generate_message(:title, :confirmation, :default => 'custom message')
  44
+    assert_equal 'custom message', @person.errors.generate_message(:title, :confirmation, :message => 'custom message')
45 45
   end
46 46
 
47  
-  # validates_acceptance_of: generate_message(attr_name, :accepted, :default => configuration[:message])
  47
+  # validates_acceptance_of: generate_message(attr_name, :accepted, :message => custom_message)
48 48
   def test_generate_message_accepted_with_default_message
49  
-    assert_equal "must be accepted", @person.errors.generate_message(:title, :accepted, :default => nil)
  49
+    assert_equal "must be accepted", @person.errors.generate_message(:title, :accepted)
50 50
   end
51 51
 
52 52
   def test_generate_message_accepted_with_custom_message
53  
-    assert_equal 'custom message', @person.errors.generate_message(:title, :accepted, :default => 'custom message')
  53
+    assert_equal 'custom message', @person.errors.generate_message(:title, :accepted, :message => 'custom message')
54 54
   end
55 55
 
56  
-  # add_on_empty: generate_message(attr, :empty, :default => custom_message)
  56
+  # add_on_empty: generate_message(attr, :empty, :message => custom_message)
57 57
   def test_generate_message_empty_with_default_message
58  
-    assert_equal "can't be empty", @person.errors.generate_message(:title, :empty, :default => nil)
  58
+    assert_equal "can't be empty", @person.errors.generate_message(:title, :empty)
59 59
   end
60 60
 
61 61
   def test_generate_message_empty_with_custom_message
62  
-    assert_equal 'custom message', @person.errors.generate_message(:title, :empty, :default => 'custom message')
  62
+    assert_equal 'custom message', @person.errors.generate_message(:title, :empty, :message => 'custom message')
63 63
   end
64 64
 
65  
-  # add_on_blank: generate_message(attr, :blank, :default => custom_message)
  65
+  # add_on_blank: generate_message(attr, :blank, :message => custom_message)
66 66
   def test_generate_message_blank_with_default_message
67  
-    assert_equal "can't be blank", @person.errors.generate_message(:title, :blank, :default => nil)
  67
+    assert_equal "can't be blank", @person.errors.generate_message(:title, :blank)
68 68
   end
69 69
 
70 70
   def test_generate_message_blank_with_custom_message
71  
-    assert_equal 'custom message', @person.errors.generate_message(:title, :blank, :default => 'custom message')
  71
+    assert_equal 'custom message', @person.errors.generate_message(:title, :blank, :message => 'custom message')
72 72
   end
73 73
 
74  
-  # validates_length_of: generate_message(attr, :too_long, :default => options[:too_long], :count => option_value.end)
  74
+  # validates_length_of: generate_message(attr, :too_long, :message => custom_message, :count => option_value.end)
75 75
   def test_generate_message_too_long_with_default_message
76  
-    assert_equal "is too long (maximum is 10 characters)", @person.errors.generate_message(:title, :too_long, :default => nil, :count => 10)
  76
+    assert_equal "is too long (maximum is 10 characters)", @person.errors.generate_message(:title, :too_long, :count => 10)
77 77
   end
78 78
 
79 79
   def test_generate_message_too_long_with_custom_message
80  
-    assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_long, :default => 'custom message %{count}', :count => 10)
  80
+    assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_long, :message => 'custom message %{count}', :count => 10)
81 81
   end
82 82
 
83  
-  # validates_length_of: generate_message(attr, :too_short, :default => options[:too_short], :count => option_value.begin)
  83
+  # validates_length_of: generate_message(attr, :too_short, :default => custom_message, :count => option_value.begin)
84 84
   def test_generate_message_too_short_with_default_message
85  
-    assert_equal "is too short (minimum is 10 characters)", @person.errors.generate_message(:title, :too_short, :default => nil, :count => 10)
  85
+    assert_equal "is too short (minimum is 10 characters)", @person.errors.generate_message(:title, :too_short, :count => 10)
86 86
   end
87 87
 
88 88
   def test_generate_message_too_short_with_custom_message
89  
-    assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_short, :default => 'custom message %{count}', :count => 10)
  89
+    assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_short, :message => 'custom message %{count}', :count => 10)
90 90
   end
91 91
 
92  
-  # validates_length_of: generate_message(attr, key, :default => custom_message, :count => option_value)
  92
+  # validates_length_of: generate_message(attr, :wrong_length, :message => custom_message, :count => option_value)
93 93
   def test_generate_message_wrong_length_with_default_message
94  
-    assert_equal "is the wrong length (should be 10 characters)", @person.errors.generate_message(:title, :wrong_length, :default => nil, :count => 10)
  94
+    assert_equal "is the wrong length (should be 10 characters)", @person.errors.generate_message(:title, :wrong_length, :count => 10)
95 95
   end
96 96
 
97 97
   def test_generate_message_wrong_length_with_custom_message
98  
-    assert_equal 'custom message 10', @person.errors.generate_message(:title, :wrong_length, :default => 'custom message %{count}', :count => 10)
  98
+    assert_equal 'custom message 10', @person.errors.generate_message(:title, :wrong_length, :message => 'custom message %{count}', :count => 10)
99 99
   end
100 100
 
101  
-  # validates_numericality_of: generate_message(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message])
  101
+  # validates_numericality_of: generate_message(attr_name, :not_a_number, :value => raw_value, :message => custom_message)
102 102
   def test_generate_message_not_a_number_with_default_message
103  
-    assert_equal "is not a number", @person.errors.generate_message(:title, :not_a_number, :default => nil, :value => 'title')
  103
+    assert_equal "is not a number", @person.errors.generate_message(:title, :not_a_number, :value => 'title')
104 104
   end
105 105
 
106 106
   def test_generate_message_not_a_number_with_custom_message
107  
-    assert_equal 'custom message title', @person.errors.generate_message(:title, :not_a_number, :default => 'custom message %{value}', :value => 'title')
  107
+    assert_equal 'custom message title', @person.errors.generate_message(:title, :not_a_number, :message => 'custom message %{value}', :value => 'title')
108 108
   end
109 109
 
110  
-  # validates_numericality_of: generate_message(attr_name, option, :value => raw_value, :default => configuration[:message])
  110
+  # validates_numericality_of: generate_message(attr_name, option, :value => raw_value, :default => custom_message)
111 111
   def test_generate_message_greater_than_with_default_message
112  
-    assert_equal "must be greater than 10", @person.errors.generate_message(:title, :greater_than, :default => nil, :value => 'title', :count => 10)
  112
+    assert_equal "must be greater than 10", @person.errors.generate_message(:title, :greater_than, :value => 'title', :count => 10)
113 113
   end
114 114
 
115 115
   def test_generate_message_greater_than_or_equal_to_with_default_message
116  
-    assert_equal "must be greater than or equal to 10", @person.errors.generate_message(:title, :greater_than_or_equal_to, :default => nil, :value => 'title', :count => 10)
  116
+    assert_equal "must be greater than or equal to 10", @person.errors.generate_message(:title, :greater_than_or_equal_to, :value => 'title', :count => 10)
117 117
   end
118 118
 
119 119
   def test_generate_message_equal_to_with_default_message
120  
-    assert_equal "must be equal to 10", @person.errors.generate_message(:title, :equal_to, :default => nil, :value => 'title', :count => 10)
  120
+    assert_equal "must be equal to 10", @person.errors.generate_message(:title, :equal_to, :value => 'title', :count => 10)
121 121
   end
122 122
 
123 123
   def test_generate_message_less_than_with_default_message
124  
-    assert_equal "must be less than 10", @person.errors.generate_message(:title, :less_than, :default => nil, :value => 'title', :count => 10)
  124
+    assert_equal "must be less than 10", @person.errors.generate_message(:title, :less_than, :value => 'title', :count => 10)
125 125
   end
126 126
 
127 127
   def test_generate_message_less_than_or_equal_to_with_default_message
128  
-    assert_equal "must be less than or equal to 10", @person.errors.generate_message(:title, :less_than_or_equal_to, :default => nil, :value => 'title', :count => 10)
  128
+    assert_equal "must be less than or equal to 10", @person.errors.generate_message(:title, :less_than_or_equal_to, :value => 'title', :count => 10)
129 129
   end
130 130
 
131 131
   def test_generate_message_odd_with_default_message
132  
-    assert_equal "must be odd", @person.errors.generate_message(:title, :odd, :default => nil, :value => 'title', :count => 10)
  132
+    assert_equal "must be odd", @person.errors.generate_message(:title, :odd, :value => 'title', :count => 10)
133 133
   end
134 134
 
135 135
   def test_generate_message_even_with_default_message
136  
-    assert_equal "must be even", @person.errors.generate_message(:title, :even, :default => nil, :value => 'title', :count => 10)
  136
+    assert_equal "must be even", @person.errors.generate_message(:title, :even, :value => 'title', :count => 10)
137 137
   end
138 138
 end
64  activemodel/test/cases/validations/i18n_validation_test.rb
@@ -22,23 +22,23 @@ def teardown
22 22
   end
23 23
 
24 24
   def test_errors_add_on_empty_generates_message
25  
-    @person.errors.expects(:generate_message).with(:title, :empty, {:default => nil})
  25
+    @person.errors.expects(:generate_message).with(:title, :empty, {})
26 26
     @person.errors.add_on_empty :title
27 27
   end
28 28
 
29 29
   def test_errors_add_on_empty_generates_message_with_custom_default_message
30  
-    @person.errors.expects(:generate_message).with(:title, :empty, {:default => 'custom'})
31  
-    @person.errors.add_on_empty :title, 'custom'
  30
+    @person.errors.expects(:generate_message).with(:title, :empty, {:message => 'custom'})
  31
+    @person.errors.add_on_empty :title, :message => 'custom'
32 32
   end
33 33
 
34 34
   def test_errors_add_on_blank_generates_message
35  
-    @person.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
  35
+    @person.errors.expects(:generate_message).with(:title, :blank, {})
36 36
     @person.errors.add_on_blank :title
37 37
   end
38 38
 
39 39
   def test_errors_add_on_blank_generates_message_with_custom_default_message
40  
-    @person.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
41  
-    @person.errors.add_on_blank :title, 'custom'
  40
+    @person.errors.expects(:generate_message).with(:title, :blank, {:message => 'custom'})
  41
+    @person.errors.add_on_blank :title, :message => 'custom'
42 42
   end
43 43
 
44 44
   def test_full_message_encoding
@@ -66,14 +66,14 @@ def test_errors_full_messages_uses_format
66 66
   def test_validates_confirmation_of_generates_message
67 67
     Person.validates_confirmation_of :title
68 68
     @person.title_confirmation = 'foo'
69  
-    @person.errors.expects(:generate_message).with(:title, :confirmation, {:default => nil})
  69
+    @person.errors.expects(:generate_message).with(:title, :confirmation, {})
70 70
     @person.valid?
71 71
   end
72 72
 
73 73
   def test_validates_confirmation_of_generates_message_with_custom_default_message
74 74
     Person.validates_confirmation_of :title, :message => 'custom'
75 75
     @person.title_confirmation = 'foo'
76  
-    @person.errors.expects(:generate_message).with(:title, :confirmation, {:default => 'custom'})
  76
+    @person.errors.expects(:generate_message).with(:title, :confirmation, {:message => 'custom'})
77 77
     @person.valid?
78 78
   end
79 79
 
@@ -81,13 +81,13 @@ def test_validates_confirmation_of_generates_message_with_custom_default_message
81 81
 
82 82
   def test_validates_acceptance_of_generates_message
83 83
     Person.validates_acceptance_of :title, :allow_nil => false
84  
-    @person.errors.expects(:generate_message).with(:title, :accepted, {:default => nil})
  84
+    @person.errors.expects(:generate_message).with(:title, :accepted, {})
85 85
     @person.valid?
86 86
   end
87 87
 
88 88
   def test_validates_acceptance_of_generates_message_with_custom_default_message
89 89
     Person.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
90  
-    @person.errors.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
  90
+    @person.errors.expects(:generate_message).with(:title, :accepted, {:message => 'custom'})
91 91
     @person.valid?
92 92
   end
93 93
 
@@ -95,13 +95,13 @@ def test_validates_acceptance_of_generates_message_with_custom_default_message
95 95
 
96 96
   def test_validates_presence_of_generates_message
97 97
     Person.validates_presence_of :title
98  
-    @person.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
  98
+    @person.errors.expects(:generate_message).with(:title, :blank, {})
99 99
     @person.valid?
100 100
   end
101 101
 
102 102
   def test_validates_presence_of_generates_message_with_custom_default_message
103 103
     Person.validates_presence_of :title, :message => 'custom'
104  
-    @person.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
  104
+    @person.errors.expects(:generate_message).with(:title, :blank, {:message => 'custom'})
105 105
     @person.valid?
106 106
   end
107 107
 
@@ -109,27 +109,27 @@ def test_validates_presence_of_generates_message_with_custom_default_message
109 109
 
110 110
   def test_validates_length_of_within_generates_message_with_title_too_short
111 111
     Person.validates_length_of :title, :within => 3..5
112  
-    @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
  112
+    @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3})
113 113
     @person.valid?
114 114
   end
115 115
 
116 116
   def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
117 117
     Person.validates_length_of :title, :within => 3..5, :too_short => 'custom'
118  
-    @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
  118
+    @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :message => 'custom'})
119 119
     @person.valid?
120 120
   end
121 121
 
122 122
   def test_validates_length_of_within_generates_message_with_title_too_long
123 123
     Person.validates_length_of :title, :within => 3..5
124 124
     @person.title = 'this title is too long'
125  
-    @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
  125
+    @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5})
126 126
     @person.valid?
127 127
   end
128 128
 
129 129
   def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
130 130
     Person.validates_length_of :title, :within => 3..5, :too_long => 'custom'
131 131
     @person.title = 'this title is too long'
132  
-    @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
  132
+    @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :message => 'custom'})
133 133
     @person.valid?
134 134
   end
135 135
 
@@ -137,13 +137,13 @@ def test_validates_length_of_within_generates_message_with_title_too_long_and_cu
137 137
 
138 138
   def test_validates_length_of_is_generates_message
139 139
     Person.validates_length_of :title, :is => 5
140  
-    @person.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => nil})
  140
+    @person.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5})
141 141
     @person.valid?
142 142
   end
143 143
 
144 144
   def test_validates_length_of_is_generates_message_with_custom_default_message
145 145
     Person.validates_length_of :title, :is => 5, :message => 'custom'
146  
-    @person.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => 'custom'})
  146
+    @person.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :message => 'custom'})
147 147
     @person.valid?
148 148
   end
149 149
 
@@ -152,14 +152,14 @@ def test_validates_length_of_is_generates_message_with_custom_default_message
152 152
   def test_validates_format_of_generates_message
153 153
     Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
154 154
     @person.title = '72x'
155  
-    @person.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => nil})
  155
+    @person.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x'})
156 156
     @person.valid?
157 157
   end
158 158
 
159 159
   def test_validates_format_of_generates_message_with_custom_default_message
160 160
     Person.validates_format_of :title, :with => /^[1-9][0-9]*$/, :message => 'custom'
161 161
     @person.title = '72x'
162  
-    @person.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => 'custom'})
  162
+    @person.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :message => 'custom'})
163 163
     @person.valid?
164 164
   end
165 165
 
@@ -168,14 +168,14 @@ def test_validates_format_of_generates_message_with_custom_default_message
168 168
   def test_validates_inclusion_of_generates_message
169 169
     Person.validates_inclusion_of :title, :in => %w(a b c)
170 170
     @person.title = 'z'
171  
-    @person.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => nil})
  171
+    @person.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z'})
172 172
     @person.valid?
173 173
   end
174 174
 
175 175
   def test_validates_inclusion_of_generates_message_with_custom_default_message
176 176
     Person.validates_inclusion_of :title, :in => %w(a b c), :message => 'custom'
177 177
     @person.title = 'z'
178  
-    @person.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => 'custom'})
  178
+    @person.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :message => 'custom'})
179 179
     @person.valid?
180 180
   end
181 181
 
@@ -184,14 +184,14 @@ def test_validates_inclusion_of_generates_message_with_custom_default_message
184 184
   def test_validates_exclusion_of_generates_message
185 185
     Person.validates_exclusion_of :title, :in => %w(a b c)
186 186
     @person.title = 'a'
187  
-    @person.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => nil})
  187
+    @person.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a'})
188 188
     @person.valid?
189 189
   end
190 190
 
191 191
   def test_validates_exclusion_of_generates_message_with_custom_default_message
192 192
     Person.validates_exclusion_of :title, :in => %w(a b c), :message => 'custom'
193 193
     @person.title = 'a'
194  
-    @person.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => 'custom'})
  194
+    @person.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :message => 'custom'})
195 195
     @person.valid?
196 196
   end
197 197
 
@@ -200,14 +200,14 @@ def test_validates_exclusion_of_generates_message_with_custom_default_message
200 200
   def test_validates_numericality_of_generates_message
201 201
     Person.validates_numericality_of :title
202 202
     @person.title = 'a'
203  
-    @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
  203
+    @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a'})
204 204
     @person.valid?
205 205
   end
206 206
 
207 207
   def test_validates_numericality_of_generates_message_with_custom_default_message
208 208
     Person.validates_numericality_of :title, :message => 'custom'
209 209
     @person.title = 'a'
210  
-    @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
  210
+    @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :message => 'custom'})
211 211
     @person.valid?
212 212
   end
213 213
 
@@ -216,14 +216,14 @@ def test_validates_numericality_of_generates_message_with_custom_default_message
216 216
   def test_validates_numericality_of_only_integer_generates_message
217 217
     Person.validates_numericality_of :title, :only_integer => true
218 218
     @person.title = '0.0'
219  
-    @person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '0.0', :default => nil})
  219
+    @person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '0.0'})
220 220
     @person.valid?
221 221
   end
222 222
 
223 223
   def test_validates_numericality_of_only_integer_generates_message_with_custom_default_message
224 224
     Person.validates_numericality_of :title, :only_integer => true, :message => 'custom'
225 225
     @person.title = '0.0'
226  
-    @person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '0.0', :default => 'custom'})
  226
+    @person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '0.0', :message => 'custom'})
227 227
     @person.valid?
228 228
   end
229 229
 
@@ -232,14 +232,14 @@ def test_validates_numericality_of_only_integer_generates_message_with_custom_de
232 232
   def test_validates_numericality_of_odd_generates_message
233 233
     Person.validates_numericality_of :title, :only_integer => true, :odd => true
234 234
     @person.title = 0
235  
-    @person.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => nil})
  235
+    @person.errors.expects(:generate_message).with(:title, :odd, {:value => 0})
236 236
     @person.valid?
237 237
   end
238 238
 
239 239
   def test_validates_numericality_of_odd_generates_message_with_custom_default_message
240 240
     Person.validates_numericality_of :title, :only_integer => true, :odd => true, :message => 'custom'
241 241
     @person.title = 0
242  
-    @person.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => 'custom'})
  242
+    @person.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :message => 'custom'})
243 243
     @person.valid?
244 244
   end
245 245
 
@@ -248,14 +248,14 @@ def test_validates_numericality_of_odd_generates_message_with_custom_default_mes
248 248
   def test_validates_numericality_of_less_than_generates_message
249 249
     Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
250 250
     @person.title = 1
251  
-    @person.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => nil})
  251
+    @person.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0})
252 252
     @person.valid?
253 253
   end
254 254
 
255 255
   def test_validates_numericality_of_less_than_odd_generates_message_with_custom_default_message
256 256
     Person.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom'
257 257
     @person.title = 1
258  
-    @person.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => 'custom'})
  258
+    @person.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :message => 'custom'})
259 259
     @person.valid?
260 260
   end
261 261
 
2  activerecord/lib/active_record/validations/associated.rb
@@ -3,7 +3,7 @@ module Validations
3 3
     class AssociatedValidator < ActiveModel::EachValidator
4 4
       def validate_each(record, attribute, value)
5 5
         return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all?
6  
-        record.errors.add(attribute, :invalid, :default => options[:message], :value => value)
  6
+        record.errors.add(attribute, :invalid, options.merge(:value => value))
7 7
       end
8 8
     end
9 9
 
2  activerecord/lib/active_record/validations/uniqueness.rb
@@ -32,7 +32,7 @@ def validate_each(record, attribute, value)
32 32
         end
33 33
 
34 34
         if relation.exists?
35  
-          record.errors.add(attribute, :taken, :default => options[:message], :value => value)
  35
+          record.errors.add(attribute, :taken, options.merge(:value => value))
36 36
         end
37 37
       end
38 38
 
12  activerecord/test/cases/validations/i18n_generate_message_validation_test.rb
@@ -9,22 +9,22 @@ def setup
9 9
     I18n.backend = I18n::Backend::Simple.new
10 10
   end
11 11
 
12  
-  # validates_associated: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
  12
+  # validates_associated: generate_message(attr_name, :invalid, :message => custom_message, :value => value)
13 13
   def test_generate_message_invalid_with_default_message
14  
-    assert_equal 'is invalid', @topic.errors.generate_message(:title, :invalid, :default => nil, :value => 'title')
  14
+    assert_equal 'is invalid', @topic.errors.generate_message(:title, :invalid, :value => 'title')
15 15
   end
16 16
 
17 17
   def test_generate_message_invalid_with_custom_message
18  
-    assert_equal 'custom message title', @topic.errors.generate_message(:title, :invalid, :default => 'custom message %{value}', :value => 'title')
  18
+    assert_equal 'custom message title', @topic.errors.generate_message(:title, :invalid, :message => 'custom message %{value}', :value => 'title')
19 19
   end
20 20
 
21  
-  # validates_uniqueness_of: generate_message(attr_name, :taken, :default => configuration[:message])
  21
+  # validates_uniqueness_of: generate_message(attr_name, :taken, :message => custom_message)
22 22
   def test_generate_message_taken_with_default_message
23  
-    assert_equal "has already been taken", @topic.errors.generate_message(:title, :taken, :default => nil, :value => 'title')
  23
+    assert_equal "has already been taken", @topic.errors.generate_message(:title, :taken, :value => 'title')
24 24
   end
25 25
 
26 26
   def test_generate_message_taken_with_custom_message
27  
-    assert_equal 'custom message title', @topic.errors.generate_message(:title, :taken, :default => 'custom message %{value}', :value => 'title')
  27
+    assert_equal 'custom message title', @topic.errors.generate_message(:title, :taken, :message => 'custom message %{value}', :value => 'title')
28 28
   end
29 29
 
30 30
   # ActiveRecord#RecordInvalid exception
8  activerecord/test/cases/validations/i18n_validation_test.rb
@@ -36,14 +36,14 @@ def replied_topic
36 36
   def test_validates_uniqueness_of_generates_message
37 37
     Topic.validates_uniqueness_of :title
38 38
     @topic.title = unique_topic.title
39  
-    @topic.errors.expects(:generate_message).with(:title, :taken, {:default => nil, :value => 'unique!'})
  39
+    @topic.errors.expects(:generate_message).with(:title, :taken, {:value => 'unique!'})
40 40
     @topic.valid?
41 41
   end
42 42
 
43 43
   def test_validates_uniqueness_of_generates_message_with_custom_default_message
44 44
     Topic.validates_uniqueness_of :title, :message => 'custom'
45 45
     @topic.title = unique_topic.title
46  
-    @topic.errors.expects(:generate_message).with(:title, :taken, {:default => 'custom', :value => 'unique!'})
  46
+    @topic.errors.expects(:generate_message).with(:title, :taken, {:message => 'custom', :value => 'unique!'})
47 47
     @topic.valid?
48 48
   end
49 49
 
@@ -51,13 +51,13 @@ def test_validates_uniqueness_of_generates_message_with_custom_default_message
51 51
 
52 52
   def test_validates_associated_generates_message
53 53
     Topic.validates_associated :replies
54  
-    replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
  54
+    replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies})
55 55
     replied_topic.valid?
56 56
   end
57 57
 
58 58
   def test_validates_associated_generates_message_with_custom_default_message
59 59
     Topic.validates_associated :replies
60  
-    replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
  60
+    replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies})
61 61
     replied_topic.valid?
62 62
   end
63 63
 

0 notes on commit bc1c8d5

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