aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/errors.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-05-15 21:55:16 +0200
committerJosé Valim <jose.valim@gmail.com>2010-05-15 21:55:16 +0200
commitd6cbb27e7b260c970bf7d07dc0b0591ed82cee2a (patch)
treeccd57aa2f19b43943ae6fb8e34fbb34b3a673726 /activemodel/lib/active_model/errors.rb
parentf055bc05d515b80c89b99b775546b954f270bc5c (diff)
downloadrails-d6cbb27e7b260c970bf7d07dc0b0591ed82cee2a.tar.gz
rails-d6cbb27e7b260c970bf7d07dc0b0591ed82cee2a.tar.bz2
rails-d6cbb27e7b260c970bf7d07dc0b0591ed82cee2a.zip
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 bc1c8d58ec45593acba614d1d0fecb49adef08ff.
Diffstat (limited to 'activemodel/lib/active_model/errors.rb')
-rw-r--r--activemodel/lib/active_model/errors.rb54
1 files changed, 13 insertions, 41 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index b18b62f926..14afc5265f 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -179,45 +179,25 @@ module ActiveModel
# If +message+ is a Proc, it will be called, allowing for things like Time.now to be used within an error
def add(attribute, message = nil, options = {})
message ||= :invalid
-
- reserved = [:minimum, :maximum, :is, :within , :in, :allow_nil, :allow_blank, :case_sensitive,
- :too_long, :too_short, :wrong_length, :on, :if, :unless , :tokenizer, :invalid,
- :only_integer, :odd, :even, :less_than, :with, :accept]
-
- message = generate_message(attribute, message, options.except(*reserved)) if message.is_a?(Symbol)
-
+ message = generate_message(attribute, message, options) 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, 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
-
+ def add_on_empty(attributes, custom_message = nil)
[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, options) if value.nil? || is_empty
+ add(attribute, :empty, :default => custom_message) unless !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, 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
-
+ def add_on_blank(attributes, custom_message = nil)
[attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
- add(attribute, :blank, options) if value.blank?
+ add(attribute, :blank, :default => custom_message) if value.blank?
end
end
@@ -274,26 +254,18 @@ module ActiveModel
# <li><tt>errors.attributes.title.blank</tt></li>
# <li><tt>errors.messages.blank</tt></li>
# </ol>
-
- 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
+ def generate_message(attribute, message = :invalid, options = {})
+ message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
defaults = @base.class.lookup_ancestors.map do |klass|
- [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{type}",
- :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ]
+ [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{message}",
+ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{message}" ]
end
- defaults << options.delete(:message)
- defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}"
- defaults << :"errors.attributes.#{attribute}.#{type}"
- defaults << :"errors.messages.#{type}"
+ defaults << options.delete(:default)
+ defaults << :"#{@base.class.i18n_scope}.errors.messages.#{message}"
+ defaults << :"errors.attributes.#{attribute}.#{message}"
+ defaults << :"errors.messages.#{message}"
defaults.compact!
defaults.flatten!