Clean up wizard updater API for better plugin use

This commit is contained in:
Robin Ward 2016-09-09 16:51:07 -04:00
parent e3640ee5f6
commit 28b6c300a0
8 changed files with 164 additions and 141 deletions

View file

@ -1,4 +1,5 @@
require_dependency 'wizard'
require_dependency 'wizard/builder'
require_dependency 'wizard/step_updater'
class StepsController < ApplicationController
@ -7,7 +8,8 @@ class StepsController < ApplicationController
before_filter :ensure_staff
def update
updater = Wizard::StepUpdater.new(current_user, params[:id])
wizard = Wizard::Builder.new(current_user).build
updater = wizard.create_updater(params[:id])
updater.update(params[:fields])
if updater.success?

View file

@ -1,4 +1,5 @@
require_dependency 'wizard'
require_dependency 'wizard/builder'
class WizardController < ApplicationController
@ -12,7 +13,7 @@ class WizardController < ApplicationController
def index
respond_to do |format|
format.json do
wizard = Wizard.build
wizard = Wizard::Builder.new(current_user).build
render_serialized(wizard, WizardSerializer)
end
format.html {}

View file

@ -2,11 +2,11 @@ require_dependency 'wizard/step'
require_dependency 'wizard/field'
class Wizard
attr_reader :start
attr_reader :steps
attr_reader :start, :steps, :user
def initialize
def initialize(user)
@steps = []
@user = user
end
def create_step(step_name)
@ -33,60 +33,9 @@ class Wizard
end
end
def self.build
wizard = Wizard.new
wizard.append_step('locale') do |step|
languages = step.add_field(id: 'default_locale',
type: 'dropdown',
required: true,
value: SiteSetting.default_locale)
LocaleSiteSetting.values.each do |locale|
languages.add_choice(locale[:value], label: locale[:name])
end
end
wizard.append_step('forum-title') do |step|
step.add_field(id: 'title', type: 'text', required: true, value: SiteSetting.title)
step.add_field(id: 'site_description', type: 'text', required: true, value: SiteSetting.site_description)
end
wizard.append_step('privacy') do |step|
locked = SiteSetting.login_required? && SiteSetting.invite_only?
privacy = step.add_field(id: 'privacy',
type: 'radio',
required: true,
value: locked ? 'restricted' : 'open')
privacy.add_choice('open', icon: 'unlock')
privacy.add_choice('restricted', icon: 'lock')
end
wizard.append_step('contact') do |step|
step.add_field(id: 'contact_email', type: 'text', required: true, value: SiteSetting.contact_email)
step.add_field(id: 'contact_url', type: 'text', value: SiteSetting.contact_url)
step.add_field(id: 'site_contact_username', type: 'text', value: SiteSetting.site_contact_username)
end
wizard.append_step('colors') do |step|
theme_id = ColorScheme.where(via_wizard: true).pluck(:theme_id)
theme_id = theme_id.present? ? theme_id[0] : 'default'
themes = step.add_field(id: 'theme_id', type: 'dropdown', required: true, value: theme_id)
ColorScheme.themes.each {|t| themes.add_choice(t[:id], data: t) }
step.add_field(id: 'theme_preview', type: 'component')
end
wizard.append_step('logos') do |step|
step.add_field(id: 'logo_url', type: 'image', value: SiteSetting.logo_url)
step.add_field(id: 'logo_small_url', type: 'image', value: SiteSetting.logo_small_url)
step.add_field(id: 'favicon_url', type: 'image', value: SiteSetting.favicon_url)
step.add_field(id: 'apple_touch_icon_url', type: 'image', value: SiteSetting.apple_touch_icon_url)
end
wizard.append_step('finished')
wizard
def create_updater(step_id)
step = @steps.find {|s| s.id == step_id.dasherize}
Wizard::StepUpdater.new(@user, step)
end
end

121
lib/wizard/builder.rb Normal file
View file

