DailyAffirmation
A simple library for external validations of POROs
There are currently two ways to validate objects in Rails, ActiveRecord based validates and ActiveModel based validations. Both require them to be placed directly into your models, making for more heavy weight, less flexible code. DailyAffirmation allows you to validate your models (or any PORO) externally.
By validating objects externally, you can have different validations depending on your code path. Perhaps you require a password for a User object when it's being created, but do not need it again when it's being updated. Simply create a UserCreationAffirmation and a UserChangeAffirmation with the correct validations.
Installation
Add this line to your application's Gemfile:
gem 'daily_affirmation'
And then execute:
$ bundle
Or install it yourself as:
$ gem install daily_affirmation
Usage
require "daily_affirmations"
class Team
attr_accessor :name, :status_cd
end
class TeamAffirmation
include DailyAffirmation.affirmations
affirms_presence_of :name
affirms_inclusion_of :status_cd, :list => 0..2
end
team1 = OpenStruct.new(:name => "", :status_cd => 3)
TeamAffirmation.new(team1).valid? #=> false
Contributing
- Fork it ( http://github.com/teamsnap/daily_affirmation/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request