Permalink
Browse files

added a should_be_valid_except method, with tests. Also added extra t…

…ests for multiple attributes and expanded README.
  • Loading branch information...
1 parent d1164b5 commit 3485f4f1616dd5fc9a3a5c59d7bd3a9564c3c73e @DrMark DrMark committed Jul 3, 2008
Showing with 61 additions and 1 deletion.
  1. +8 −0 README
  2. +15 −0 lib/partially_valid/core.rb
  3. +38 −1 test/partially_valid_test.rb
View
8 README
@@ -19,6 +19,9 @@ Assume, for the purposes of this example that the name, age and phone number fie
bill.should_be_partially_valid_on :phone_number
bill.partially_valid? #=> false (fails now because phone number is checked)
+ bill.should_be_partially_valid_except :phone_number
+ bill.partially_valid? #=> true (passes now because phone number is not checked)
+
h2. Notes
After invoking :partially_valid?, the errors object will contain
@@ -28,3 +31,8 @@ partial validation.
The list of partially valid attributes are stored on the object itself. The object may be marshalled into session data and restored and will remember the attributes that have been declared to be checked for validity. However, the list of valid attributes are not persisted to the database. Generally, this is not a problem, because a model that is only partially valid cannot be saved to the database anyways.
The :valid? method continues to work as before.
+
+h3. Requirements
+
+The flexmock plugin.
+ > gem install flexmock
View
15 lib/partially_valid/core.rb
@@ -41,6 +41,21 @@ def should_be_partially_valid_on(*attrs)
partially_valid_attributes.uniq!
end
+ # Declare that the list of attributes that should not be added to the list
+ # checked for partial validation. The attribute names may be
+ # strings or symbols. All other attributes will be validated.
+ def should_be_partially_valid_except(*attrs)
+ invalid_attributes = []
+ all_attrs = self.instance_variables.map {|iv| iv.gsub('@','')}
+ # use the key names from @attributes for ActiveRecord objects
+ all_attrs = self.attributes.keys if self.instance_variables.include?('attributes')
+ attrs.each do |attr_name|
+ invalid_attributes << attr_name.to_s
+ end
+ should_be_valid_on = all_attrs - invalid_attributes
+ should_be_partially_valid_on(should_be_valid_on)
+ end
+
# Is the model valid on the attributes declared to be checked for
# partial validation? Errors on non-checked attributes are removed
# from the error list not not considered as errors for the purpose
View
39 test/partially_valid_test.rb
@@ -25,11 +25,17 @@ def each(&block)
end
class FauxModel < ActiveRecord::Base
- attr_reader :errors
+ attr_reader :errors, :x, :y
+
+ def initialize
+ @x = 1
+ @y = 2
+ end
def valid?
@errors = FauxErrors.new
@errors.add("x", "bad value")
+ @errors.add("y", "bad value")
@errors.empty?
end
end
@@ -39,6 +45,7 @@ def test_faux_model_has_errors
model = FauxModel.new
assert ! model.valid?, "Faux Model should have errors"
assert_equal "bad value", model.errors.on(:x)
+ assert_equal "bad value", model.errors.on(:y)
end
def test_model_with_no_pv_attributes_is_partially_valid
@@ -50,14 +57,44 @@ def test_model_with_no_pv_attributes_is_partially_valid
def test_model_with_pv_attributes_is_not_partially_valid
model = FauxModel.new
model.should_be_partially_valid_on(:x)
+ model.should_be_partially_valid_on(:y)
assert ! model.partially_valid?, "should not be partially valid"
assert ! model.errors.empty?
assert_equal "bad value", model.errors.on(:x)
+ assert_equal "bad value", model.errors.on(:y)
+ end
+
+ def test_model_with_multiple_pv_attributes_is_not_partially_valid
+ model = FauxModel.new
+ model.should_be_partially_valid_on(:x,:y)
+ assert ! model.partially_valid?, "should not be partially valid"
+ assert ! model.errors.empty?
+ assert_equal "bad value", model.errors.on(:x)
+ assert_equal "bad value", model.errors.on(:y)
+ end
+
+ def test_model_with_some_pv_attributes_is_not_partially_valid
+ model = FauxModel.new
+ model.should_be_partially_valid_on(:y)
+ assert ! model.partially_valid?, "should not be partially valid"
+ assert ! model.errors.empty?
+ assert_equal nil, model.errors.on(:x)
+ assert_equal "bad value", model.errors.on(:y)
end
+ def test_model_with_some_pv_attributes_is_not_partially_valid_using_except
+ model = FauxModel.new
+ model.should_be_partially_valid_except(:y)
+ assert ! model.partially_valid?, "should not be partially valid"
+ assert ! model.errors.empty?
+ assert_equal "bad value", model.errors.on(:x)
+ assert_equal nil, model.errors.on(:y)
+ end
+
def test_model_can_clear_pv_attributes
model = FauxModel.new
model.should_be_partially_valid_on(:x)
+ model.should_be_partially_valid_on(:y)
model.partially_valid_clear
assert model.partially_valid?, "should be partially valid"
end

0 comments on commit 3485f4f

Please sign in to comment.