aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/errors.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model/errors.rb')
-rw-r--r--activemodel/lib/active_model/errors.rb52
1 files changed, 39 insertions, 13 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 9efb683547..b4660f3587 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -182,25 +182,43 @@ module ActiveModel
# If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error.
def add(attribute, message = nil, options = {})
message ||= :invalid
- message = generate_message(attribute, message, options) if message.is_a?(Symbol)
+
+ validation_conditionals = [:if, :unless, :on]
+
+ message = generate_message(attribute, message, options.except(*validation_conditionals)) if message.is_a?(Symbol)
+
message = message.call if message.is_a?(Proc)
self[attribute] << message
end
# Will add an error message to each of the attributes in +attributes+ that is empty.
- def add_on_empty(attributes, custom_message = nil)
+ def add_on_empty(attributes, options = {})
+ if options && !options.is_a?(Hash)
+ options = { :message => options }
+ ActiveSupport::Deprecation.warn \
+ "ActiveModel::Errors#add_on_empty(attributes, custom_message) has been deprecated.\n" +
+ "Instead of passing a custom_message pass an options Hash { :message => custom_message }."
+ end
+
[attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
is_empty = value.respond_to?(:empty?) ? value.empty? : false
- add(attribute, :empty, :default => custom_message) unless !value.nil? && !is_empty
+ add(attribute, :empty, options) if value.nil? || is_empty
end
end
# Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).
- def add_on_blank(attributes, custom_message = nil)
+ def add_on_blank(attributes, options = {})
+ if options && !options.is_a?(Hash)
+ options = { :message => options }
+ ActiveSupport::Deprecation.warn \
+ "ActiveModel::Errors#add_on_blank(attributes, custom_message) has been deprecated.\n" +
+ "Instead of passing a custom_message pass an options Hash { :message => custom_message }."
+ end
+
[attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
- add(attribute, :blank, :default => custom_message) if value.blank?
+ add(attribute, :blank, options) if value.blank?
end
end
@@ -262,18 +280,26 @@ module ActiveModel
# <li><tt>errors.attributes.title.blank</tt></li>
# <li><tt>errors.messages.blank</tt></li>
# </ol>
- def generate_message(attribute, message = :invalid, options = {})
- message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
+
+ def generate_message(attribute, type = :invalid, options = {})
+ type = options.delete(:message) if options[:message].is_a?(Symbol)
+
+ if options[:default]
+ ActiveSupport::Deprecation.warn \
+ "ActiveModel::Errors#generate_message(attributes, custom_message) has been deprecated.\n" +
+ "Use ActiveModel::Errors#generate_message(attributes, :message => 'your message') instead."
+ options[:message] = options.delete(:default)
+ end
defaults = @base.class.lookup_ancestors.map do |klass|
- [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{message}",
- :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{message}" ]
+ [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{type}",
+ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ]
end
- defaults << options.delete(:default)
- defaults << :"#{@base.class.i18n_scope}.errors.messages.#{message}"
- defaults << :"errors.attributes.#{attribute}.#{message}"
- defaults << :"errors.messages.#{message}"
+ defaults << options.delete(:message)
+ defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}"
+ defaults << :"errors.attributes.#{attribute}.#{type}"
+ defaults << :"errors.messages.#{type}"
defaults.compact!
defaults.flatten!