Skip to content

Commit d6cbb27

Browse files
committed
Revert "Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an options hash and make various Validators pass their (filtered) options."
Having a huge array to whitelist options is not the proper way to handle this case. This means that the ActiveModel::Errors object should know about the options given in *all* validators and break the extensibility added by the validators itself. If the intent is to whitelist options before sending them to I18n, each validator should clean its respective options instead of throwing the responsibility to the Errors object. This reverts commit bc1c8d5.
1 parent f055bc0 commit d6cbb27

File tree

15 files changed

+111
-139
lines changed

15 files changed

+111
-139
lines changed

activemodel/lib/active_model/errors.rb

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -179,45 +179,25 @@ def to_xml(options={})
179179
# If +message+ is a Proc, it will be called, allowing for things like Time.now to be used within an error
180180
def add(attribute, message = nil, options = {})
181181
message ||= :invalid
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-
182+
message = generate_message(attribute, message, options) if message.is_a?(Symbol)
189183
message = message.call if message.is_a?(Proc)
190184
self[attribute] << message
191185
end
192186

193187
# Will add an error message to each of the attributes in +attributes+ that is empty.
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-
188+
def add_on_empty(attributes, custom_message = nil)
202189
[attributes].flatten.each do |attribute|
203190
value = @base.send(:read_attribute_for_validation, attribute)
204191
is_empty = value.respond_to?(:empty?) ? value.empty? : false
205-
add(attribute, :empty, options) if value.nil? || is_empty
192+
add(attribute, :empty, :default => custom_message) unless !value.nil? && !is_empty
206193
end
207194
end
208195

209196
# Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).
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-
197+
def add_on_blank(attributes, custom_message = nil)
218198
[attributes].flatten.each do |attribute|
219199
value = @base.send(:read_attribute_for_validation, attribute)
220-
add(attribute, :blank, options) if value.blank?
200+
add(attribute, :blank, :default => custom_message) if value.blank?
221201
end
222202
end
223203

@@ -274,26 +254,18 @@ def full_messages
274254
# <li><tt>errors.attributes.title.blank</tt></li>
275255
# <li><tt>errors.messages.blank</tt></li>
276256
# </ol>
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
257+
def generate_message(attribute, message = :invalid, options = {})
258+
message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
287259

288260
defaults = @base.class.lookup_ancestors.map do |klass|
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}" ]
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}" ]
291263
end
292264

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}"
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}"
297269

298270
defaults.compact!
299271
defaults.flatten!

activemodel/lib/active_model/validations/acceptance.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def initialize(options)
77

88
def validate_each(record, attribute, value)
99
unless value == options[:accept]
10-
record.errors.add(attribute, :accepted, options)
10+
record.errors.add(attribute, :accepted, :default => options[:message])
1111
end
1212
end
1313

activemodel/lib/active_model/validations/confirmation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ConfirmationValidator < EachValidator
44
def validate_each(record, attribute, value)
55
confirmed = record.send(:"#{attribute}_confirmation")
66
return if confirmed.nil? || value == confirmed
7-
record.errors.add(attribute, :confirmation, options)
7+
record.errors.add(attribute, :confirmation, :default => options[:message])
88
end
99

1010
def setup(klass)

activemodel/lib/active_model/validations/exclusion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def check_validity!
88

99
def validate_each(record, attribute, value)
1010
return unless options[:in].include?(value)
11-
record.errors.add(attribute, :exclusion, options.merge(:value => value))
11+
record.errors.add(attribute, :exclusion, :default => options[:message], :value => value)
1212
end
1313
end
1414

activemodel/lib/active_model/validations/format.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module Validations
33
class FormatValidator < EachValidator
44
def validate_each(record, attribute, value)
55
if options[:with] && value.to_s !~ options[:with]
6-
record.errors.add(attribute, :invalid, options.merge(:value => value))
6+
record.errors.add(attribute, :invalid, :default => options[:message], :value => value)
77
elsif options[:without] && value.to_s =~ options[:without]
8-
record.errors.add(attribute, :invalid, options.merge(:value => value))
8+
record.errors.add(attribute, :invalid, :default => options[:message], :value => value)
99
end
1010
end
1111

activemodel/lib/active_model/validations/inclusion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def check_validity!
88

99
def validate_each(record, attribute, value)
1010
return if options[:in].include?(value)
11-
record.errors.add(attribute, :inclusion, options.merge(:value => value))
11+
record.errors.add(attribute, :inclusion, :default => options[:message], :value => value)
1212
end
1313
end
1414

activemodel/lib/active_model/validations/length.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def validate_each(record, attribute, value)
3737

3838
CHECKS.each do |key, validity_check|
3939
next unless check_value = options[key]
40-
options[:message] ||= options[MESSAGES[key]] if options[MESSAGES[key]]
40+
custom_message = options[:message] || options[MESSAGES[key]]
4141

4242
valid_value = if key == :maximum
4343
value.nil? || value.size.send(validity_check, check_value)
@@ -46,7 +46,7 @@ def validate_each(record, attribute, value)
4646
end
4747

4848
next if valid_value
49-
record.errors.add(attribute, MESSAGES[key], options.merge(:count => check_value))
49+
record.errors.add(attribute, MESSAGES[key], :default => custom_message, :count => check_value)
5050
end
5151
end
5252
end

activemodel/lib/active_model/validations/numericality.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ def validate_each(record, attr_name, value)
2626
return if options[:allow_nil] && raw_value.nil?
2727

2828
unless value = parse_raw_value_as_a_number(raw_value)
29-
record.errors.add(attr_name, :not_a_number, options.merge(:value => raw_value))
29+
record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => options[:message])
3030
return
3131
end
3232

3333
if options[:only_integer]
3434
unless value = parse_raw_value_as_an_integer(raw_value)
35-
record.errors.add(attr_name, :not_an_integer, options.merge(:value => raw_value))
35+
record.errors.add(attr_name, :not_an_integer, :value => raw_value, :default => options[:message])
3636
return
3737
end
3838
end
@@ -41,14 +41,14 @@ def validate_each(record, attr_name, value)
4141
case option
4242
when :odd, :even
4343
unless value.to_i.send(CHECKS[option])
44-
record.errors.add(attr_name, option, options.merge(:value => value))
44+
record.errors.add(attr_name, option, :value => value, :default => options[:message])
4545
end
4646
else
4747
option_value = option_value.call(record) if option_value.is_a?(Proc)
4848
option_value = record.send(option_value) if option_value.is_a?(Symbol)
4949

5050
unless value.send(CHECKS[option], option_value)
51-
record.errors.add(attr_name, option, options.merge(:value => value, :count => option_value))
51+
record.errors.add(attr_name, option, :default => options[:message], :value => value, :count => option_value)
5252
end
5353
end
5454
end

activemodel/lib/active_model/validations/presence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ActiveModel
44
module Validations
55
class PresenceValidator < EachValidator
66
def validate(record)
7-
record.errors.add_on_blank(attributes, options)
7+
record.errors.add_on_blank(attributes, options[:message])
88
end
99
end
1010

0 commit comments

Comments
 (0)