ValidatableAssociations

ValidatableAssociations is a Rails plugin and add-on to Jay Fields Validatable library. This add-on lets you specify associations to other validatable Classes and allows you to set up a decent validatable structure.

Install

Install the gem (recommended):

$ gem install validatable_associations

Please notice that the validatable_associations gem is in the gemcutter repository. Please follow the instructions provided on their website to set up your rubygems installation.

Or install as a Rails plugin:

$ script/plugin install git://github.com/rubiii/validatable_associations.git

Associations

A very simple example of a “User has one Gorilla” association:

class User
  include Validatable
  include ValidatableAssociations

  has_one :gorilla

  attr_accessor :username, :password

  validates_presence_of :username
  validates_length_of :password, :minimum => 6
end

class Gorilla
  include Validatable
  include ValidatableAssociations

  attr_accessor :name, :size

  validates_presence_of :name
  validates_numericality_of :size
end

Currently implemented associations:

The first version of this add-on only includes has_one associations. I intend to only add more associations if I need them myself or if anyone convinces me that he really needs support for another type of association.

Validation

Calling the valid? method (provided by the Validatable library) on an instance of one of your validatable Classes will also run the validations off all associations of the Class.

Mass-assignment

You can use mass-assignment to assign multiple values to your Class at once. This also includes every one of its associations.

user = User.new :username => "apricot", :password => "secret",
  :gorilla => { :name => "Joe", :size => 4411 }