<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dynamic and Concise &#187; Validatable</title>
	<atom:link href="http://yiwenandsoftware.wordpress.com/tag/validatable/feed/" rel="self" type="application/rss+xml" />
	<link>http://yiwenandsoftware.wordpress.com</link>
	<description>Everything about my experience with Software Development</description>
	<lastBuildDate>Sat, 14 Mar 2009 22:05:25 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='yiwenandsoftware.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/fa9ad49ca1c5d02890788e46a95fe979?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Dynamic and Concise &#187; Validatable</title>
		<link>http://yiwenandsoftware.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://yiwenandsoftware.wordpress.com/osd.xml" title="Dynamic and Concise" />
		<item>
		<title>When RSpec meets Validatable</title>
		<link>http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 16:00:30 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Validatable]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/</guid>
		<description><![CDATA[Jay Fields writes an excellent module called Validatable to provide a class who include the module the ActiveRecord like behavior. For example, you can say validates_presence_of :attribute etc. This page has the RDoc of the module.When I write RSpec for a Validatable class, I ended up writing a stattement like this:

  @email.validate_only("presence_of/body")
  @email.errors.on(:body).should_not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=9&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://blog.jayfields.com/">Jay Fields</a> writes an excellent module called <a href="http://blog.jayfields.com/2007/11/validatable-166-released.html">Validatable</a> to provide a class who include the module the ActiveRecord like behavior. For example, you can say <em>validates_presence_of :attribute</em> etc. <a href="http://validatable.rubyforge.org/">This page</a> has the RDoc of the module.When I write RSpec for a Validatable class, I ended up writing a stattement like this:</p>
<pre>
  @email.validate_only("presence_of/body")
  @email.errors.on(:body).should_not be_nil
</pre>
<p>Which is basically testing <em>body</em> is a required field. It works, but it looks complicated and hard to understand.The RSpec on Rails plugin provides a method <em>error_on</em> so that I can say something like this:
<pre> model.should have(2).errors_on(:attribute)</pre>
<p>This is much more readable than the previous one.I came up with a small extension on Validatable so that I can have a better syntax for testing Validatable.Here&#8217;s the spec:</p>
<pre class="brush: ruby;">
require File.dirname(__FILE__) + '/../../../spec_helper'

module RspecValidatableSpec
  class Thing
    include Validatable
    attr_accessor :age
    attr_accessor :name
    validates_presence_of :age
    validates_numericality_of :age
  end
end

describe RSpecValidatable, 'a validatable object' do
  before :each do
    @thing = RspecValidatableSpec::Thing.new
  end

  it &quot;should tell you it has at least one error on a field&quot; do
    @thing.should have_at_least(1).error_on(:age)
  end

  it &quot;should tell you it has one error on a required field&quot; do
    @thing.should have(1).error_on_presence_of(:age)
  end

  it &quot;should tell you it has one error on a numerical field&quot; do
    @thing.should have(1).error_on_numericality_of(:age)
  end
end

describe RSpecValidatable, 'a validatable object' do
  before :each do
    @thing = RspecValidatableSpec::Thing.new
  end

  it &quot;should tell you it has no error on a field&quot; do
    @thing.should have(:no).errors_on(:name)
  end
end
</pre>
<p>You may need a different path to your spec_helper.rb to make it work.I got the &#8220;traditional&#8221; <em>errors_on</em> just like how RSpec works on ActiveRecords. What&#8217;s more, Validatable has a <em>validates_only</em> method which allows us validating only one type of error on one attribute. so I can have even better control than RSpec on Rails giving me over ActiveRecords. For example, I can specify what type of errors I expect to have.The only problem is that <em>validates_only</em> raises an error if you check an attribute on a validation that is not specified. So a nice spec like</span> email.should have(:no).errors_on_presence_of(:attachment)</span>is not quite possible. For those of you who are interested in this, here is the actually code:</p>
<pre class="brush: ruby;">
module RSpecValidatable
  def errors_on(attribute)
    self.valid?
    [self.errors.on(attribute)].flatten.compact
  end

  def respond_to? method_name
    super(method_name) || method_name.to_s.index('error_on') == 0
  end

  def method_missing(error_on_method, attribute)
    reason = error_on_method.to_s.sub(/error_on_/, '')
    self.validate_only(&quot;#{reason.to_s}/#{attribute.to_s}&quot;)
    [self.errors.on(attribute)].flatten.compact
  end

  alias :error_on :errors_on

end

Validatable.send :include, RSpecValidatable
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=9&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Captcha and Validatable</title>
		<link>http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 16:03:00 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[simple captcha]]></category>
		<category><![CDATA[Validatable]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/</guid>
		<description><![CDATA[I need a captcha in a form, whose associated model is a Validatable.   After googling, I decided to go with Simple Captcha. The Simple captcha is extremely easy to use, see its documentation, you&#8217;ll know. But it works fine with a ActiveRecord::Base, not a Validatable. There has to be some hack to make [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=6&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I need a captcha in a form, whose associated model is a <a href="http://blog.jayfields.com/2007/04/ruby-validatable-122-released.html">Validatable.</a>   After googling, I decided to go with <a href="http://expressica.com/2007/03/23/simple_captcha_1_0/">Simple Captcha</a>. The Simple captcha is extremely easy to use, see its documentation, you&#8217;ll know. But it works fine with a ActiveRecord::Base, not a Validatable. There has to be some hack to make it work. I could use &#8220;Controller mode&#8221; of it. But it will be nice to put it into model, make it part of validation process. and here&#8217;s my code:</p>
<p>First, include SimpleCaptcha into your Validatable model:</p>
<pre class="brush: ruby;">
YourValidatable.module_eval do
    class &lt;&lt; self; include SimpleCaptcha::ModelHelpers; end
end
</pre>
<p>And overwrite <i>apply_simple_captcha</i></p>
<pre class="brush: ruby;">
  def self.apply_simple_captcha(options = {})
    module_eval do
      require 'pstore'
      include SimpleCaptcha::ConfigTasks
      attr_accessor :captcha, :captcha_code,
      :authenticate_with_captcha
      alias_method :valid_without_captcha?, :valid?
    end
    @captcha_invalid_message =
 (options[:message].nil? || options[:message].empty?) ?
  &quot; image did not match with text&quot; : options[:message]
    module_eval(turing_valid_method)
    module_eval(turing_save_method)
  end
</pre>
<p>Finally, call <code>apply_simple_captcha</code></p>
<p>That&#8217;s it.</p>
<p>In the controller, instead of calling <code>valid?</code> to decide if the object is valid, you call <code>valid_with_captcha?</code></p>
<p>BTW: <a href="http://blog.inlet-media.de/rmagick-does-not-play-well-on-ubuntu">Follow this link</a> to see how to install RMagick on Ubuntu</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=6&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
	</channel>
</rss>