Module: AasmActionable::ControllerMixin

Extended by:
ActiveSupport::Concern
Defined in:
lib/aasm_actionable/controller_mixin.rb

Overview

The ControllerMixin concern implements a controller-based helper that can automatically render those actions which can be performed by a user on a taskable. In order to use it, you must do the following:

  1. Create a Pundit policy for the taskable, with one method named <event>? for every event in the taskable’s state machine. For example, define submit? if there is a submit event.

  2. Create a partial with the same name as every event defined in the taskable’s state machine. For example, create _submit.html.erb if there is a submit event.

Partials are looked up in the view directory with the same name as the controller by default. You can change this by providing the method action_view_prefix(event) in your controller, and returning the view prefix you wish to look up partials in.

Author:

  • Brendan MacDonell

Instance Method Summary collapse

Instance Method Details

#action_view_prefix(event) ⇒ Object



46
47
48
# File 'lib/aasm_actionable/controller_mixin.rb', line 46

def action_view_prefix(event)
  controller_name
end

#render_state_actions(taskable) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aasm_actionable/controller_mixin.rb', line 50

def render_state_actions(taskable)
  a_policy = policy(taskable)
  events = taskable.aasm.events
    .select { |event| a_policy.send("#{event}?") }

  actions = events.map do |event|
    StateAction.new(event, action_view_prefix(event))
  end
  actions.sort_by! {|action| action.title}

  rendered = render_to_string partial: 'aasm_actionable/list',
                              locals: {actions: actions, taskable: taskable}
  rendered.html_safe
end