diff options
author | Prem Sichanugrist <s@sikachu.com> | 2010-02-18 22:28:48 +0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-02-21 12:37:46 +0100 |
commit | 8f97e9d19abf02b33c5f7c0c1f1d5daf13e28893 (patch) | |
tree | 3858a21a27d5c83e67b28722a3b7b9d62c16efa9 /activemodel/lib/active_model/validations.rb | |
parent | 250c8092461f5e6bf62751b313f6605a37fd1b2b (diff) | |
download | rails-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/lib/active_model/validations.rb')
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 03733a9c89..7f6748a660 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/array/extract_options' +require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/hash/keys' require 'active_model/errors' @@ -45,6 +46,9 @@ module ActiveModel included do extend ActiveModel::Translation define_callbacks :validate, :scope => :name + + class_attribute :_validators + self._validators = Hash.new { |h,k| h[k] = [] } end module ClassMethods @@ -117,9 +121,20 @@ module ActiveModel end set_callback(:validate, *args, &block) end - - private - + + # List all validators that being used to validate the model using +validates_with+ + # method. + def validators + _validators.values.flatten.uniq + end + + # List all validators that being used to validate a specific attribute. + def validators_on(attribute) + _validators[attribute.to_sym] + end + + private + def _merge_attributes(attr_names) options = attr_names.extract_options! options.merge(:attributes => attr_names) |