Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

tomoasleep/activemodel-behavior_validator

Repository files navigation

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 }

About

Validate the behavior of attribute

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages