aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2011-02-05 20:27:02 -0800
committerCarl Lerche <me@carllerche.com>2011-02-05 20:27:02 -0800
commitcf9324e5909e71ec0a2477338e696b6af2f17f13 (patch)
tree21658c73bdc72de2814032252f6ab9a3b512978e /activemodel
parentcd13fbd8d8071b822f6d4f8967ef80c617c036ba (diff)
downloadrails-cf9324e5909e71ec0a2477338e696b6af2f17f13.tar.gz
rails-cf9324e5909e71ec0a2477338e696b6af2f17f13.tar.bz2
rails-cf9324e5909e71ec0a2477338e696b6af2f17f13.zip
Find all validators for multiple attributes
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/validations.rb6
-rw-r--r--activemodel/test/cases/validations_test.rb18
2 files changed, 22 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index cdf23c7b1b..a0f90452b3 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -146,8 +146,10 @@ module ActiveModel
end
# List all validators that being used to validate a specific attribute.
- def validators_on(attribute)
- _validators[attribute.to_sym]
+ def validators_on(*attributes)
+ attributes.inject([]) do |all, attribute|
+ all |= _validators[attribute.to_sym] || []
+ end
end
# Check if method is an attribute method or not.
diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index e90dc7d4e3..2f36195627 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -254,6 +254,24 @@ class ValidationsTest < ActiveModel::TestCase
assert_equal 10, Topic.validators_on(:title).first.options[:minimum]
end
+ def test_list_of_validators_on_multiple_attributes
+ Topic.validates :title, :length => { :minimum => 10 }
+ Topic.validates :author_name, :presence => true, :format => /a/
+
+ validators = Topic.validators_on(:title, :author_name)
+
+ assert_equal [
+ ActiveModel::Validations::FormatValidator,
+ ActiveModel::Validations::LengthValidator,
+ ActiveModel::Validations::PresenceValidator
+ ], validators.map { |v| v.class }.sort_by { |c| c.to_s }
+ end
+
+ def test_list_of_validators_will_be_empty_when_empty
+ Topic.validates :title, :length => { :minimum => 10 }
+ assert_equal [], Topic.validators_on(:author_name)
+ end
+
def test_validations_on_the_instance_level
auto = Automobile.new