Permalink
Browse files

making valid? no longer run all validations

  • Loading branch information...
1 parent f74b324 commit d3c5f87c449c7b32b69bc059acef90a81e686829 jaycfields committed Jun 9, 2007
Showing with 18 additions and 6 deletions.
  1. +3 −3 lib/validatable_instance_methods.rb
  2. +14 −2 test/functional/validatable_test.rb
  3. +1 −1 test/unit/validatable_test.rb
@@ -7,7 +7,7 @@ def self.included(klass) #:nodoc:
#
# Returns true if no errors were added otherwise false.
def valid?
- valid_for_group?
+ valid_for_group?(nil)
end
# call-seq: errors
@@ -17,7 +17,7 @@ def errors
@errors ||= Validatable::Errors.new
end
- def valid_for_group?(group=nil) #:nodoc:
+ def valid_for_group?(group) #:nodoc:
errors.clear
self.class.validate_children(self, group)
self.validate(group)
@@ -65,7 +65,7 @@ def add_error(attribute, message) #:nodoc:
def validations_for_level_and_group(level, group) #:nodoc:
validations_for_level = self.class.validations.select { |validation| validation.level == level }
- return validations_for_level if group.nil?
+ return validations_for_level.select { |validation| validation.groups.empty? } if group.nil?
validations_for_level.select { |validation| validation.groups.include?(group) }
end
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
module Functional
- class ValidatableTest < Test::Unit::TestCase
+ class ValidatableTest < Test::Unit::TestCase
expect ArgumentError do
Class.new do
@@ -227,7 +227,19 @@ class ValidatableTest < Test::Unit::TestCase
instance.valid_for_group_one?
end
- expect false do
+ expect true do
+ klass = Class.new do
+ include Validatable
+ validates_presence_of :name, :groups => :group_one
+ validates_presence_of :address
+ attr_accessor :name, :address
+ end
+ instance = klass.new
+ instance.address = 'anything'
+ instance.valid?
+ end
+
+ expect true do
klass = Class.new do
include Validatable
validates_presence_of :name, :groups => :group_one
@@ -3,7 +3,7 @@
module Unit
class ValidatableTest < Test::Unit::TestCase
expect false do
- validation = stub_everything(:valid? => false, :should_validate? => true, :attribute => "attribute", :level => 1)
+ validation = stub_everything(:should_validate? => true, :attribute => "attribute", :level => 1, :groups => [])
klass = Class.new do
include Validatable
validations << validation

0 comments on commit d3c5f87

Please sign in to comment.