codeprimate-codeprimate-wizard_controller 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/History.txt +1 -0
  2. data/README.rdoc +3 -0
  3. data/lib/wizard_controller.rb +236 -0
  4. metadata +57 -0
@@ -0,0 +1 @@
1
+ 20090925: Packaged as Gem
@@ -0,0 +1,3 @@
1
+ Wizard Controller
2
+
3
+ Make your controller inherit from this class.
@@ -0,0 +1,236 @@
1
+ module Codeprimate
2
+ VERSION = "0.1.1"
3
+
4
+ module Wizard
5
+
6
+ class Base < ApplicationController
7
+ before_filter :restrict_access, :init_wizard_session
8
+
9
+ ### CLASS METHODS
10
+ class << self
11
+ @wizard_steps = []
12
+ @finish_path = '/'
13
+ @wizard_default_error = 'There was a problem processing the last step.'
14
+ attr_accessor :wizard_steps, :finish_path, :abort_path, :wizard_default_error
15
+
16
+ def define_steps(*args)
17
+ self.wizard_steps = args
18
+ end
19
+
20
+ def set_finish_path(p)
21
+ self.finish_path = p
22
+ end
23
+
24
+ def set_abort_path(p)
25
+ self.abort_path = p
26
+ end
27
+
28
+ def set_default_error(e)
29
+ self.wizard_default_error = e
30
+ end
31
+ end
32
+
33
+ ### PUBLIC ACTIONS
34
+
35
+ def set_abort_path(p)
36
+ self.abort_path = p
37
+ end
38
+
39
+ def index
40
+ if finished
41
+ handle_finished_wizard
42
+ else
43
+ handle_unfinished_wizard
44
+ end
45
+ end
46
+
47
+ def next_step
48
+ if step_completed
49
+ incr_step
50
+ else
51
+ flash[:error] ||= self.class.wizard_default_error
52
+ end
53
+ self.finish_path = params[:redirect] unless params[:redirect].blank?
54
+ redirect_to :action => :index
55
+ end
56
+
57
+ def previous_step
58
+ decr_step
59
+ redirect_to :action => :index
60
+ end
61
+
62
+ def reset
63
+ reset_wizard
64
+ redirect_to :action => :index
65
+ end
66
+
67
+
68
+
69
+ private
70
+
71
+ ### PRIVATE CONTROLLER METHODS
72
+
73
+ def handle_finished_wizard
74
+ redirect_to finish_path
75
+ reset_wizard
76
+ end
77
+
78
+ def handle_unfinished_wizard
79
+ if request.get?
80
+ handle_get_action
81
+ else
82
+ handle_post_action
83
+ end
84
+ end
85
+
86
+ def handle_get_action
87
+ execute_method
88
+ render_step_view
89
+ end
90
+
91
+ def handle_post_action
92
+ if (self.wizard_step_completion = execute_process_method)
93
+ next_step
94
+ else
95
+ flash[:error] ||= self.class.wizard_default_error
96
+ render_step_view
97
+ end
98
+ end
99
+
100
+ def restrict_access
101
+ ['index', 'next_step', 'previous_step', 'reset'].include?(params[:action])
102
+ end
103
+
104
+ def execute_method(m=current_wizard_step_method)
105
+ return send(m)
106
+ end
107
+
108
+ def execute_process_method
109
+ return execute_method("process_#{current_wizard_step_method}".to_sym)
110
+ end
111
+
112
+ def render_step_view
113
+ render :action => current_wizard_step_method
114
+ end
115
+
116
+ helper_method :step_number
117
+ def step_number
118
+ current_wizard_step
119
+ end
120
+
121
+ helper_method :total_steps
122
+ def total_steps
123
+ self.class.wizard_steps.size
124
+ end
125
+
126
+ helper_method :next_step_path
127
+ def next_step_path(options={})
128
+ url_for(({:controller => self.controller_name, :action => :next_step}).merge(options))
129
+ end
130
+
131
+ helper_method :previous_step_path
132
+ def previous_step_path
133
+ url_for(:controller => self.controller_name, :action => :previous_step)
134
+ end
135
+
136
+ helper_method :step_completed
137
+ def step_completed
138
+ session[:wizard][self.class.to_s][:completed][current_wizard_step] == true
139
+ end
140
+
141
+ helper_method :wizard_path
142
+ def wizard_path
143
+ url_for(:controller => self.controller_name)
144
+ end
145
+
146
+ helper_method :reset_wizard_path
147
+ def reset_wizard_path
148
+ url_for(:controller => self.controller_name, :action => :reset)
149
+ end
150
+
151
+
152
+ helper_method :abort_wizard_path
153
+ def abort_wizard_path
154
+ abort_path
155
+ end
156
+
157
+ #### SESSION MANAGEMENT
158
+
159
+ def current_wizard_step
160
+ @wizard_step ||= session[:wizard][self.class.to_s][:step].to_i
161
+ end
162
+
163
+ def set_current_wizard_step(step)
164
+ session[:wizard][self.class.to_s][:step] = step
165
+ @wizard_step = step
166
+ end
167
+
168
+ def incr_step
169
+ set_current_wizard_step(current_wizard_step + 1)
170
+ end
171
+
172
+ def decr_step
173
+ set_current_wizard_step([1, (current_wizard_step - 1)].max)
174
+ end
175
+
176
+ def current_wizard_step_method
177
+ self.class.wizard_steps[(current_wizard_step - 1)]
178
+ end
179
+
180
+ def finished
181
+ self.class.wizard_steps.size < current_wizard_step
182
+ end
183
+
184
+ def finish_path=(p)
185
+ unless p.blank?
186
+ session[:return_to] = p
187
+ end
188
+ finish_path
189
+ end
190
+
191
+ def finish_path
192
+ # should be set to self.class.finish_path but that comes out to nil here somehow. --Dallas
193
+ session[:return_to] ||= self.class.finish_path ||= '/'
194
+ end
195
+
196
+ def abort_path=(p)
197
+ unless p.blank?
198
+ session[:abort_return_to] = p
199
+ end
200
+ abort_path
201
+ end
202
+
203
+ def abort_path
204
+ session[:abort_return_to] ||= self.class.abort_path ||= '/'
205
+ end
206
+
207
+ def no_processing
208
+ self.wizard_step_completion = true
209
+ end
210
+
211
+ def set_as_not_completed
212
+ self.wizard_step_completion = false
213
+ end
214
+
215
+ def wizard_step_completion=(completed)
216
+ session[:wizard][self.class.to_s][:completed][current_wizard_step] = completed
217
+ end
218
+
219
+ def reset_wizard
220
+ session[:wizard][self.class.to_s] = nil
221
+ init_wizard_session
222
+ end
223
+
224
+ def init_wizard_session
225
+ session[:wizard] ||= {}
226
+ session[:wizard][self.class.to_s] ||= {}
227
+ session[:wizard][self.class.to_s][:step] ||= 1
228
+ session[:wizard][self.class.to_s][:completed] ||= {}
229
+ session[:return_to] ||= self.class.finish_path
230
+ @wizard_step = session[:wizard][self.class.to_s][:step].to_i
231
+ end
232
+
233
+ end
234
+ end
235
+
236
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codeprimate-codeprimate-wizard_controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Wizard Controller is an inheritable class to ease the creation of Wizards
17
+ email: patrick.morgan@masterwebdesign.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - History.txt
27
+ - lib/wizard_controller.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/codeprimate/codeprimate-wizard_controller
30
+ licenses:
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --inline-source
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: codeprimate-wizard_controller
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Wizard Controller is an inheritable class to ease the creation of Wizards
56
+ test_files: []
57
+