Today I put out the first preliminary version of a gem I call ‘validate_block’. It comes from a discussion I had with @jakemallory yesterday where I said I wish I could logically scope the ‘validates_*’ commands in an ActiveRecord object.

It didn’t seem too hard and so last night I threw something together and it’s up on now.

The main idea is that this gem will let you take a block with a lot of duplication like this:

validates_presence_of :hair, :unless => :bald?
validates_length_of :hair, :within => 3..15, :unless => :bald?
validates_inclusion_of :hair_color, :in => HAIR_COLORS, :unless => bald?

..and convert it in to this:

validate_block :unless => :bald? do
  presence_of :hair
  length_of :hair, :within => 3..15
  inclusion_of :hair_color, :in => HAIR_COLORS

As you can see, it eliminates the need to prefix each item with ‘validate_’ and adds the :unless condition to the whole group (you can put any AR Validation options in there).

Check it out at github. That is all.