Module: SimpleWorkflow::Controller

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

Instance Method Summary collapse

Instance Method Details

#backObject



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

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

#back_or_redirect_to(options) ⇒ Object



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

def back_or_redirect_to(options)
  back or redirect_to options
end

#detour_to(options) ⇒ Object



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

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

#pop_detourObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/simple_workflow/controller.rb', line 53

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

#redirect_to_post(options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/simple_workflow/controller.rb', line 64

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

#rjs_detour_to(options) ⇒ Object



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

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

#rjs_redirect_to(options) ⇒ Object



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

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

#store_detour(options, post = false) ⇒ Object



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

def store_detour(options, post = false)
  options[:request_method] = :post if post
  if session[:detours] && session[:detours].last == options
    logger.debug "duplicate detour: #{options.inspect}"
    return
  end
  logger.debug "adding detour: #{options.inspect}"
  session[:detours] ||= []
  session[:detours] << options
end

#store_detour_from_paramsObject



28
29
30
31
32
33
34
35
# File 'lib/simple_workflow/controller.rb', line 28

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