Permalink
Browse files

Fix some validators when used on model instance

Now that Validator #setup is called from the initializer, we need a
reference to the model's class to be passed in to allow the validators
to continue functioning when used at the instance level.

Closes #14134.
  • Loading branch information...
1 parent c20fe91 commit 088c11658aa6b79d05e9014201d585c7700669aa @ehutzelman ehutzelman committed with carlosantoniodasilva Feb 21, 2014
View
2 activemodel/lib/active_model/validations/with.rb
@@ -139,6 +139,8 @@ def validates_with(*args, &block)
# class version of this method for more information.
def validates_with(*args, &block)
options = args.extract_options!
+ options[:class] = self.class
+
args.each do |klass|
validator = klass.new(options, &block)
validator.validate(self)
View
3 activemodel/test/cases/validations_test.rb
@@ -284,10 +284,11 @@ def test_validations_on_the_instance_level
auto = Automobile.new
assert auto.invalid?
- assert_equal 2, auto.errors.size
+ assert_equal 3, auto.errors.size
auto.make = 'Toyota'
auto.model = 'Corolla'
+ auto.approved = '1'
assert auto.valid?
end
View
3 activemodel/test/models/automobile.rb
@@ -3,10 +3,11 @@ class Automobile
validate :validations
- attr_accessor :make, :model
+ attr_accessor :make, :model, :approved
def validations
validates_presence_of :make
validates_length_of :model, within: 2..10
+ validates_acceptance_of :approved, allow_nil: false
end
end

0 comments on commit 088c116

Please sign in to comment.