From 2545c2ffa6e13bbd439535f23197137b860b381b Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 20 Sep 2016 17:01:54 -0400 Subject: [PATCH] Add new welcome message step --- .../components/wizard-field-textarea.js.es6 | 5 +++ .../wizard/components/wizard-step.js.es6 | 2 +- .../components/wizard-field-textarea.hbs | 1 + app/assets/stylesheets/wizard.scss | 15 +++++++ config/locales/server.en.yml | 9 ++++ lib/introduction_updater.rb | 42 +++++++++++++++++++ lib/wizard/builder.rb | 18 ++++++++ spec/components/step_updater_spec.rb | 15 +++++++ 8 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/wizard/components/wizard-field-textarea.js.es6 create mode 100644 app/assets/javascripts/wizard/templates/components/wizard-field-textarea.hbs create mode 100644 lib/introduction_updater.rb diff --git a/app/assets/javascripts/wizard/components/wizard-field-textarea.js.es6 b/app/assets/javascripts/wizard/components/wizard-field-textarea.js.es6 new file mode 100644 index 000000000..3747a92bc --- /dev/null +++ b/app/assets/javascripts/wizard/components/wizard-field-textarea.js.es6 @@ -0,0 +1,5 @@ +export default Ember.Component.extend({ + keyPress(e) { + e.stopPropagation(); + } +}); diff --git a/app/assets/javascripts/wizard/components/wizard-step.js.es6 b/app/assets/javascripts/wizard/components/wizard-step.js.es6 index d49a69c5f..b0a0618ab 100644 --- a/app/assets/javascripts/wizard/components/wizard-step.js.es6 +++ b/app/assets/javascripts/wizard/components/wizard-step.js.es6 @@ -74,7 +74,7 @@ export default Ember.Component.extend({ }, animateInvalidFields() { - Ember.run.scheduleOnce('afterRender', () => $('.invalid input[type=text]').wiggle(2, 100)); + Ember.run.scheduleOnce('afterRender', () => $('.invalid input[type=text], .invalid textarea').wiggle(2, 100)); }, advance() { diff --git a/app/assets/javascripts/wizard/templates/components/wizard-field-textarea.hbs b/app/assets/javascripts/wizard/templates/components/wizard-field-textarea.hbs new file mode 100644 index 000000000..8e2c042af --- /dev/null +++ b/app/assets/javascripts/wizard/templates/components/wizard-field-textarea.hbs @@ -0,0 +1 @@ +{{textarea elementId=field.id value=field.value class=fieldClass placeholder=field.placeholder}} diff --git a/app/assets/stylesheets/wizard.scss b/app/assets/stylesheets/wizard.scss index a945a314f..ddd184b62 100644 --- a/app/assets/stylesheets/wizard.scss +++ b/app/assets/stylesheets/wizard.scss @@ -334,6 +334,21 @@ body.wizard { } } +.textarea-field { + textarea { + width: 100%; + height: 10em; + } + + &.invalid { + textarea { + padding: 3px; + border: 4px solid red; + border-radius: 3px; + } + } +} + .text-field { input { width: 100%; diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 365ccf96c..6fbf44c19 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -3238,6 +3238,15 @@ en: label: "How would you describe your community in one sentence?" placeholder: "A place for Jane and her friends to discuss cool stuff" + introduction: + title: "Introduction" + + fields: + welcome: + label: "Welcome Topic" + description: 'Think of this as your "elevator pitch" – how would you describe this site to a stranger on an elevator when you had about 1 minute to talk?' + one_paragraph: "Please restrict your welcome message to one paragraph." + privacy: title: "Access" description: "Is your community open to everyone, or is it restricted by membership, invitation, or approval? If you prefer, you can set things up privately, then switch to public later." diff --git a/lib/introduction_updater.rb b/lib/introduction_updater.rb new file mode 100644 index 000000000..a576b0433 --- /dev/null +++ b/lib/introduction_updater.rb @@ -0,0 +1,42 @@ +class IntroductionUpdater + + def initialize(user) + @user = user + end + + def get_summary + summary_from_post(find_welcome_post) + end + + def update_summary(new_value) + post = find_welcome_post + return unless post + + previous_value = summary_from_post(post).strip + + if previous_value != new_value + revisor = PostRevisor.new(post) + + remaining = post.raw.split("\n")[1..-1] + revisor.revise!(@user, raw: "#{new_value}\n#{remaining.join("\n")}") + end + + end + +protected + + def summary_from_post(post) + return post ? post.raw.split("\n").first : nil + end + + def find_welcome_post + welcome_topic = Topic.where(slug: 'welcome-to-discourse').first + return nil unless welcome_topic.present? + + post = welcome_topic.posts.where(post_number: 1).first + return nil unless post.present? + + post + end + +end diff --git a/lib/wizard/builder.rb b/lib/wizard/builder.rb index ba6901422..64d8476cd 100644 --- a/lib/wizard/builder.rb +++ b/lib/wizard/builder.rb @@ -1,3 +1,5 @@ +require_dependency 'introduction_updater' + class Wizard class Builder @@ -40,6 +42,22 @@ class Wizard end end + @wizard.append_step('introduction') do |step| + introduction = IntroductionUpdater.new(@wizard.user) + + step.add_field(id: 'welcome', type: 'textarea', required: true, value: introduction.get_summary) + + step.on_update do |updater| + value = updater.fields[:welcome].strip + + if value.index("\n") + updater.errors.add(:welcome, I18n.t("wizard.step.introduction.fields.welcome.one_paragraph")) + else + introduction.update_summary(value) + end + end + end + @wizard.append_step('privacy') do |step| locked = SiteSetting.login_required? && SiteSetting.invite_only? privacy = step.add_field(id: 'privacy', diff --git a/spec/components/step_updater_spec.rb b/spec/components/step_updater_spec.rb index 66dc33bdc..ab383cf78 100644 --- a/spec/components/step_updater_spec.rb +++ b/spec/components/step_updater_spec.rb @@ -38,6 +38,21 @@ describe Wizard::StepUpdater do expect(wizard.completed_steps?('forum-title')).to eq(true) end + it "updates the introduction step" do + topic = Fabricate(:topic, title: "Welcome to Discourse") + welcome_post = Fabricate(:post, topic: topic, raw: "this will be the welcome topic post\n\ncool!") + + updater = wizard.create_updater('introduction', welcome: "Welcome to my new awesome forum!") + updater.update + + expect(updater.success?).to eq(true) + welcome_post.reload + expect(welcome_post.raw).to eq("Welcome to my new awesome forum!\n\ncool!") + + expect(wizard.completed_steps?('introduction')).to eq(true) + + end + it "won't allow updates to the default value, when required" do updater = wizard.create_updater('forum_title', title: SiteSetting.title, site_description: 'neat place') updater.update