Module: Controllers::Workflowable

Overview

ActiveSupport::Concern mixin: workflowable.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#render_workflow_error_streamObject

Override in controllers to customize the turbo_stream error response.
Default: redirect via turbo_stream so the behavior matches the HTML path.



75
76
77
# File 'app/concerns/controllers/workflowable.rb', line 75

def render_workflow_error_stream
  render turbo_stream: turbo_stream.redirect(@return_path || record_redirect_path)
end

#render_workflow_success_streamObject

Override in controllers to customize the turbo_stream success response.
Default: redirect via turbo_stream so the behavior matches the HTML path.



69
70
71
# File 'app/concerns/controllers/workflowable.rb', line 69

def render_workflow_success_stream
  render turbo_stream: turbo_stream.redirect(@return_path || record_redirect_path)
end

#workflow_actionObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/concerns/controllers/workflowable.rb', line 12

def workflow_action
  # initialize all our goodies
  load_record
  lock_key = "#{@record.class.name.downcase}_#{@record.id}".freeze
  @record.with_advisory_lock(lock_key, timeout_seconds: 120) do
    # If our controller defines a custom handler for this event (e.g. a form),
    # call it. We check the controller's own public instance methods to avoid
    # matching inherited methods like :error from ActionController.
    if self.class.public_method_defined?(@event, false)
      send(@event)
    else
      workflow_action_complete
    end
  end
end

#workflow_action_completeObject



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
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/concerns/controllers/workflowable.rb', line 28

def workflow_action_complete
  load_record
  unless @event
    flash[:error] = "Event not specified"
    redirect_to_return_path_or_default record_redirect_path
    return
  end

  @record.quick_note(params[:note]) if params[:note].present? && @record.respond_to?(:quick_note)
  @record.attributes = params[:record] if params[:record]
  @record.state_event = @event

  if @record.save
    respond_to do |format|
      format.turbo_stream do
        flash.now[:info] = "Status updated to #{@record.human_state_name}"
        render_workflow_success_stream
      end
      format.html do
        flash[:info] = "Status updated to #{@record.human_state_name}"
        redirect_to_return_path_or_default record_redirect_path
      end
    end
  elsif self.class.public_method_defined?(@event, false)
    send(@event)
  else
    respond_to do |format|
      format.turbo_stream do
        flash.now[:error] = @record.errors_to_s
        render_workflow_error_stream
      end
      format.html do
        flash[:error] = @record.errors_to_s
        redirect_to_return_path_or_default record_redirect_path
      end
    end
  end
end