Class: HatTrick::Wizard
Instance Attribute Summary collapse
Instance Method Summary
collapse
#add_step, #delete_step, #find_step, #move_step, #replace_step, #step_after, #step_before, #steps_after, #steps_before
Constructor Details
#initialize(wizard_def) ⇒ Wizard
Returns a new instance of Wizard.
14
15
16
17
18
|
# File 'lib/hat_trick/wizard.rb', line 14
def initialize(wizard_def)
@wizard_def = wizard_def
@steps = @wizard_def.steps.map { |s| HatTrick::Step.new(s, self) }
@current_step = first_step
end
|
Instance Attribute Details
#controller ⇒ Object
Returns the value of attribute controller.
9
10
11
|
# File 'lib/hat_trick/wizard.rb', line 9
def controller
@controller
end
|
#current_step ⇒ Object
Returns the value of attribute current_step.
10
11
12
|
# File 'lib/hat_trick/wizard.rb', line 10
def current_step
@current_step
end
|
#external_redirect_url ⇒ Object
Returns the value of attribute external_redirect_url.
9
10
11
|
# File 'lib/hat_trick/wizard.rb', line 9
def external_redirect_url
@external_redirect_url
end
|
#model ⇒ Object
Returns the value of attribute model.
9
10
11
|
# File 'lib/hat_trick/wizard.rb', line 9
def model
@model
end
|
#steps ⇒ Object
Returns the value of attribute steps.
10
11
12
|
# File 'lib/hat_trick/wizard.rb', line 10
def steps
@steps
end
|
#wizard_def ⇒ Object
Returns the value of attribute wizard_def.
10
11
12
|
# File 'lib/hat_trick/wizard.rb', line 10
def wizard_def
@wizard_def
end
|
Instance Method Details
#advance_step(next_step_name = nil) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/hat_trick/wizard.rb', line 167
def advance_step(next_step_name=nil)
current_step.mark_as_visited
before_callback_next_step = current_step.next_step
run_after_callback
after_callback_next_step = current_step.next_step
return if external_redirect_url.present?
requested_next_step = if after_callback_next_step != before_callback_next_step
after_callback_next_step
else
find_step(next_step_name) unless next_step_name.nil?
end
if current_step == last_step && !requested_next_step
raise "Tried to advance beyond the last step of the wizard"
else
if requested_next_step
Rails.logger.debug "Force advancing to step: #{requested_next_step}"
self.current_step = requested_next_step
run_before_callback
else
self.current_step = next_step
run_before_callback
while current_step.skipped?
self.current_step = next_step
run_before_callback
break if current_step == last_step
end
Rails.logger.debug "Advancing to step: #{current_step}"
end
end
end
|
#alias_action_methods ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/hat_trick/wizard.rb', line 222
def alias_action_methods
action_methods = controller.action_methods.reject do |m|
/^render/ =~ m.to_s or
m.to_s.include?('!') or
controller.respond_to?("#{m}_without_hat_trick", :include_private)
end
HatTrick::ControllerHooks.def_action_method_aliases(action_methods)
action_methods.each do |m|
controller.class.send(:alias_method_chain, m, :hat_trick)
controller.class.send(:private, "#{m}_without_hat_trick")
end
end
|
#create_url ⇒ Object
56
57
58
59
60
|
# File 'lib/hat_trick/wizard.rb', line 56
def create_url
wizard_def.configured_create_url or
controller.url_for(:controller => controller.controller_name,
:action => 'create', :only_path => true)
end
|
52
53
54
|
# File 'lib/hat_trick/wizard.rb', line 52
def current_form_method
model_created? ? 'put' : 'post'
end
|
48
49
50
|
# File 'lib/hat_trick/wizard.rb', line 48
def current_form_url
model_created? ? update_url : create_url
end
|
#include_data ⇒ Object
209
210
211
|
# File 'lib/hat_trick/wizard.rb', line 209
def include_data
include_data_for_step(current_step)
end
|
#include_data_for_step(step) ⇒ Object
213
214
215
216
217
218
219
220
|
# File 'lib/hat_trick/wizard.rb', line 213
def include_data_for_step(step)
return {} if model.nil?
inc_data = step.include_data(controller, model)
contents = step.step_contents(controller, model)
inc_data.merge! contents
inc_data.delete_if { |k,v| v.nil? }
inc_data
end
|
#model_created? ⇒ Boolean
44
45
46
|
# File 'lib/hat_trick/wizard.rb', line 44
def model_created?
!(model.nil? || (model.respond_to?(:new_record?) && model.new_record?))
end
|
#next_step ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/hat_trick/wizard.rb', line 73
def next_step
step = find_next_step
while step.skipped? do
step = step_after(step)
end
step
end
|
#override_step_count ⇒ Object
145
146
147
|
# File 'lib/hat_trick/wizard.rb', line 145
def override_step_count
session['hat-trick.override_step_count']
end
|
#override_step_count=(count) ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/hat_trick/wizard.rb', line 137
def override_step_count=(count)
if count.nil?
session.delete('hat-trick.override_step_count')
else
session['hat-trick.override_step_count'] = count
end
end
|
#percent_complete(step = current_step) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/hat_trick/wizard.rb', line 116
def percent_complete(step=current_step)
percent = (steps_before_current.count.to_f / total_step_count) * 100
if percent > 100.0
100
elsif percent <= 0
0
elsif percent < 5.0
5
else
percent
end
end
|
#previously_visited_step ⇒ Object
81
82
83
|
# File 'lib/hat_trick/wizard.rb', line 81
def previously_visited_step
steps_before(current_step).select { |s| s.visited? }.last
end
|
#redirect_to_step(step) ⇒ Object
161
162
163
164
165
|
# File 'lib/hat_trick/wizard.rb', line 161
def redirect_to_step(step)
redirect_from = current_step.fieldset
self.current_step = step
current_step.redirect_from = redirect_from
end
|
#run_after_callback(step = current_step) ⇒ Object
157
158
159
|
# File 'lib/hat_trick/wizard.rb', line 157
def run_after_callback(step=current_step)
step.run_after_callback(controller, model)
end
|
#run_before_callback(step = current_step) ⇒ Object
153
154
155
|
# File 'lib/hat_trick/wizard.rb', line 153
def run_before_callback(step=current_step)
step.run_before_callback(controller, model)
end
|
#session ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/hat_trick/wizard.rb', line 36
def session
if controller.nil?
fake_session
else
controller.session
end
end
|
#skip_step(_step) ⇒ Object
85
86
87
88
|
# File 'lib/hat_trick/wizard.rb', line 85
def skip_step(_step)
step = find_step(_step)
step.skipped = true
end
|
#skipped_steps ⇒ Object
112
113
114
|
# File 'lib/hat_trick/wizard.rb', line 112
def skipped_steps
session["hat-trick.skipped_steps"] ||= []
end
|
#start ⇒ Object
94
95
96
97
98
|
# File 'lib/hat_trick/wizard.rb', line 94
def start
reset_step_session_data
self.current_step ||= first_step
run_before_callback
end
|
#started? ⇒ Boolean
90
91
92
|
# File 'lib/hat_trick/wizard.rb', line 90
def started?
!current_step.nil?
end
|
#steps_after_current ⇒ Object
104
105
106
|
# File 'lib/hat_trick/wizard.rb', line 104
def steps_after_current
steps_after(current_step)
end
|
#steps_before_current ⇒ Object
100
101
102
|
# File 'lib/hat_trick/wizard.rb', line 100
def steps_before_current
steps_before(current_step)
end
|
#steps_remaining ⇒ Object
133
134
135
|
# File 'lib/hat_trick/wizard.rb', line 133
def steps_remaining
total_step_count - steps_before_current.count
end
|
#steps_remaining=(count) ⇒ Object
129
130
131
|
# File 'lib/hat_trick/wizard.rb', line 129
def steps_remaining=(count)
self.override_step_count = steps_before_current.count + count
end
|
#total_step_count ⇒ Object
149
150
151
|
# File 'lib/hat_trick/wizard.rb', line 149
def total_step_count
override_step_count or steps.count
end
|
#update_url ⇒ Object
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/hat_trick/wizard.rb', line 62
def update_url
wizard_def.configured_update_url or
if model_created?
controller.url_for(:controller => controller.controller_name,
:action => 'update', :id => model,
:only_path => true)
else
nil
end
end
|
#visited_steps ⇒ Object
108
109
110
|
# File 'lib/hat_trick/wizard.rb', line 108
def visited_steps
session["hat-trick.visited_steps"] ||= []
end
|