Sunday, November 04, 2007

Validatable 1.6.6 released

The 1.6.6 version of Validatable was released this morning.

This was a minor release that removed custom validation assertion syntax. (http://blog.jayfields.com/2007/09/ruby-creating-custom-assertions.html)

You can replace custom validation assertions with the newly introduced validate_only instance method. The validate_only method is my latest attempt to make it easy to test model validations one at a time.

The following code should provide a decent example.

require 'rubygems'
require 'test/unit'
require 'dust'
require 'validatable'

class SampleModel
include Validatable
validates_presence_of :name, :address
attr_accessor :name, :address
end

unit_tests do
test "individual validation for name is executed" do
instance = SampleModel.new
instance.validate_only("presence_of/name")
assert_equal "can't be empty", instance.errors.on(:name)
end

test "individual validation for address is not executed" do
instance = SampleModel.new
instance.validate_only("presence_of/name")
assert_equal nil, instance.errors.on(:address)
end
end


The validate_only method accepts an argument that is a DSL for running a single validation. The format of the DSL is "[validation type]/[key or attribute]". If the validation has a key set you should provide the key, otherwise the attribute will suffice. The documentation provides the following examples.
  • validates_presence_of :name can be run with obj.validate_only("presence_of/name")
  • validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")
Post a Comment