From 8f97e9d19abf02b33c5f7c0c1f1d5daf13e28893 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Thu, 18 Feb 2010 22:28:48 +0700 Subject: Add validators reflection so you can do 'Person.validators' and 'Person.validators_on(:name)'. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../test/cases/validations/with_validation_test.rb | 1 + activemodel/test/cases/validations_test.rb | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) (limited to 'activemodel/test') 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 -- cgit v1.2.3