0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
This gem allows similar ActiveRecord validates_* commands to be grouped together in blocks and pruned of repeated parameters. How often have you had a block of validation commands in an ActiveRecord object that are repeated, especially :id or :unless options? Does this look familiar? validates_presence_of :hair, :hair_color, :unless => :bald? validates_length_of :hair, :within => 3..15, :unless => :bald? validates_inclusion_of :hair_color, :in => HAIR_COLORS, :unless => bald? Instead, this gem will allow you to replace the above code with: validate_block :unless => :bald? do presence_of :hair, :hair_color length_of :hair, :within => 3..15 inclusion_of :hair_color, :in => HAIR_COLORS end ..which is a great way to DRY your :hair, don't you think? Basically, this gem 1) removes the requirement to have 'validates_' on the front of the commands and 2) passes the options on the validate_block command to each validation command inside the block. The syntax of the validation commands remains the same. Keeping the 'validates_*' prefix on the commands inside the block _will_ work but it is not required.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 2.6.1
>= 2.0.4
 Project Readme

validate_block¶ ↑

DEPRECATION NOTICE¶ ↑

This gem is NOT maintained. The funcationality has been replaced by ActiveRecord ‘with_option`: guides.rubyonrails.org/active_record_validations.html#grouping-conditional-validations. I’d like to think this gem had a little something to do with that being added to AR :)

DESCRIPTION:¶ ↑

This gem allows similar ActiveRecord validates_* commands to be grouped together in blocks and pruned of repeated parameters.

How often have you had a block of validation commands in an ActiveRecord object that are repeated, especially :id or :unless options? Does this look familiar?

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

Instead, this gem will allow you to replace the above code with:

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

..which is a great way to DRY your :hair, don’t you think?

Basically, this gem 1) removes the requirement to have ‘validates_’ on the front of the commands and 2) passes the options on the validate_block command to each validation command inside the block.

The syntax of the validation commands remains the same. Keeping the ‘validates_*’ prefix on the commands inside the block will work but it is not required.

SYNOPSIS:¶ ↑

require 'rubygems'
require 'activerecord'
require 'validate_block'

class SomeObject < ActiveRecord::Base

  validate_block :if => :some_method? do
    presence_of :some_field
    inclusion_of :some_other_field, :in => SOME_VALUES
  end

end

REQUIREMENTS:¶ ↑

ActiveRecord >= 2.1.0

INSTALL:¶ ↑

sudo gem install validate_block

LICENSE:¶ ↑

sam.zoy.org/wtfpl/COPYING