-
Notifications
You must be signed in to change notification settings - Fork 184
/
wizard.rb
83 lines (67 loc) · 2.39 KB
/
wizard.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Wicked
module Wizard
extend ActiveSupport::Concern
class InvalidStepError < RuntimeError
def initialize
super "The requested step did not match any steps defined for this controller."
end
end
class UndefinedStepsError < RuntimeError
def initialize
super "No step definitions have been supplied; if setting via `before_action`, use `prepend_before_action`"
end
end
# Include the modules!!
include Wicked::Controller::Concerns::Path
include Wicked::Controller::Concerns::RenderRedirect
include Wicked::Controller::Concerns::Steps
included do
include Wicked::Controller::Concerns::Action
# Give our Views helper methods!
helper_method :wizard_path, :next_wizard_path, :previous_wizard_path,
:step, :wizard_steps, :current_step?,
:past_step?, :future_step?, :previous_step?,
:next_step?
# Set @step and @next_step variables
before_action :initialize_wicked_variables, :setup_wizard
end
# forward to first step with whatever params are provided
def index
redirect_to wizard_path(steps.first, request.query_parameters)
end
# returns the canonical value for a step name, needed for translation support
def wizard_value(step_name)
step_name
end
private def initialize_wicked_variables
@skip_to = nil
@wicked_redirect_params = nil
end
private def check_redirect_to_first_last!(step)
redirect_to wizard_path(steps.first) if step.to_s == Wicked::FIRST_STEP
redirect_to wizard_path(steps.last) if step.to_s == Wicked::LAST_STEP
end
private def setup_step_from(the_step)
return if steps.nil?
the_step ||= steps.first
check_redirect_to_first_last!(the_step)
valid_steps = steps + self.class::PROTECTED_STEPS
the_step = valid_steps.detect { |stp| stp.to_s == the_step }
raise InvalidStepError if the_step.nil?
the_step
end
private def check_steps!
raise UndefinedStepsError if steps.nil?
end
private def set_previous_next(step)
@previous_step = previous_step(step)
@next_step = next_step(step)
end
private def setup_wizard
check_steps!
return if params[:id].nil?
@step = setup_step_from(params[:id])
set_previous_next(@step)
end
end
end