aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations/length.rb
diff options
context:
space:
mode:
authorJeroen van Dijk <jeroen@jeevidee.nl>2010-05-19 15:25:46 +0100
committerJosé Valim <jose.valim@gmail.com>2010-06-21 11:55:21 +0200
commit26392c4ac57e27c63984d47c6326c13f502d5786 (patch)
treed3ee41f6eb5f8a394d059097629d84365936a995 /activemodel/lib/active_model/validations/length.rb
parentead72b319f781ae3767a8695e3e29e9249388f7f (diff)
downloadrails-26392c4ac57e27c63984d47c6326c13f502d5786.tar.gz
rails-26392c4ac57e27c63984d47c6326c13f502d5786.tar.bz2
rails-26392c4ac57e27c63984d47c6326c13f502d5786.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). Also, refactoring of ActiveModel and ActiveRecord Validation tests. Test are a lot more DRY now. Better test coverage as well now. The first four points were reapplied from an older patch of Sven Fuchs which didn't apply cleanly anymore and was not complete yet. Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activemodel/lib/active_model/validations/length.rb')
-rw-r--r--activemodel/lib/active_model/validations/length.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb
index dc191d3150..77db437a33 100644
--- a/activemodel/lib/active_model/validations/length.rb
+++ b/activemodel/lib/active_model/validations/length.rb
@@ -39,7 +39,8 @@ module ActiveModel
CHECKS.each do |key, validity_check|
next unless check_value = options[key]
- custom_message = options[:message] || options[MESSAGES[key]]
+ default_message = options[MESSAGES[key]]
+ options[:message] ||= default_message if default_message
valid_value = if key == :maximum
value.nil? || value.size.send(validity_check, check_value)
@@ -48,7 +49,10 @@ module ActiveModel
end
next if valid_value
- record.errors.add(attribute, MESSAGES[key], :default => custom_message, :count => check_value)
+
+ reserved_options = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
+ record.errors.add(attribute, MESSAGES[key],
+ options.except(*reserved_options).merge(:count => check_value))
end
end
end