Etat
Etat is a minimalistic gem to help you (me, for now) manage some states in your app. It is built for Rails 4 and assumes the use of ActiveRecord 4. See the 'usage' section for more.
Etat means "State" in French.
Installation
Add this line to your application's Gemfile:
gem 'etat'
And then execute:
$ bundle
Usage
class MyModel < ActiveRecord::Base
has_states :draft, :published, :deleted
event :publish do
# some code to happen on publish...
self.state = :publish
self.save!
end
end
Etat will generate a few methods and scopes that will help you:
MyModel.states
will give you an array of the states. # [:draft, :published, :deleted]
MyModel.all_published
: scope that will return all records with that state (and equivalents for each states).
MyModel.all_but_published
: scope that will return all records except the ones with that state (and equivalents for each states).
my_model_instance.state
: it will return the current state, as a symbol, always.
my_model_instance.published?
: boolean. Will tell you if current record has that state or not (and equivalents for each states).
Contributing
- Fork it
- 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