diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index 8324eca1fa1..c65ff16cf95 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -9,8 +9,8 @@ class StepsController < ApplicationController def update wizard = Wizard::Builder.new(current_user).build - updater = wizard.create_updater(params[:id]) - updater.update(params[:fields]) + updater = wizard.create_updater(params[:id], params[:fields]) + updater.update if updater.success? result = { success: 'OK' } diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 1396ffc5993..1ba5f441c4d 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1410,6 +1410,10 @@ en: suppress_overlapping_tags_in_list: "If tags match exact words in topic titles, don't show the tag" remove_muted_tags_from_latest: "Don't show topics tagged with muted tags in the latest topic list." + company_short_name: "Company Name (short)" + company_full_name: "Company Name (full)" + company_domain: "Company Domain" + errors: invalid_email: "Invalid email address." invalid_username: "There's no user with that username." @@ -3265,6 +3269,22 @@ en: site_contact_username: label: "Site Contact Username" description: "All automated messages will be sent from this user." + + corporate: + title: "About your Company" + description: "If you don't have a company, feel free to leave this step blank." + + fields: + company_short_name: + label: "Company Name (short)" + placeholder: "PP Inc." + company_full_name: + label: "Company Name (full)" + placeholder: "Pied Piper Incorporated" + company_domain: + label: "Company Domain" + placeholder: "piedpiper.com" + colors: title: "Choose a Theme" fields: diff --git a/config/site_settings.yml b/config/site_settings.yml index 8c35fdf473d..9e19d79b358 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -60,6 +60,12 @@ required: exclude_rel_nofollow_domains: default: '' type: list + company_short_name: + default: '' + company_full_name: + default: '' + company_domain: + default: '' basic: default_locale: diff --git a/lib/wizard.rb b/lib/wizard.rb index 5ed976ccaf1..5413263b7a1 100644 --- a/lib/wizard.rb +++ b/lib/wizard.rb @@ -33,9 +33,9 @@ class Wizard end end - def create_updater(step_id) + def create_updater(step_id, fields) step = @steps.find {|s| s.id == step_id.dasherize} - Wizard::StepUpdater.new(@user, step) + Wizard::StepUpdater.new(@user, step, fields) end end diff --git a/lib/wizard/builder.rb b/lib/wizard/builder.rb index 28bb6c34e38..addcccf5509 100644 --- a/lib/wizard/builder.rb +++ b/lib/wizard/builder.rb @@ -16,10 +16,10 @@ class Wizard languages.add_choice(locale[:value], label: locale[:name]) end - step.on_update do |updater, fields| + step.on_update do |updater| old_locale = SiteSetting.default_locale - updater.update_setting_field(:default_locale, fields, :default_locale) - updater.refresh_required = true if old_locale != fields[:default_locale] + updater.apply_setting(:default_locale) + updater.refresh_required = true if old_locale != updater.fields[:default_locale] end end @@ -27,9 +27,8 @@ class Wizard 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) + step.on_update do |updater| + updater.apply_settings(:title, :site_description) end end @@ -42,9 +41,9 @@ class Wizard 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') + step.on_update do |updater| + updater.update_setting(:login_required, updater.fields[:privacy] == 'restricted') + updater.update_setting(:invite_only, updater.fields[:privacy] == 'restricted') end end @@ -53,10 +52,31 @@ class Wizard 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) + step.on_update do |updater| + updater.apply_settings(:contact_email, :contact_url, :site_contact_username) + end + end + + @wizard.append_step('corporate') do |step| + step.add_field(id: 'company_short_name', type: 'text', value: SiteSetting.company_short_name) + step.add_field(id: 'company_full_name', type: 'text', value: SiteSetting.company_full_name) + step.add_field(id: 'company_domain', type: 'text', value: SiteSetting.company_domain) + + step.on_update do |updater| + + tos_post = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).first + if tos_post.present? + raw = tos_post.raw.dup + + replace_company(updater, raw, 'company_full_name') + replace_company(updater, raw, 'company_short_name') + replace_company(updater, raw, 'company_domain') + + revisor = PostRevisor.new(tos_post) + revisor.revise!(@wizard.user, raw: raw) + end + + updater.apply_settings(:company_short_name, :company_full_name, :company_domain) end end @@ -68,8 +88,8 @@ class Wizard 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] + step.on_update do |updater| + scheme_name = updater.fields[:theme_id] theme = ColorScheme.themes.find {|s| s[:id] == scheme_name } @@ -104,18 +124,28 @@ class Wizard 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) + step.on_update do |updater| + updater.apply_settings(:logo_url, :logo_small_url, :favicon_url, :apple_touch_icon_url) end end + DiscourseEvent.trigger(:build_wizard, @wizard) + @wizard.append_step('finished') @wizard end + protected + + def replace_company(updater, raw, field_name) + old_value = SiteSetting.send(field_name) + old_value = field_name if old_value.blank? + + new_value = updater.fields[field_name.to_sym] + new_value = field_name if new_value.blank? + + raw.gsub!(old_value, new_value) + end end end diff --git a/lib/wizard/step_updater.rb b/lib/wizard/step_updater.rb index 61c9e7f8041..4cd4e6a7fdd 100644 --- a/lib/wizard/step_updater.rb +++ b/lib/wizard/step_updater.rb @@ -2,16 +2,17 @@ class Wizard class StepUpdater include ActiveModel::Model - attr_accessor :refresh_required + attr_accessor :refresh_required, :fields - def initialize(current_user, step) + def initialize(current_user, step, fields) @current_user = current_user @step = step @refresh_required = false + @fields = fields end - def update(fields) - @step.updater.call(self, fields) if @step.updater.present? + def update + @step.updater.call(self) if @step.updater.present? end def success? @@ -27,10 +28,14 @@ class Wizard 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]) + def apply_setting(id) + update_setting(id, @fields[id]) rescue Discourse::InvalidParameters => e - errors.add(field_id, e.message) + errors.add(id, e.message) + end + + def apply_settings(*ids) + ids.each {|id| apply_setting(id)} end end diff --git a/spec/components/step_updater_spec.rb b/spec/components/step_updater_spec.rb index dc5f2cadd9b..4591bffab77 100644 --- a/spec/components/step_updater_spec.rb +++ b/spec/components/step_updater_spec.rb @@ -8,23 +8,24 @@ describe Wizard::StepUpdater do let(:wizard) { Wizard::Builder.new(user).build } context "locale" do - let(:updater) { wizard.create_updater('locale') } it "does not require refresh when the language stays the same" do - updater.update(default_locale: 'en') + updater = wizard.create_updater('locale', default_locale: 'en') + updater.update expect(updater.refresh_required?).to eq(false) end it "updates the locale and requires refresh when it does change" do - updater.update(default_locale: 'ru') + updater = wizard.create_updater('locale', default_locale: 'ru') + updater.update expect(SiteSetting.default_locale).to eq('ru') expect(updater.refresh_required?).to eq(true) end end it "updates the forum title step" do - updater = wizard.create_updater('forum_title') - updater.update(title: 'new forum title', site_description: 'neat place') + updater = wizard.create_updater('forum_title', title: 'new forum title', site_description: 'neat place') + updater.update expect(updater.success?).to eq(true) expect(SiteSetting.title).to eq("new forum title") @@ -32,17 +33,17 @@ describe Wizard::StepUpdater do end context "privacy settings" do - let(:updater) { wizard.create_updater('privacy') } - it "updates to open correctly" do - updater.update(privacy: 'open') + updater = wizard.create_updater('privacy', privacy: 'open') + updater.update expect(updater.success?).to eq(true) expect(SiteSetting.login_required?).to eq(false) expect(SiteSetting.invite_only?).to eq(false) end it "updates to private correctly" do - updater.update(privacy: 'restricted') + updater = wizard.create_updater('privacy', privacy: 'restricted') + updater.update expect(updater.success?).to eq(true) expect(SiteSetting.login_required?).to eq(true) expect(SiteSetting.invite_only?).to eq(true) @@ -50,13 +51,13 @@ describe Wizard::StepUpdater do end context "contact step" do - let(:updater) { wizard.create_updater('contact') } - it "updates the fields correctly" do - updater.update(contact_email: 'eviltrout@example.com', - contact_url: 'http://example.com/custom-contact-url', - site_contact_username: user.username) + updater = wizard.create_updater('contact', + contact_email: 'eviltrout@example.com', + contact_url: 'http://example.com/custom-contact-url', + site_contact_username: user.username) + updater.update expect(updater).to be_success expect(SiteSetting.contact_email).to eq("eviltrout@example.com") expect(SiteSetting.contact_url).to eq("http://example.com/custom-contact-url") @@ -64,33 +65,71 @@ describe Wizard::StepUpdater do end it "doesn't update when there are errors" do - updater.update(contact_email: 'not-an-email', - site_contact_username: 'not-a-username') + updater = wizard.create_updater('contact', + contact_email: 'not-an-email', + site_contact_username: 'not-a-username') + updater.update expect(updater).to_not be_success expect(updater.errors).to be_present end end - context "colors step" do - let(:updater) { wizard.create_updater('colors') } + context "corporate step" do + it "updates the fields properly" do + + p = Fabricate(:post, raw: 'company_domain - company_full_name - company_short_name template') + SiteSetting.tos_topic_id = p.topic_id + + updater = wizard.create_updater('corporate', + company_short_name: 'ACME', + company_full_name: 'ACME, Inc.', + company_domain: 'acme.com') + updater.update + expect(updater).to be_success + expect(SiteSetting.company_short_name).to eq("ACME") + expect(SiteSetting.company_full_name).to eq("ACME, Inc.") + expect(SiteSetting.company_domain).to eq("acme.com") + + # Should update the TOS topic + raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck(:raw).first + expect(raw).to eq("acme.com - ACME, Inc. - ACME template") + + # Can update the TOS topic again + updater = wizard.create_updater('corporate', + company_short_name: 'PPI', + company_full_name: 'Pied Piper Inc', + company_domain: 'piedpiper.com') + updater.update + raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck(:raw).first + expect(raw).to eq("piedpiper.com - Pied Piper Inc - PPI template") + + # Can update the TOS to nothing + updater = wizard.create_updater('corporate', {}) + updater.update + raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck(:raw).first + expect(raw).to eq("company_domain - company_full_name - company_short_name template") + end + end + + context "colors step" do context "with an existing color scheme" do let!(:color_scheme) { Fabricate(:color_scheme, name: 'existing', via_wizard: true) } it "updates the scheme" do - updater.update(theme_id: 'dark') + updater = wizard.create_updater('colors', theme_id: 'dark') + updater.update expect(updater.success?).to eq(true) color_scheme.reload expect(color_scheme).to be_enabled - end end context "without an existing scheme" do - it "creates the scheme" do - updater.update(theme_id: 'dark') + updater = wizard.create_updater('colors', theme_id: 'dark') + updater.update expect(updater.success?).to eq(true) color_scheme = ColorScheme.where(via_wizard: true).first @@ -102,15 +141,14 @@ describe Wizard::StepUpdater do end context "logos step" do - let(:updater) { wizard.create_updater('logos') } it "updates the fields correctly" do - updater.update( - logo_url: '/uploads/logo.png', - logo_small_url: '/uploads/logo-small.png', - favicon_url: "/uploads/favicon.png", - apple_touch_icon_url: "/uploads/apple.png" - ) + updater = wizard.create_updater('logos', + logo_url: '/uploads/logo.png', + logo_small_url: '/uploads/logo-small.png', + favicon_url: "/uploads/favicon.png", + apple_touch_icon_url: "/uploads/apple.png") + updater.update expect(updater).to be_success expect(SiteSetting.logo_url).to eq('/uploads/logo.png') diff --git a/spec/components/wizard_step_spec.rb b/spec/components/wizard_step_spec.rb index 54f95072212..45005b89fa2 100644 --- a/spec/components/wizard_step_spec.rb +++ b/spec/components/wizard_step_spec.rb @@ -3,7 +3,7 @@ require 'wizard' describe Wizard::Step do - let(:wizard) { Wizard.new } + let(:wizard) { Wizard.new(Fabricate.build(:user)) } let(:step) { wizard.create_step('test-step') } it "supports fields and options" do