diff options
author | José Valim <jose.valim@gmail.com> | 2010-06-10 19:39:09 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-06-10 19:39:09 +0200 |
commit | b67ec8ba20099c8b39bf344f385bbbd2b9c8f47d (patch) | |
tree | 008a576bb72baffd49b2483b3b08e181933e29c1 /activemodel/lib/active_model | |
parent | c4d6245e875bbb276c122a5a401422d341dac4df (diff) | |
download | rails-b67ec8ba20099c8b39bf344f385bbbd2b9c8f47d.tar.gz rails-b67ec8ba20099c8b39bf344f385bbbd2b9c8f47d.tar.bz2 rails-b67ec8ba20099c8b39bf344f385bbbd2b9c8f47d.zip |
class_attribute is not a direct replacement of class_inheritable_*.
If you are setting a hash or an array in class_attribute or you need
to freeze it, to ensure people won't modify it in place or you need
to dup it on inheritance.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 7520e0331d..d7e3544849 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -50,11 +50,10 @@ module ActiveModel extend HelperMethods include HelperMethods - define_callbacks :validate, :scope => :name - attr_accessor :validation_context + define_callbacks :validate, :scope => :name - class_inheritable_accessor :_validators + class_attribute :_validators self._validators = Hash.new { |h,k| h[k] = [] } end @@ -128,8 +127,7 @@ module ActiveModel set_callback(:validate, *args, &block) end - # List all validators that being used to validate the model using +validates_with+ - # method. + # List all validators that being used to validate the model using +validates_with+ method. def validators _validators.values.flatten.uniq end @@ -139,9 +137,17 @@ module ActiveModel _validators[attribute.to_sym] end + # Check if method is an attribute method or not. def attribute_method?(attribute) method_defined?(attribute) end + + # Copy validators on inheritance. + def inherited(base) + dup = _validators.dup + base._validators = dup.each { |k, v| dup[k] = v.dup } + super + end end # Returns the Errors object that holds all information about attribute error messages. |