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



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/simple_workflow/controller.rb', line 47

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



60
61
62
# File 'lib/simple_workflow/controller.rb', line 60

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



79
80
81
82
83
84
85
86
# File 'lib/simple_workflow/controller.rb', line 79

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/simple_workflow/controller.rb', line 92

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

#reset_workflowObject



88
89
90
# File 'lib/simple_workflow/controller.rb', line 88

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
30
31
32
33
34
35
36
# File 'lib/simple_workflow/controller.rb', line 18

def store_detour(options, post = false)
  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
  ss = ws = nil
  loop do
    ss = Base64.encode64(Marshal.dump(session.to_hash)).size
    ws = Base64.encode64(Marshal.dump(session[:detours])).size
    break unless ws >= 2048 || (ss >= 3072 && session[:detours].size > 0)
    logger.warn "Workflow too large (#{ws}).  Dropping oldest detour."
    session[:detours].shift
    reset_workflow if session[:detours].empty?
  end
  logger.debug "Added detour: #{options.inspect}, session: #{ss} bytes, workflow(#{session[:detours].size}): #{ws} bytes"
end

#store_detour_from_paramsObject



38
39
40
41
42
43
44
45
# File 'lib/simple_workflow/controller.rb', line 38

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