Truthiness is...
...knowing that one line can make a difference.
This is a simple gem with a simple purpose. I love Ruby. One of the biggest reasons why I love Ruby so much is because of its readability. Unfortunately, I'd been forever typing stuff like this:
validates_inclusion_of :loves_long_walks_on_the_beach, :in => [true, false]
Is this horrible? Hell no. Do I want to make it better? Hell yes.
What I really wanted to type was this:
validates_truthiness_of :loves_long_walks_on_the_beach
See 'Aliases' at the bottom if you'd rather have a more unambiguous validation title. :)
Installation
gem install validates_truthiness
Usage
You can either use the Rails 2-style to chain multiple columns together: validates_truthiness_of :opting_out_of_newsletters, :consents_to_strip_search
Or the Rails 3-style: validates :works_hard_for_the_money, truthiness => true
If you'd like a specific message, say "Yes" or "No" instead of "True" or "False",
just pass it as a :message
option:
validates :believes_they_may_be_a_cylon,
:truthiness => {:message => "must be either a 'Yes' or a fraking 'No'"}
Also, don't forget you can :allow_nil
in case you need the triumvirate of:
true, false and nothing.
I18n
Due to this being wired up through ActiveModel, internationalization is free, just add the proper keys to your .yml files.
No Stone Unturned
Just to make sure this was the ULTIMATE boolean validation gem, although I believe use cases for these are slim to none:
Validating Truth
validates_verity_of
validates_truth_of
Validating Falsity
validates_falsity_of
validates_falsehood_of
Aliases
In case some of you aren't fond of the ambiguity of "truthiness" I've also aliased validates_truthiness_of
to validates_booleanship_of
Issues
If you have problems, please create a Github issue.
Credits
validates_truthiness is maintained by The Kompanee, Ltd.
The names and logos for The Kompanee are trademarks of The Kompanee, Ltd.
License
validates_truthiness is Copyright © 2011 The Kompanee. It is free software, and may be redistributed under the terms specified in the LICENSE file.