discourse/lib/wizard.rb

42 lines
788 B
Ruby
Raw Normal View History

2016-08-25 13:14:56 -04:00
require_dependency 'wizard/step'
require_dependency 'wizard/field'
class Wizard
attr_reader :start, :steps, :user
2016-08-25 13:14:56 -04:00
def initialize(user)
2016-08-25 13:14:56 -04:00
@steps = []
@user = user
2016-08-25 13:14:56 -04:00
end
2016-09-07 18:04:01 -04:00
def create_step(step_name)
Step.new(step_name)
2016-08-25 13:14:56 -04:00
end
def append_step(step)
2016-09-07 18:04:01 -04:00
step = create_step(step) if step.is_a?(String)
yield step if block_given?
2016-08-25 13:14:56 -04: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
def create_updater(step_id, fields)
step = @steps.find {|s| s.id == step_id.dasherize}
Wizard::StepUpdater.new(@user, step, fields)
2016-08-25 13:14:56 -04:00
end
2016-08-25 13:14:56 -04:00
end