activemodel-behavior_validator
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 }