2016-08-25 12:14:56 -05:00
|
|
|
require_dependency 'wizard/step'
|
|
|
|
require_dependency 'wizard/field'
|
|
|
|
|
|
|
|
class Wizard
|
2016-09-09 15:51:07 -05:00
|
|
|
attr_reader :start, :steps, :user
|
2016-08-25 12:14:56 -05:00
|
|
|
|
2016-09-09 15:51:07 -05:00
|
|
|
def initialize(user)
|
2016-08-25 12:14:56 -05:00
|
|
|
@steps = []
|
2016-09-09 15:51:07 -05:00
|
|
|
@user = user
|
2016-08-25 12:14:56 -05:00
|
|
|
end
|
|
|
|
|
2016-09-07 17:04:01 -05:00
|
|
|
def create_step(step_name)
|
|
|
|
Step.new(step_name)
|
2016-08-25 12:14:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def append_step(step)
|
2016-09-07 17:04:01 -05:00
|
|
|
step = create_step(step) if step.is_a?(String)
|
|
|
|
|
|
|
|
yield step if block_given?
|
|
|
|
|
2016-08-25 12:14:56 -05:00
|
|
|
last_step = @steps.last
|
|
|
|
|
|
|
|
@steps << step
|
|
|
|
|
|
|
|
# If it's the first step
|
|
|
|
if @steps.size == 1
|
|
|
|
@start = step
|
|
|
|
step.index = 0
|
|
|
|
elsif last_step.present?
|
|
|
|
last_step.next = step
|
|
|
|
step.previous = last_step
|
|
|
|
step.index = last_step.index + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-09 15:51:07 -05:00
|
|
|
def create_updater(step_id)
|
|
|
|
step = @steps.find {|s| s.id == step_id.dasherize}
|
|
|
|
Wizard::StepUpdater.new(@user, step)
|
2016-08-25 12:14:56 -05:00
|
|
|
end
|
2016-09-09 15:51:07 -05:00
|
|
|
|
2016-08-25 12:14:56 -05:00
|
|
|
end
|