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
# define an event. The name of the even has nothing to do with the names of states. It could be `event 'do_whatever_you_want' { puts 'OK!' } `
event :publish do
# some code to happen on publish...
self.state = :published
self.save!
end
end
instance = MyModel.new(state: :draft)
instance.draft? # true
instance.publish
instance.draft? # false
instance.published? # true
You can declare your states with has_states
in your model. See the example above.
You can also create events that can be called, manually. They are basically the same as creating methods right now. In the example above, event :publish
will create an instance method called publish
. That's it. This is just for code clarity; we know it is related to states. Again, just to be clear, it won't be called automatically.
Etat will also generate a few more 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).
TODO
Here are a few things that could be useful, PRs are welcome!
- inject
Etat::ActiveRecord
intoActiveRecord::Base
automatically instead of having toinclude Etat::ActiveRecord
(planned) - default_state :draft (planned)
- callbacks
- before_state :some_state, :do_whatever
- after_state :some_other_state, :do_whatever_else
- around_state :yet_another_state, :lets_do_stuff
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