Module: SimpleWorkflow::Controller

Includes:
Detour
Defined in:
lib/simple_workflow/controller.rb

Overview

Mixin to add controller methods for workflow navigation.

Instance Method Summary collapse

Methods included from Detour

#pop_detour, #reset_workflow, #store_detour_in_session

Instance Method Details

#back(response_status_and_flash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simple_workflow/controller.rb', line 32

def back(response_status_and_flash)
  return false if session[:detours].nil?

  detour = pop_detour(session)
  post = detour.delete(:request_method) == :post
  if post
    save_flash(response_status_and_flash)
    redirect_to_post(detour)
  else
    redirect_to detour, response_status_and_flash
  end
  true
rescue StandardError
  retry
end

#back_or_redirect_to(options = {}, response_status_and_flash = {}) ⇒ Object



48
49
50
# File 'lib/simple_workflow/controller.rb', line 48

def back_or_redirect_to(options = {}, response_status_and_flash = {})
  back(response_status_and_flash) || redirect_to(options, response_status_and_flash)
end

#detour_to(options) ⇒ Object

Like ActionController::Base#redirect_to, but stores the location we come from, enabling returning here later.



11
12
13
14
# File 'lib/simple_workflow/controller.rb', line 11

def detour_to(options)
  store_detour(params)
  redirect_to(options)
end

#redirect_to_post(options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simple_workflow/controller.rb', line 67

def redirect_to_post(options)
  url = url_for options
  render text: <<~HTML, layout: false
    <html>
      <body onload="document.getElementById('form').submit()">
        <form id="form" action="#{url}" method="POST">
        </form>
      </body>
    </html>
  HTML
end

#rjs_detour_to(options) ⇒ Object



16
17
18
19
# File 'lib/simple_workflow/controller.rb', line 16

def rjs_detour_to(options)
  store_detour(params, request.post?)
  rjs_redirect_to(options)
end

#rjs_redirect_to(options) ⇒ Object



21
22
23
24
# File 'lib/simple_workflow/controller.rb', line 21

def rjs_redirect_to(options)
  @options = options
  render template: 'redirect', layout: false, formats: :js
end

#store_detour(options, post = false) ⇒ Object



26
27
28
29
30
# File 'lib/simple_workflow/controller.rb', line 26

def store_detour(options, post = false)
  options = options.dup.permit!.to_h if options.is_a?(ActionController::Parameters)
  options[:request_method] = :post if post
  store_detour_in_session(session, options)
end