This plugin provides a workflow for approving new records and changes to existing records.
Add the gem to your Gemfile:
Then run the generator:
rails g acts_as_approvable
Add the gem to
rake gems:install. After the gem is installed, run the generator:
$ script/generate acts_as_approvable
These options are also available by passing
--help as an option to the generator.
* This option is not available in Rails 3. You should configure your template engine in
API Documentation is available online.
The generator creates an initializor at
config/initializers/acts_as_approvable.rb. A sample
initializer might look like this:
. = 'haml' ::.
Ownership functionality expects a
User model in your project by default, but by providing
:owner option you can change the expected model to whatever you wish.
accepts a block which it applies to the
Approval model, allowing you to override methods as
you see fit.
For example, to only allow Users with the "admin" role to 'own' an Approval, change your initializer to something like this:
. = 'haml' ::.(:source => ApprovalSources)
ApprovalSources class should be created by you if you wish to override how available owners
are selected or the option arrays for
select_tag are created. Below is an example:
class ApprovalSources def self.available_owners User.all(:conditions => ['role', 'admin']) end def self.option_for_owner(owner) # default is [owner.to_str, owner.id] [owner.email, owner.id] end end
Require approval for new Users, but not modifications...
class User < ActiveRecord::Base acts_as_approvable :on => :create, :state_field => :state # Let the user know they've been approved (ApprovalMailer is your creation) def after_approve(approval) ApprovalMailer.deliver_user_approved(self.email) end # Let the user know they were rejected def after_reject(approval) ApprovalMailer.deliver_user_rejected(self.email, approval.reason) end end
Require approval when a Game's title or description is changed, but not when view or installation count is changed...
class Game < ActiveRecord::Base acts_as_approvable :on => :update, :ignore => [:views, :installs] end
Require approval for all changes, except the standard ignored fields (
class Advertisement < ActiveRecord::Base acts_as_approvable :state_field => :state end
The following options may be used to configure the workflow on a per-model basis:
:onThe type of events (
:destroy) to require approval on.
:ignoreA list of fields to ignore for
:onlyA list of fields that should be approved. All other fields are ignored. If set, the
:ignoreoption is... ignored.
:state_fieldA local model field to save the
:createapprovals state. Useful for selecting approved models without joining the approvals table.
:updated_at and whatever is set for the
are automatically ignored.