wizard_controller 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/History.txt +2 -0
  2. data/README +3 -0
  3. data/README.rdoc +3 -0
  4. data/lib/wizard_controller.rb +235 -0
  5. metadata +59 -0
data/History.txt ADDED
@@ -0,0 +1,2 @@
1
+ 20091102: 0.1.5 Changed session variable that stores redirect information, to avoid conflicts.
2
+ 20090925: Packaged as Gem
data/README ADDED
@@ -0,0 +1,3 @@
1
+ Wizard Controller
2
+
3
+ Make your controller inherit from this class.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ Wizard Controller
2
+
3
+ Make your controller inherit from this class.
@@ -0,0 +1,235 @@
1
+ module Codeprimate
2
+ VERSION = "0.1.5"
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
+ def finish_path=(p)
68
+ unless p.blank?
69
+ session[:finish_path] = p
70
+ end
71
+ finish_path
72
+ end
73
+
74
+ private
75
+
76
+ ### PRIVATE CONTROLLER METHODS
77
+
78
+ def handle_finished_wizard
79
+ redirect_to finish_path
80
+ reset_wizard
81
+ end
82
+
83
+ def handle_unfinished_wizard
84
+ if request.get?
85
+ handle_get_action
86
+ else
87
+ handle_post_action
88
+ end
89
+ end
90
+
91
+ def handle_get_action
92
+ execute_method
93
+ render_step_view
94
+ end
95
+
96
+ def handle_post_action
97
+ if (self.wizard_step_completion = execute_process_method)
98
+ next_step
99
+ else
100
+ flash[:error] ||= self.class.wizard_default_error
101
+ render_step_view
102
+ end
103
+ end
104
+
105
+ def restrict_access
106
+ ['index', 'next_step', 'previous_step', 'reset'].include?(params[:action])
107
+ end
108
+
109
+ def execute_method(m=current_wizard_step_method)
110
+ return send(m)
111
+ end
112
+
113
+ def execute_process_method
114
+ return execute_method("process_#{current_wizard_step_method}".to_sym)
115
+ end
116
+
117
+ def render_step_view
118
+ render :action => current_wizard_step_method
119
+ end
120
+
121
+ helper_method :step_number
122
+ def step_number
123
+ current_wizard_step
124
+ end
125
+
126
+ helper_method :total_steps
127
+ def total_steps
128
+ self.class.wizard_steps.size
129
+ end
130
+
131
+ helper_method :next_step_path
132
+ def next_step_path(options={})
133
+ url_for(({:controller => self.controller_name, :action => :next_step}).merge(options))
134
+ end
135
+
136
+ helper_method :previous_step_path
137
+ def previous_step_path
138
+ url_for(:controller => self.controller_name, :action => :previous_step)
139
+ end
140
+
141
+ helper_method :step_completed
142
+ def step_completed
143
+ session[:wizard][self.class.to_s][:completed][current_wizard_step] == true
144
+ end
145
+
146
+ helper_method :wizard_path
147
+ def wizard_path
148
+ url_for(:controller => self.controller_name)
149
+ end
150
+
151
+ helper_method :reset_wizard_path
152
+ def reset_wizard_path
153
+ url_for(:controller => self.controller_name, :action => :reset)
154
+ end
155
+
156
+
157
+ helper_method :abort_wizard_path
158
+ def abort_wizard_path
159
+ abort_path
160
+ end
161
+
162
+ #### SESSION MANAGEMENT
163
+
164
+ def current_wizard_step
165
+ @wizard_step ||= session[:wizard][self.class.to_s][:step].to_i
166
+ end
167
+
168
+ def set_current_wizard_step(step)
169
+ session[:wizard][self.class.to_s][:step] = step
170
+ @wizard_step = step
171
+ end
172
+
173
+ def incr_step
174
+ set_current_wizard_step(current_wizard_step + 1)
175
+ end
176
+
177
+ def decr_step
178
+ set_current_wizard_step([1, (current_wizard_step - 1)].max)
179
+ end
180
+
181
+ def current_wizard_step_method
182
+ self.class.wizard_steps[(current_wizard_step - 1)]
183
+ end
184
+
185
+ def finished
186
+ self.class.wizard_steps.size < current_wizard_step
187
+ end
188
+
189
+ def finish_path
190
+ # should be set to self.class.finish_path but that comes out to nil here somehow. --Dallas
191
+ fp = session[:finish_path] ||= self.class.finish_path ||= '/'
192
+ return fp
193
+ end
194
+
195
+ def abort_path=(p)
196
+ unless p.blank?
197
+ session[:abort_return_to] = p
198
+ end
199
+ abort_path
200
+ end
201
+
202
+ def abort_path
203
+ session[:abort_return_to] ||= self.class.abort_path ||= '/'
204
+ end
205
+
206
+ def no_processing
207
+ self.wizard_step_completion = true
208
+ end
209
+
210
+ def set_as_not_completed
211
+ self.wizard_step_completion = false
212
+ end
213
+
214
+ def wizard_step_completion=(completed)
215
+ session[:wizard][self.class.to_s][:completed][current_wizard_step] = completed
216
+ end
217
+
218
+ def reset_wizard
219
+ session[:wizard][self.class.to_s] = nil
220
+ init_wizard_session
221
+ end
222
+
223
+ def init_wizard_session
224
+ session[:wizard] ||= {}
225
+ session[:wizard][self.class.to_s] ||= {}
226
+ session[:wizard][self.class.to_s][:step] ||= 1
227
+ session[:wizard][self.class.to_s][:completed] ||= {}
228
+ session[:finish_path] ||= self.class.finish_path
229
+ @wizard_step = session[:wizard][self.class.to_s][:step].to_i
230
+ end
231
+
232
+ end
233
+ end
234
+
235
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wizard_controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-02 00:00:00 -06: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
26
+ - README.rdoc
27
+ - History.txt
28
+ - lib/wizard_controller.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/codeprimate/wizard_controller
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --inline-source
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project: wizard_controller
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Wizard Controller is an inheritable class to ease the creation of Wizards
58
+ test_files: []
59
+