Skip to content

Commit bc1c8d5

Browse files
jeroenvandijkjeremy
authored andcommitted
Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an options 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 [rails#4057 state:committed] Signed-off-by: Jeremy Kemper <[email protected]>
1 parent 47c9a35 commit bc1c8d5

File tree

15 files changed

+139
-111
lines changed

15 files changed

+139
-111
lines changed

activemodel/lib/active_model/errors.rb

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,25 +179,45 @@ 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-
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+
183189
message = message.call if message.is_a?(Proc)
184190
self[attribute] << message
185191
end
186192

187193
# 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+
189202
[attributes].flatten.each do |attribute|
190203
value = @base.send(:read_attribute_for_validation, attribute)
191204
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
193206
end
194207
end
195208

196209
# 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+
198218
[attributes].flatten.each do |attribute|
199219
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?
201221
end
202222
end
203223

@@ -254,18 +274,26 @@ def full_messages
254274
# <li><tt>errors.attributes.title.blank</tt></li>
255275
# <li><tt>errors.messages.blank</tt></li>
256276
# </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
259287

260288
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}" ]
263291
end
264292

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

270298
defaults.compact!
271299
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, :default => options[:message])
10+
record.errors.add(attribute, :accepted, options)
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, :default => options[:message])
7+
record.errors.add(attribute, :confirmation, options)
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, :default => options[:message], :value => value)
11+
record.errors.add(attribute, :exclusion, options.merge(: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, :default => options[:message], :value => value)
6+
record.errors.add(attribute, :invalid, options.merge(:value => value))
77
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))
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, :default => options[:message], :value => value)
11+
record.errors.add(attribute, :inclusion, options.merge(: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-
custom_message = options[:message] || options[MESSAGES[key]]
40+
options[:message] ||= options[MESSAGES[key]] if 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], :default => custom_message, :count => check_value)
49+
record.errors.add(attribute, MESSAGES[key], options.merge(: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, :value => raw_value, :default => options[:message])
29+
record.errors.add(attr_name, :not_a_number, options.merge(:value => raw_value))
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, :value => raw_value, :default => options[:message])
35+
record.errors.add(attr_name, :not_an_integer, options.merge(:value => raw_value))
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, :value => value, :default => options[:message])
44+
record.errors.add(attr_name, option, options.merge(:value => value))
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, :default => options[:message], :value => value, :count => option_value)
51+
record.errors.add(attr_name, option, options.merge(: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[:message])
7+
record.errors.add_on_blank(attributes, options)
88
end
99
end
1010

0 commit comments

Comments
 (0)