aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/errors.rb
diff options
context:
space:
mode:
authorJeroen van Dijk <jeroen@jeevidee.nl>2010-05-15 19:43:45 +0200
committerJeremy Kemper <jeremy@bitsweat.net>2010-05-15 11:17:58 -0700
commitbc1c8d58ec45593acba614d1d0fecb49adef08ff (patch)
tree5515d6211f61cc86804a6580b9d2ec1c710b9db0 /activemodel/lib/active_model/errors.rb
parent47c9a355062888feb2c7ea7c794e914a9b78f50c (diff)
downloadrails-bc1c8d58ec45593acba614d1d0fecb49adef08ff.tar.gz
rails-bc1c8d58ec45593acba614d1d0fecb49adef08ff.tar.bz2
rails-bc1c8d58ec45593acba614d1d0fecb49adef08ff.zip
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 [#4057 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activemodel/lib/active_model/errors.rb')
-rw-r--r--activemodel/lib/active_model/errors.rb54
1 files changed, 41 insertions, 13 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 14afc5265f..b18b62f926 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -179,25 +179,45 @@ 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
- message = generate_message(attribute, message, options) if message.is_a?(Symbol)
+
+ 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 = 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
@@ -254,18 +274,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!