FIX: Wizard tries harder to find existing Welcome Topic

The wizard searches for:

* a topic that with the "is_welcome_topic" custom field
* a topic with the correct slug for the current default locale
* a topic with the correct slug for the English locale
* the oldest globally pinned topic

It gives up if it didn't find any of the above.
This commit is contained in:
Gerhard Schlager 2018-12-05 18:05:37 +01:00
parent 6cf2e64e44
commit 43cfdb1cb9
6 changed files with 95 additions and 15 deletions

View File

@ -39,9 +39,8 @@ class WizardStepSerializer < ApplicationSerializer
end
def description
return translate("disabled") if object.disabled
translate("description", base_path: Discourse.base_path)
key = object.disabled ? "disabled" : "description"
translate(key, object.description_vars)
end
def include_description?

View File

@ -4049,10 +4049,10 @@ en:
introduction:
title: "Introduction"
disabled: "<p>We couldnt find any topic with the title “Welcome to Discourse”.</p>
disabled: "<p>We couldnt find any topic with the title “%{topic_title}”.</p>
<ul>
<li>If you have changed the title, edit that topic to modify your sites introductory text.</li>
<li>If you have deleted this topic, create another topic with “Welcome to Discourse” as the title. The content of the first post is your sites introductory text.</li>
<li>If you have deleted this topic, create another topic with “%{topic_title}” as the title. The content of the first post is your sites introductory text.</li>
</ul>"
fields:

View File

@ -22,25 +22,47 @@ class IntroductionUpdater
end
end
protected
protected
def summary_from_post(post)
return post ? post.raw.split("\n").first : nil
end
def find_welcome_post
topic_id = TopicCustomField.where(name: "is_welcome_topic").where(value: "true").pluck(:topic_id)
unless topic_id.present?
topic_id = Topic.listable_topics.where(slug: 'welcome-to-discourse').pluck(:id)
topic_id = TopicCustomField
.where(name: "is_welcome_topic", value: "true")
.pluck(:topic_id)
if topic_id.blank?
title = I18n.t("discourse_welcome_topic.title")
topic_id = find_topic_id(title)
end
if topic_id.blank?
title = I18n.t("discourse_welcome_topic.title", locale: :en)
topic_id = find_topic_id(title)
end
if topic_id.blank?
topic_id = Topic.listable_topics
.where(pinned_globally: true)
.order(:created_at)
.limit(1)
.pluck(:id)
end
welcome_topic = Topic.where(id: topic_id).first
return nil unless welcome_topic.present?
return nil if welcome_topic.blank?
post = welcome_topic.posts.where(post_number: 1).first
return nil unless post.present?
post
welcome_topic.first_post
end
def find_topic_id(topic_title)
slug = Slug.for(topic_title, nil)
return nil if slug.blank?
Topic.listable_topics
.where(slug: slug)
.pluck(:id)
end
end

View File

@ -48,6 +48,7 @@ class Wizard
if @wizard.completed_steps?('introduction') && !introduction.get_summary
step.disabled = true
step.description_vars = { topic_title: I18n.t("discourse_welcome_topic.title") }
else
step.add_field(id: 'welcome', type: 'textarea', required: true, value: introduction.get_summary)
@ -102,6 +103,7 @@ class Wizard
end
@wizard.append_step('corporate') do |step|
step.description_vars = { base_path: Discourse.base_path }
step.add_field(id: 'company_name', type: 'text', value: SiteSetting.company_name)
step.add_field(id: 'governing_law', type: 'text', value: SiteSetting.governing_law)
step.add_field(id: 'city_for_disputes', type: 'text', value: SiteSetting.city_for_disputes)
@ -263,6 +265,7 @@ class Wizard
@wizard.append_step('finished') do |step|
step.banner = "finished.png"
step.description_vars = { base_path: Discourse.base_path }
end
@wizard
end

View File

@ -1,7 +1,7 @@
class Wizard
class Step
attr_reader :id, :updater
attr_accessor :index, :fields, :next, :previous, :banner, :disabled
attr_accessor :index, :fields, :next, :previous, :banner, :disabled, :description_vars
def initialize(id)
@id = id

View File

@ -0,0 +1,56 @@
require 'rails_helper'
require 'introduction_updater'
describe IntroductionUpdater do
describe "#get_summary" do
subject { IntroductionUpdater.new(Fabricate(:admin)) }
let(:welcome_post_raw) { "lorem ipsum" }
let(:welcome_topic) do
topic = Fabricate(:topic)
Fabricate(:post, topic: topic, raw: welcome_post_raw, post_number: 1)
topic
end
it "finds the welcome topic by custom field" do
TopicCustomField.create(topic_id: welcome_topic.id, name: "is_welcome_topic", value: "true")
expect(subject.get_summary).to eq(welcome_post_raw)
end
context "without custom field" do
it "finds the welcome topic by slug using the default locale" do
I18n.locale = :de
welcome_topic.title = I18n.t("discourse_welcome_topic.title")
welcome_topic.save!
expect(subject.get_summary).to eq(welcome_post_raw)
end
it "finds the welcome topic by slug using the English locale" do
welcome_topic.title = I18n.t("discourse_welcome_topic.title", locale: :en)
welcome_topic.save!
I18n.locale = :de
expect(subject.get_summary).to eq(welcome_post_raw)
end
it "doesn't find the topic when slug_generation_method is set to 'none'" do
SiteSetting.slug_generation_method = :none
welcome_topic.title = I18n.t("discourse_welcome_topic.title")
welcome_topic.save!
expect(subject.get_summary).to be_blank
end
it "finds the oldest globally pinned topic" do
welcome_topic.update_columns(pinned_at: Time.zone.now, pinned_globally: true)
expect(subject.get_summary).to eq(welcome_post_raw)
end
it "doesn't find the topic when there is no globally pinned topic or a topic with the correct slug" do
expect(subject.get_summary).to be_blank
end
end
end
end