Module: ActsAsApprovable::Model

Defined in:
lib/acts_as_approvable/model.rb,
lib/acts_as_approvable/model/class_methods.rb,
lib/acts_as_approvable/model/instance_methods.rb,
lib/acts_as_approvable/model/create_instance_methods.rb,
lib/acts_as_approvable/model/update_instance_methods.rb

Overview

The meat of ActsAsApprovable. This applies methods for the configured approval events and configures the required relationships.

Defined Under Namespace

Modules: ClassMethods, CreateInstanceMethods, InstanceMethods, UpdateInstanceMethods

Instance Method Summary collapse

Instance Method Details

#acts_as_approvable(options = {}) ⇒ Object

Declare this in your model to require approval on new records or changes to fields.

Parameters:

  • options (Hash) (defaults to: {})

    the options for this models approval workflow.

Options Hash (options):

  • :on (Symbol, Array)

    The events to require approval on (‘:create` or `:update`).

  • :state_field (String)

    The local field to store ‘:create` approval state.

  • :ignore (Array)

    A list of fields to ignore. By default we ignore ‘:created_at`, `:updated_at` and the field specified in `:state_field`.

  • :only (Array)

    A list of fields to explicitly require approval on. This list supercedes ‘:ignore`.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/acts_as_approvable/model.rb', line 19

def acts_as_approvable(options = {})
  extend ClassMethods
  include InstanceMethods

  cattr_accessor :approvable_on
  self.approvable_on = Array.wrap(options.delete(:on) { [:create, :update] })

  cattr_accessor :approvable_field
  self.approvable_field = options.delete(:state_field)

  cattr_accessor :approvable_ignore
  ignores = Array.wrap(options.delete(:ignore) { [] })
  ignores.push('created_at', 'updated_at', primary_key, self.approvable_field)
  self.approvable_ignore = ignores.compact.uniq.map(&:to_s)

  cattr_accessor :approvable_only
  self.approvable_only = Array.wrap(options.delete(:only) { [] }).uniq.map(&:to_s)

  cattr_accessor :approvals_disabled
  self.approvals_disabled = false

  has_many :approvals, :as => :item, :dependent => :destroy

  if approvable_on?(:update)
    include UpdateInstanceMethods
    before_update :approvable_update, :if => :approvable_update?
  end

  if approvable_on?(:create)
    include CreateInstanceMethods
    before_create :approvable_create, :if => :approvable_create?
  end

  after_save :approvable_save, :if => :approvals_enabled?
end