@ -0,0 +1,121 @@
class Wizard
class Builder
def initialize(user)
@wizard = Wizard.new(user)
end
def build
@wizard.append_step('locale') do |step|
languages = step.add_field(id: 'default_locale',
type: 'dropdown',
required: true,
value: SiteSetting.default_locale)
LocaleSiteSetting.values.each do |locale|
languages.add_choice(locale[:value], label: locale[:name])
end
step.on_update do |updater, fields|
old_locale = SiteSetting.default_locale
updater.update_setting_field(:default_locale, fields, :default_locale)
updater.refresh_required = true if old_locale != fields[:default_locale]
end
end
@wizard.append_step('forum-title') do |step|
step.add_field(id: 'title', type: 'text', required: true, value: SiteSetting.title)
step.add_field(id: 'site_description', type: 'text', required: true, value: SiteSetting.site_description)
step.on_update do |updater, fields|
updater.update_setting_field(:title, fields, :title)
updater.update_setting_field(:site_description, fields, :site_description)
end
end
@wizard.append_step('privacy') do |step|
locked = SiteSetting.login_required? && SiteSetting.invite_only?
privacy = step.add_field(id: 'privacy',
type: 'radio',
required: true,
value: locked ? 'restricted' : 'open')
privacy.add_choice('open', icon: 'unlock')
privacy.add_choice('restricted', icon: 'lock')
step.on_update do |updater, fields|
updater.update_setting(:login_required, fields[:privacy] == 'restricted')
updater.update_setting(:invite_only, fields[:privacy] == 'restricted')
end
end
@wizard.append_step('contact') do |step|
step.add_field(id: 'contact_email', type: 'text', required: true, value: SiteSetting.contact_email)
step.add_field(id: 'contact_url', type: 'text', value: SiteSetting.contact_url)
step.add_field(id: 'site_contact_username', type: 'text', value: SiteSetting.site_contact_username)
step.on_update do |updater, fields|
updater.update_setting_field(:contact_email, fields, :contact_email)
updater.update_setting_field(:contact_url, fields, :contact_url)
updater.update_setting_field(:site_contact_username, fields, :site_contact_username)
end
end
@wizard.append_step('colors') do |step|
theme_id = ColorScheme.where(via_wizard: true).pluck(:theme_id)
theme_id = theme_id.present? ? theme_id[0] : 'default'
themes = step.add_field(id: 'theme_id', type: 'dropdown', required: true, value: theme_id)
ColorScheme.themes.each {|t| themes.add_choice(t[:id], data: t) }
step.add_field(id: 'theme_preview', type: 'component')
step.on_update do |updater, fields|
scheme_name = fields[:theme_id]
theme = ColorScheme.themes.find {|s| s[:id] == scheme_name }
colors = []
theme[:colors].each do |name, hex|
colors << {name: name, hex: hex[1..-1] }
end
attrs = {
enabled: true,
name: I18n.t("wizard.step.colors.fields.color_scheme.options.#{scheme_name}"),
colors: colors,
theme_id: scheme_name
}
scheme = ColorScheme.where(via_wizard: true).first
if scheme.present?
attrs[:colors] = colors
revisor = ColorSchemeRevisor.new(scheme, attrs)
revisor.revise
else
attrs[:via_wizard] = true
scheme = ColorScheme.new(attrs)
scheme.save!
end
end
end
@wizard.append_step('logos') do |step|
step.add_field(id: 'logo_url', type: 'image', value: SiteSetting.logo_url)
step.add_field(id: 'logo_small_url', type: 'image', value: SiteSetting.logo_small_url)
step.add_field(id: 'favicon_url', type: 'image', value: SiteSetting.favicon_url)
step.add_field(id: 'apple_touch_icon_url', type: 'image', value: SiteSetting.apple_touch_icon_url)
step.on_update do |updater, fields|
updater.update_setting_field(:logo_url, fields, :logo_url)
updater.update_setting_field(:logo_small_url, fields, :logo_small_url)
updater.update_setting_field(:favicon_url, fields, :favicon_url)
updater.update_setting_field(:apple_touch_icon_url, fields, :apple_touch_icon_url)
end
end
@wizard.append_step('finished')
@wizard
end
end
end

View file

@ -1,6 +1,6 @@
class Wizard
class Step
attr_reader :id
attr_reader :id, :updater
attr_accessor :index, :fields, :next, :previous
def initialize(id)
@ -14,5 +14,9 @@ class Wizard
@fields << field
field
end
def on_update(&block)
@updater = block
end
end
end

View file

