diff options
author | Samuel Kadolph <samuel@kadolph.com> | 2010-12-09 13:30:02 -0500 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2010-12-11 16:35:18 -0200 |
commit | 2650742bd02e108bc4ccdc59efa54b4916e3a443 (patch) | |
tree | ed314872ec7bbe9435922a72e8aff14e0cb6edd2 | |
parent | 307443972c5f6de959a5401eec76ca327484b10c (diff) | |
download | rails-2650742bd02e108bc4ccdc59efa54b4916e3a443.tar.gz rails-2650742bd02e108bc4ccdc59efa54b4916e3a443.tar.bz2 rails-2650742bd02e108bc4ccdc59efa54b4916e3a443.zip |
Add support for namespaced validators
Includes test and documentation for new feature
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
-rw-r--r-- | activemodel/lib/active_model/validations/validates.rb | 8 | ||||
-rw-r--r-- | activemodel/test/cases/validations/validates_test.rb | 8 | ||||
-rwxr-xr-x | activemodel/test/validators/namespace/email_validator.rb | 6 |
3 files changed, 21 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index 77c5073c6e..0132f68282 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -55,6 +55,10 @@ module ActiveModel # validates :name, :title => true # end # + # Additionally validator classes may be in another namespace and still used within any class. + # + # validates :name, :'file/title' => true + # # The validators hash can also handle regular expressions, ranges, # arrays and strings in shortcut form, e.g. # @@ -86,8 +90,10 @@ module ActiveModel defaults.merge!(:attributes => attributes) validations.each do |key, options| + key = "#{key.to_s.camelize}Validator" + begin - validator = const_get("#{key.to_s.camelize}Validator") + validator = key.include?('::') ? key.constantize : const_get(key) rescue NameError raise ArgumentError, "Unknown validator: '#{key}'" end diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 666c48c8a0..3a9900939e 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -3,6 +3,7 @@ require 'cases/helper' require 'models/person' require 'models/person_with_validator' require 'validators/email_validator' +require 'validators/namespace/email_validator' class ValidatesTest < ActiveModel::TestCase setup :reset_callbacks @@ -34,6 +35,13 @@ class ValidatesTest < ActiveModel::TestCase assert_equal ['is not an email'], person.errors[:karma] end + def test_validates_with_namespaced_validator_class + Person.validates :karma, :'namespace/email' => true + person = Person.new + person.valid? + assert_equal ['is not an email'], person.errors[:karma] + end + def test_validates_with_if_as_local_conditions Person.validates :karma, :presence => true, :email => { :unless => :condition_is_true } person = Person.new diff --git a/activemodel/test/validators/namespace/email_validator.rb b/activemodel/test/validators/namespace/email_validator.rb new file mode 100755 index 0000000000..57e2793ce2 --- /dev/null +++ b/activemodel/test/validators/namespace/email_validator.rb @@ -0,0 +1,6 @@ +require 'validators/email_validator' + +module Namespace + class EmailValidator < ::EmailValidator + end +end |