Module: Controllers::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.



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

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.



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

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

#workflow_actionObject



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

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_instance_methods(false).include?(@event)
      send(@event)
    else
      workflow_action_complete
    end
  end
end

#workflow_action_completeObject



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

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

  if params[:note].present? && @record.respond_to?('quick_note')
    @record.quick_note(params[:note])
  end
  @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
  else
    if self.class.public_instance_methods(false).include?(@event)
      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
end