@ -2,73 +2,16 @@ class Wizard
class StepUpdater
include ActiveModel::Model
def initialize(current_user, id)
attr_accessor :refresh_required
def initialize(current_user, step)
@current_user = current_user
@id = id
@step = step
@refresh_required = false
end
def update(fields)
updater_method = "update_#{@id.underscore}".to_sym
send(updater_method, fields.symbolize_keys) if respond_to?(updater_method)
end
def update_locale(fields)
old_locale = SiteSetting.default_locale
update_setting_field(:default_locale, fields, :default_locale)
@refresh_required = true if old_locale != fields[:default_locale]
end
def update_privacy(fields)
update_setting(:login_required, fields[:privacy] == 'restricted')
update_setting(:invite_only, fields[:privacy] == 'restricted')
end
def update_forum_title(fields)
update_setting_field(:title, fields, :title)
update_setting_field(:site_description, fields, :site_description)
end
def update_contact(fields)
update_setting_field(:contact_email, fields, :contact_email)
update_setting_field(:contact_url, fields, :contact_url)
update_setting_field(:site_contact_username, fields, :site_contact_username)
end
def update_colors(fields)
scheme_name = fields[:theme_id]
theme = ColorScheme.themes.find {|s| s[:id] == scheme_name }
colors = []
theme[:colors].each do |name, hex|
colors << {name: name, hex: hex[1..-1] }
end
attrs = {
enabled: true,
name: I18n.t("wizard.step.colors.fields.color_scheme.options.#{scheme_name}"),
colors: colors,
theme_id: scheme_name
}
scheme = ColorScheme.where(via_wizard: true).first
if scheme.present?
attrs[:colors] = colors
revisor = ColorSchemeRevisor.new(scheme, attrs)
revisor.revise
else
attrs[:via_wizard] = true
scheme = ColorScheme.new(attrs)
scheme.save!
end
end
def update_logos(fields)
update_setting_field(:logo_url, fields, :logo_url)
update_setting_field(:logo_small_url, fields, :logo_small_url)
update_setting_field(:favicon_url, fields, :favicon_url)
update_setting_field(:apple_touch_icon_url, fields, :apple_touch_icon_url)
@step.updater.call(self, fields) if @step.updater.present?
end
def success?
@ -79,18 +22,16 @@ class Wizard
@refresh_required
end
protected
def update_setting(id, value)
value.strip! if value.is_a?(String)
SiteSetting.set_and_log(id, value, @current_user) if SiteSetting.send(id) != value
end
def update_setting(id, value)
value.strip! if value.is_a?(String)
SiteSetting.set_and_log(id, value, @current_user) if SiteSetting.send(id) != value
end
def update_setting_field(id, fields, field_id)
update_setting(id, fields[field_id])
rescue Discourse::InvalidParameters => e
errors.add(field_id, e.message)
end
def update_setting_field(id, fields, field_id)
update_setting(id, fields[field_id])
rescue Discourse::InvalidParameters => e
errors.add(field_id, e.message)
end
end
end

View file

@ -1,11 +1,14 @@
require 'rails_helper'
require_dependency 'wizard'
require_dependency 'wizard/builder'
require_dependency 'wizard/step_updater'
describe Wizard::StepUpdater do
let(:user) { Fabricate(:admin) }
let(:wizard) { Wizard::Builder.new(user).build }
context "locale" do
let(:updater) { Wizard::StepUpdater.new(user, 'locale') }
let(:updater) { wizard.create_updater('locale') }
it "does not require refresh when the language stays the same" do
updater.update(default_locale: 'en')
@ -20,7 +23,7 @@ describe Wizard::StepUpdater do
end
it "updates the forum title step" do
updater = Wizard::StepUpdater.new(user, 'forum_title')
updater = wizard.create_updater('forum_title')
updater.update(title: 'new forum title', site_description: 'neat place')
expect(updater.success?).to eq(true)
@ -29,7 +32,7 @@ describe Wizard::StepUpdater do
end
context "privacy settings" do
let(:updater) { Wizard::StepUpdater.new(user, 'privacy') }
let(:updater) { wizard.create_updater('privacy') }
it "updates to open correctly" do
updater.update(privacy: 'open')
@ -47,7 +50,7 @@ describe Wizard::StepUpdater do
end
context "contact step" do
let(:updater) { Wizard::StepUpdater.new(user, 'contact') }
let(:updater) { wizard.create_updater('contact') }
it "updates the fields correctly" do
updater.update(contact_email: 'eviltrout@example.com',
@ -69,7 +72,7 @@ describe Wizard::StepUpdater do
end
context "colors step" do
let(:updater) { Wizard::StepUpdater.new(user, 'colors') }
let(:updater) { wizard.create_updater('colors') }
context "with an existing color scheme" do
let!(:color_scheme) { Fabricate(:color_scheme, name: 'existing', via_wizard: true) }
@ -99,7 +102,7 @@ describe Wizard::StepUpdater do
end
context "logos step" do
let(:updater) { Wizard::StepUpdater.new(user, 'logos') }
let(:updater) { wizard.create_updater('logos') }
it "updates the fields correctly" do
updater.update(

View file

@ -3,11 +3,13 @@ require 'wizard'
describe Wizard do
let(:wizard) { Wizard.new }
let(:user) { Fabricate.build(:user) }
let(:wizard) { Wizard.new(user) }
it "has default values" do
expect(wizard.start).to be_blank
expect(wizard.steps).to be_empty
expect(wizard.user).to be_present
end
describe "append_step" do