diff options
author | Obie Fernandez <obie@hashrocket.com> | 2010-09-20 21:35:41 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-24 12:49:16 +0200 |
commit | 275f922a23b780600a32f70de5b661371c8ffdf4 (patch) | |
tree | a49898a3284e4508768eefa9567e0cf105786f7a /activemodel/test | |
parent | 097240f60215b866d24aebd02cc4159bdc6e7451 (diff) | |
download | rails-275f922a23b780600a32f70de5b661371c8ffdf4.tar.gz rails-275f922a23b780600a32f70de5b661371c8ffdf4.tar.bz2 rails-275f922a23b780600a32f70de5b661371c8ffdf4.zip |
Better shortcut options for custom validators [#5672 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/validations/validates_test.rb | 9 | ||||
-rw-r--r-- | activemodel/test/models/person_with_validator.rb | 13 |
2 files changed, 22 insertions, 0 deletions
diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index db023f6169..666c48c8a0 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -111,4 +111,13 @@ class ValidatesTest < ActiveModel::TestCase person.valid? assert_equal ['Local validator please'], person.errors[:title] end + + def test_validates_with_included_validator_and_wildcard_shortcut + # Shortcut for PersonWithValidator.validates :title, :like => { :with => "Mr." } + PersonWithValidator.validates :title, :like => "Mr." + person = PersonWithValidator.new + person.title = "Ms. Pacman" + person.valid? + assert_equal ['does not appear to be like Mr.'], person.errors[:title] + end end diff --git a/activemodel/test/models/person_with_validator.rb b/activemodel/test/models/person_with_validator.rb index f6f665ccee..505ed880c1 100644 --- a/activemodel/test/models/person_with_validator.rb +++ b/activemodel/test/models/person_with_validator.rb @@ -7,5 +7,18 @@ class PersonWithValidator end end + class LikeValidator < ActiveModel::EachValidator + def initialize(options) + @with = options[:with] + super + end + + def validate_each(record, attribute, value) + unless value[@with] + record.errors.add attribute, "does not appear to be like #{@with}" + end + end + end + attr_accessor :title, :karma end |