Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial version

  • Loading branch information...
commit 755ac09809889294769ef6bdfc89ebffe7b37d7d 0 parents
@jimweirich authored
12 Rakefile
@@ -0,0 +1,12 @@
+#!/usr/bin/env ruby
+
+require 'rake/testtask'
+require 'rake/clean'
+
+task :default => :test
+
+Rake::TestTask.new(:test) do |t|
+ t.verbose = true
+ t.warning = true
+ t.test_files = FileList['test/*_test.rb']
+end
1  init.rb
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + '/lib/partially_valid'
2  lib/partially_valid.rb
@@ -0,0 +1,2 @@
+require File.dirname(__FILE__) + '/partially_valid/core'
+require File.dirname(__FILE__) + '/partially_valid/active_record_ext'
6 lib/partially_valid/active_record_ext.rb
@@ -0,0 +1,6 @@
+# Extend ActiveRecord::Base with the partially valid extension module.
+module ActiveRecord
+ class Base
+ include PartiallyValid
+ end
+end
60 lib/partially_valid/core.rb
@@ -0,0 +1,60 @@
+# Add partial validation to ActiveRecord model objects.
+#
+# Example: Assume name, address, and phone number are all required
+# fields on a Person model.
+#
+# bill = Person.new
+# bill.should_be_partially_valid_on :name, :address
+# bill.partially_valid? #=> false (name and address are invalid)
+#
+# bill.name = "Bill"
+# bill.address = "Home Address"
+# bill.partially_valid? #=> true (now name and address are valid)
+#
+# bill.should_be_partially_valid_on :phone_number
+# bill.partially_valid? #=> false (fails now because phone number is checked)
+#
+# After invoking :partially_valid?, the errors object will contain
+# only errors referencing attributes declared to be considered for
+# partial validation.
+#
+# The :valid? method continues to work as before.
+#
+module PartiallyValid
+ # List of attributes that are checked for partial validation.
+ def partially_valid_attributes
+ @partially_valid_attributes ||= []
+ end
+
+ # Clear the list of attributes used in partial validation.
+ def partially_valid_clear
+ @partially_valid_attributes = []
+ end
+
+ # Declare that the list of attributes should be added to the list
+ # checked for partial validation. The attribute names may be
+ # strings or symbols.
+ def should_be_partially_valid_on(*attrs)
+ attrs.each do |attr_name|
+ partially_valid_attributes << attr_name.to_s
+ end
+ partially_valid_attributes.uniq!
+ 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
+ # of partial validation.
+ def partially_valid?
+ valid?
+ pertinent_errors = []
+ errors.each do |attr, msg|
+ pertinent_errors << [attr, msg] if partially_valid_attributes.include?(attr.to_s)
+ end
+ errors.clear
+ pertinent_errors.each do |attr, msg|
+ errors.add(attr, msg)
+ end
+ errors.empty?
+ end
+end
65 test/partially_valid_test.rb
@@ -0,0 +1,65 @@
+require 'rubygems'
+require 'test/unit'
+require 'partially_valid'
+require 'flexmock/test_unit'
+
+class FauxErrors
+ def initialize
+ clear
+ end
+ def add(attr, msg)
+ @errors[attr.to_s] = msg
+ end
+ def on(attr)
+ @errors[attr.to_s]
+ end
+ def empty?
+ @errors.empty?
+ end
+ def clear
+ @errors = {}
+ end
+ def each(&block)
+ @errors.each(&block)
+ end
+end
+
+class FauxModel < ActiveRecord::Base
+ attr_reader :errors
+
+ def valid?
+ @errors = FauxErrors.new
+ @errors.add("x", "bad value")
+ @errors.empty?
+ end
+end
+
+class TestPartiallyValid < Test::Unit::TestCase
+ 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)
+ end
+
+ def test_model_with_no_pv_attributes_is_partially_valid
+ model = FauxModel.new
+ assert model.partially_valid?, "should be partially valid"
+ assert model.errors.empty?
+ end
+
+ def test_model_with_pv_attributes_is_not_partially_valid
+ model = FauxModel.new
+ model.should_be_partially_valid_on(:x)
+ assert ! model.partially_valid?, "should not be partially valid"
+ assert ! model.errors.empty?
+ assert_equal "bad value", model.errors.on(:x)
+ end
+
+ def test_model_can_clear_pv_attributes
+ model = FauxModel.new
+ model.should_be_partially_valid_on(:x)
+ model.partially_valid_clear
+ assert model.partially_valid?, "should be partially valid"
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.