From e11bb95d56ed77b10d54d9dfe5a3cc4aa48b3a61 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 9 Jun 2010 23:07:54 -0400 Subject: Validators should at model level and not at AR:Base level [Closes #4804] [#4804 state:resolved] Signed-off-by: David Heinemeier Hansson --- activemodel/lib/active_model/validations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index f472f50f9b..7520e0331d 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -54,7 +54,7 @@ module ActiveModel attr_accessor :validation_context - class_attribute :_validators + class_inheritable_accessor :_validators self._validators = Hash.new { |h,k| h[k] = [] } end -- cgit v1.2.3 From b67ec8ba20099c8b39bf344f385bbbd2b9c8f47d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 10 Jun 2010 19:39:09 +0200 Subject: 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. --- activemodel/lib/active_model/validations.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'activemodel') 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. -- cgit v1.2.3