This gem allows a model to be activated or deactivated, saving informations like 'activated_at', 'deactivated_at', 'activated_by' and 'deactivated_by'.
Add this line to your application's Gemfile:
gem 'activable'And then execute:
$ bundle
Create the initializer file:
$ rails g activable:install
Configure the models you want to be "activable". They need to be created first:
$ rails g model product description
$ rails g activable product
$ rake db:migrate
Your models will look like this:
class Product < ActiveRecord::Base
is_activable
# ...
endOptionally, you can customize Activable configuration for each model:
class Product < ActiveRecord::Base
is_activable has_responsible: false
# ...
end
class Category < ActiveRecord::Base
is_activable responsible: "Admin"
# ...
endBy default, new objects of an "activable" model are active:
p = Product.new
p.active? # => trueIf your record has a responsible, you must provide it before saving your object:
p = Product.new
p.save # will not work, it will say that activated_by_id is required
# You need to do something like this
p = Product.new
p.activate responsible: current_userThe same rule applies when deactivating:
product.deactivate responsible: current_userYou can check whether it is active or not whenever you want by calling the method
active?. Another useful informations are persisted and you can see them:
product.activated_at
product.deactivated_at
# And if it has a responsible:
product.activated_by
product.deactivated_byAlso, this gem creates two useful scopes:
Product.active
Product.inactiverspec spec
- 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