Module: SimpleWorkflow::Controller

Included in:
ActionController::Base
Defined in:
lib/simple_workflow/controller.rb

Instance Method Summary collapse

Instance Method Details

#back(response_status_and_flash) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/simple_workflow/controller.rb', line 70

def back(response_status_and_flash)
  return false if session[:detours].nil?
  detour = pop_detour
  post = detour.delete(:request_method) == :post
  if post
    set_flash(response_status_and_flash)
    redirect_to_post(detour)
  else
    redirect_to detour, response_status_and_flash
  end
  true
end

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



83
84
85
# File 'lib/simple_workflow/controller.rb', line 83

def back_or_redirect_to(options = {}, response_status_and_flash = {})
  back(response_status_and_flash) or 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.



3
4
5
6
# File 'lib/simple_workflow/controller.rb', line 3

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

#pop_detourObject



102
103
104
105
106
107
108
109
# File 'lib/simple_workflow/controller.rb', line 102

def pop_detour
  detours = session[:detours]
  return nil unless detours
  detour = detours.pop
  logger.debug "popped detour: #{detour.inspect} #{session[:detours].size} more"
  reset_workflow if detours.empty?
  detour
end

#redirect_to_post(options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/simple_workflow/controller.rb', line 115

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

#remove_old_detoursObject



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

def remove_old_detours
  if Rails.application.config.session_store == ActionDispatch::Session::CookieStore
    ss = ws = nil
    loop do
      ser_val = cookies.signed_or_encrypted.send(:serialize, nil, session.to_hash)
      ss = encryptor.encrypt_and_sign(ser_val).size
      wf_ser_val = cookies.signed_or_encrypted.send(:serialize, nil, session[:detours])
      ws = encryptor.encrypt_and_sign(wf_ser_val).size
      break unless ws >= 2048 || (ss >= 3072 && session[:detours] && session[:detours].size > 0)
      logger.warn "Workflow too large (#{ws}/#{ss}).  Dropping oldest detour."
      session[:detours].shift
      reset_workflow if session[:detours].empty?
    end
    logger.debug "session: #{ss} bytes, workflow(#{session[:detours].try(:size) || 0}): #{ws} bytes"
  end
end

#reset_workflowObject



111
112
113
# File 'lib/simple_workflow/controller.rb', line 111

def reset_workflow
  session.delete(:detours)
end

#rjs_detour_to(options) ⇒ Object



8
9
10
11
# File 'lib/simple_workflow/controller.rb', line 8

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

#rjs_redirect_to(options) ⇒ Object



13
14
15
16
# File 'lib/simple_workflow/controller.rb', line 13

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

#store_detour(options, post = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simple_workflow/controller.rb', line 18

def store_detour(options, post = false)
  options = options.dup.permit!.to_h if options.is_a?(ActionController::Parameters)
  options[:request_method] = :post if post
  if session[:detours] && session[:detours].last == options
    logger.debug "Ignored duplicate detour: #{options.inspect}"
    return
  end
  session[:detours] ||= []
  session[:detours] << options
  remove_old_detours
  logger.debug "Added detour (#{session[:detours].try(:size) || 0}): #{options.inspect}"
end

#store_detour_from_paramsObject



61
62
63
64
65
66
67
68
# File 'lib/simple_workflow/controller.rb', line 61

def store_detour_from_params
  if params[:detour]
    store_detour(params[:detour])
  end
  if params[:return_from_detour] && session[:detours]
    pop_detour
  end
end