Repository is archived
No commit activity in last 3 years
No release in over 3 years
A behavior validator for Rails 3 and 4.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0
>= 0

Runtime

 Project Readme

activemodel-behavior_validator

Build Status Code Climate Coverage Status Dependency Status

Usage

Add to your Gemfile:

gem 'activemodel-behavior_validator'

Run:

bundle install

Then add the following to your model:

validates :my_attribute, behavior: { method_name => expected_result }

Example

Comment must belong to a published public Article.

class Article < ActiveRecord::Base
  attr_accessible :date, :private

  def published?
    date > Time.now
  end
end

class Comment < ActiveRecord::Base
  belongs_to :article
  validates :article, behavior: { private?: false, published?: true }
end

Validation outside a model

If you need to validate a outside a model, you can do that:

BehaviorValidator.valid?(object, { method_name => expected_result }