module AasmActionable::ControllerMixin
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:
-
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.
-
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
Public Instance Methods
# File lib/aasm_actionable/controller_mixin.rb, line 46 def action_view_prefix(event) controller_name end
# 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