mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
Wizard: Step 1
This commit is contained in:
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