mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 16:21:18 +08:00
FEATURE: Allow to modify topic-backed static pages (#15324)
A plugin API that allows customizing existing topic-backed static pages, like: faq, tos, privacy (see: StaticController) The block passed to this method has to return a SiteSetting name that contains a topic id. ``` add_topic_static_page("faq") do |controller| current_user&.locale == "pl" ? "polish_faq_topic_id" : "faq_topic_id" end ``` You can also add new pages in a plugin, but remember to add a route, for example: ``` get "contact" => "static#show", id: "contact" ```
This commit is contained in:
@ -277,6 +277,52 @@ describe StaticController do
|
||||
expect(response.body).to include("<title>FAQ - Discourse</title>")
|
||||
end
|
||||
end
|
||||
|
||||
context "plugin api extensions" do
|
||||
after do
|
||||
Rails.application.reload_routes!
|
||||
StaticController::CUSTOM_PAGES.clear
|
||||
end
|
||||
|
||||
it "adds new topic-backed pages" do
|
||||
routes = Proc.new do
|
||||
get "contact" => "static#show", id: "contact"
|
||||
end
|
||||
Discourse::Application.routes.send(:eval_block, routes)
|
||||
|
||||
topic_id = Fabricate(:post, cooked: "contact info").topic_id
|
||||
SiteSetting.setting(:contact_topic_id, topic_id)
|
||||
|
||||
Plugin::Instance.new.add_topic_static_page("contact", topic_id: "contact_topic_id")
|
||||
|
||||
get "/contact"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include("contact info")
|
||||
end
|
||||
|
||||
it "replaces existing topic-backed pages" do
|
||||
topic_id = Fabricate(:post, cooked: "Regular FAQ").topic_id
|
||||
SiteSetting.setting(:faq_topic_id, topic_id)
|
||||
polish_topic_id = Fabricate(:post, cooked: "Polish FAQ").topic_id
|
||||
SiteSetting.setting(:polish_faq_topic_id, polish_topic_id)
|
||||
|
||||
Plugin::Instance.new.add_topic_static_page("faq") do
|
||||
current_user&.locale == "pl" ? "polish_faq_topic_id" : "faq_topic_id"
|
||||
end
|
||||
|
||||
get "/faq"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include("Regular FAQ")
|
||||
|
||||
sign_in(Fabricate(:user, locale: "pl"))
|
||||
get "/faq"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include("Polish FAQ")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#enter' do
|
||||
|
Reference in New Issue
Block a user