Validates
Simple format validators for Rails 3
Installation
Add this line to your application's Gemfile:
gem 'validates'
And then execute:
$ bundle
Or install it yourself as:
$ gem install 'validates'
Usage
Availables validators: AssociationLength, Email, Existence, Slug, Url, Money
class User < ActiveRecord::Base
validates :email, :email => true
validates :site, :url => true, :allow_blank => true
validates :inn, :inn => true
end
class Company < ActiveRecord::Base
# note AssociationLengthValidator is inherited from ActiveModel::Validations::LengthValidator
# http://api.rubyonrails.org/classes/ActiveModel/Validations/LengthValidator.html
# so you can easily use standard options like :is, :minimum, :maximum, etc.
validates :employees,
:association_length => {
:minimum => 1,
:select => ->(employee) { employee.name.in? ["Mike", "John"] }
}
validates :employees, :association_length => { :minimum => 1, :select => :employees_filter }
def employees_filter(employees)
employees.select { |employee| employee.name.in? ["Mike", "John"] }
end
end
class Page < ActiveRecord::Base
validates :slug, :slug => true
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
run tests:
turn -Itest test/lib