aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test
diff options
context:
space:
mode:
authorPrem Sichanugrist <s@sikachu.com>2010-02-18 22:28:48 +0700
committerJosé Valim <jose.valim@gmail.com>2010-02-21 12:37:46 +0100
commit8f97e9d19abf02b33c5f7c0c1f1d5daf13e28893 (patch)
tree3858a21a27d5c83e67b28722a3b7b9d62c16efa9 /activemodel/test
parent250c8092461f5e6bf62751b313f6605a37fd1b2b (diff)
downloadrails-8f97e9d19abf02b33c5f7c0c1f1d5daf13e28893.tar.gz
rails-8f97e9d19abf02b33c5f7c0c1f1d5daf13e28893.tar.bz2
rails-8f97e9d19abf02b33c5f7c0c1f1d5daf13e28893.zip
Add validators reflection so you can do 'Person.validators' and 'Person.validators_on(:name)'.
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activemodel/test')
-rw-r--r--activemodel/test/cases/validations/with_validation_test.rb1
-rw-r--r--activemodel/test/cases/validations_test.rb27
2 files changed, 28 insertions, 0 deletions
diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb
index 66b072ea38..92df4dd6cd 100644
--- a/activemodel/test/cases/validations/with_validation_test.rb
+++ b/activemodel/test/cases/validations/with_validation_test.rb
@@ -9,6 +9,7 @@ class ValidatesWithTest < ActiveRecord::TestCase
def teardown
Topic.reset_callbacks(:validate)
+ Topic._validators.clear
end
ERROR_MESSAGE = "Validation error from validator"
diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index eb100d1c35..9fedd84c73 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -10,6 +10,10 @@ require 'models/custom_reader'
class ValidationsTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase
+ def setup
+ Topic._validators.clear
+ end
+
# Most of the tests mess with the validations of Topic, so lets repair it all the time.
# Other classes we mess with will be dealt with in the specific tests
def teardown
@@ -220,4 +224,27 @@ class ValidationsTest < ActiveModel::TestCase
assert !t.valid?
assert ["NO BLANKS HERE"], t.errors[:title]
end
+
+ def test_list_of_validators_for_model
+ Topic.validates_presence_of :title
+ Topic.validates_length_of :title, :minimum => 2
+
+ assert_equal 2, Topic.validators.count
+ assert_equal [:presence, :length], Topic.validators.map(&:kind)
+ end
+
+ def test_list_of_validators_on_an_attribute
+ Topic.validates_presence_of :title, :content
+ Topic.validates_length_of :title, :minimum => 2
+
+ assert_equal 2, Topic.validators_on(:title).count
+ assert_equal [:presence, :length], Topic.validators_on(:title).map(&:kind)
+ assert_equal 1, Topic.validators_on(:content).count
+ assert_equal [:presence], Topic.validators_on(:content).map(&:kind)
+ end
+
+ def test_accessing_instance_of_validator_on_an_attribute
+ Topic.validates_length_of :title, :minimum => 10
+ assert_equal 10, Topic.validators_on(:title).first.options[:minimum]
+ end
end