mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Wizard: Step 1
This commit is contained in:
46
lib/wizard.rb
Normal file
46
lib/wizard.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
require_dependency 'wizard/step'
|
||||
require_dependency 'wizard/field'
|
||||
|
||||
class Wizard
|
||||
attr_reader :start
|
||||
attr_reader :steps
|
||||
|
||||
def initialize
|
||||
@steps = []
|
||||
end
|
||||
|
||||
def create_step(args)
|
||||
Step.new(args)
|
||||
end
|
||||
|
||||
def append_step(step)
|
||||
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 self.build
|
||||
wizard = Wizard.new
|
||||
title = wizard.create_step('forum-title')
|
||||
title.add_field(id: 'title', type: 'text', required: true, value: SiteSetting.title)
|
||||
title.add_field(id: 'site_description', type: 'text', required: true, value: SiteSetting.site_description)
|
||||
wizard.append_step(title)
|
||||
|
||||
contact = wizard.create_step('contact')
|
||||
contact.add_field(id: 'contact_email', type: 'text', required: true)
|
||||
wizard.append_step(contact)
|
||||
|
||||
wizard
|
||||
end
|
||||
end
|
||||
15
lib/wizard/field.rb
Normal file
15
lib/wizard/field.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class Wizard
|
||||
class Field
|
||||
attr_reader :id, :type, :required, :value
|
||||
attr_accessor :step
|
||||
|
||||
def initialize(attrs)
|
||||
attrs = attrs || {}
|
||||
|
||||
@id = attrs[:id]
|
||||
@type = attrs[:type]
|
||||
@required = !!attrs[:required]
|
||||
@value = attrs[:value]
|
||||
end
|
||||
end
|
||||
end
|
||||
18
lib/wizard/step.rb
Normal file
18
lib/wizard/step.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
class Wizard
|
||||
class Step
|
||||
attr_reader :id
|
||||
attr_accessor :index, :fields, :next, :previous
|
||||
|
||||
def initialize(id)
|
||||
@id = id
|
||||
@fields = []
|
||||
end
|
||||
|
||||
def add_field(attrs)
|
||||
field = Field.new(attrs)
|
||||
field.step = self
|
||||
@fields << field
|
||||
field
|
||||
end
|
||||
end
|
||||
end
|
||||
42
lib/wizard/step_updater.rb
Normal file
42
lib/wizard/step_updater.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
class Wizard
|
||||
class StepUpdater
|
||||
|
||||
attr_accessor :errors
|
||||
|
||||
def initialize(current_user, id)
|
||||
@current_user = current_user
|
||||
@id = id
|
||||
@errors = []
|
||||
end
|
||||
|
||||
def update(fields)
|
||||
updater_method = "update_#{@id.underscore}".to_sym
|
||||
|
||||
if respond_to?(updater_method)
|
||||
send(updater_method, fields.symbolize_keys)
|
||||
else
|
||||
raise Discourse::InvalidAccess.new
|
||||
end
|
||||
end
|
||||
|
||||
def update_forum_title(fields)
|
||||
update_setting(:title, fields, :title)
|
||||
update_setting(:site_description, fields, :site_description)
|
||||
end
|
||||
|
||||
def success?
|
||||
@errors.blank?
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def update_setting(id, fields, field_id)
|
||||
value = fields[field_id]
|
||||
value.strip! if value.is_a?(String)
|
||||
SiteSetting.set_and_log(id, value, @current_user)
|
||||
rescue Discourse::InvalidParameters => e
|
||||
@errors << {field: field_id, description: e.message }
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user