No commit activity in last 3 years
No release in over 3 years
Simple way to do validations more clear :)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
= 0.2.3
>= 3.2
~> 10.0
= 3.2.0
= 1.2.0
 Project Readme

ExtractedValidator

Code Climate Test Coverage Build Status

ActiveRecord and ActiveModel, as parts of Rails, are powerfull and cool things. They can do many works with simple API call. But coupling of model, validations and business logic in one place is awkward and ugly. Espessialy in big projects. And sometimes simple creating record for testing is painfull and I want to cry 😢.

And we must make our life and coding easier! Extract validations rules!

Installation

Add this line to your application's Gemfile:

gem 'extracted_validator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install extracted_validator

Usage

Basic usage

Validator is defining as separated class. For example:

class PostValidator < ExtractedValidator::Base
  validate :title_for_blank

  def title_for_blank
    add_error('Title must be set!') if title.blank?
  end
end

Then pass model that must be validated to initializer and use same API as well as you use validations on model directly.

post = Post.new title: 'New amazing post'

validator = PostValidator.new(post)

# Check if model valid
validator.valid?

# Return Errors instance
validator.errors

# Return errors messages
validator.errors_messages
validator.errors_full_messages

Model class required validations

Some validations is required model class for do work well. For example, validates_uniqueness_of and others. To consider this cases we must pass model class like in the next example:

class PostValidator < ExtractedValidator::Base[Post]
  validates :title, presence: true
  validates_uniqueness_of :title
end

Association validation

In most cases records are not independent. There are singular or plural associations and you will want to be able to validate all the tree of records. Lets consider the example below:

# We has next models
class Article < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :article
end

# Lets define some validator
class CommentValidator < ExtractedValidator::Base[Comment]
  validates :body, presence: true
end

class ArticleValidator < ExtractedValidator::Base[Article]
  validates :title, presence: true

  validating :comments, by: CommentValidator
end

article = Article.new
article.comments.build

validator = ArticleValidator.new(article)

validator.valid? # => false

Place for validators

IMHO validators must be contained in app/validators folder. Don't forget to add this path for autoloading in config/application.rb

config.autoload_paths += %W(#{config.root}/app/validators)

Contributing

  1. Fork it ( https://github.com/alterego-labs/extracted_validator/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request