A platform for community discussion. Free, open, simple.
DEV: Move blog's prepended routes out of plugin.rb (#45463)
David Battersby56480f344162
diff --git a/plugins/blog/lib/blog/routes.rb b/plugins/blog/lib/blog/routes.rbnew file mode 100644index 00000000000..3e9c31ed914--- /dev/null+++ b/plugins/blog/lib/blog/routes.rb@@ -0,0 +1,90 @@+# frozen_string_literal: true++module Blog+ # Prepended from plugin.rb so these routes are matched before core's routes. Rails+ # draws prepended blocks before loading any routes file, so they can't live in+ # config/routes.rb.+ module Routes+ ON_BLOG_HOST = ->(request) do+ Configuration.blog_host?(request) && !Configuration.asset_path?(request.path)+ end+ ON_PATH_PREFIX = ->(_) do+ UpcomingChanges.enabled?(:blog_enabled) && !Configuration.custom_domain?+ end+ MOVED_TO_CUSTOM_DOMAIN = ->(_) do+ UpcomingChanges.enabled?(:blog_enabled) && Configuration.custom_domain?+ end++ ARTICLES =+ proc do+ get "/" => "articles#index"+ get "/archive" => "articles#index", :defaults => { archive: true }+ get "/tag/:tag" => "articles#index"+ get "/about" => "articles#about"+ get "/blog-theme.css" => "articles#theme_css", :format => false+ get "/blog-highlight/:version.js" => "articles#highlight_js", :format => false+ get "/blog-custom.js" => "articles#theme_js", :format => false+ get "/feed.xml" => "articles#feed", :defaults => { format: "rss" }, :format => false+ get "/sitemap" => "articles#sitemap", :format => true, :constraints => { format: :xml }+ get "/*path" => "articles#show", :format => false+ end++ DRAW =+ proc do+ scope module: :blog do+ constraints(ON_BLOG_HOST) do+ get "/session/csrf" => "review_reader#csrf"+ get "/blog/review" => "review_reader#show"+ post "/blog/review/feedback" => "review_reader#feedback"+ get "/review" =>+ redirect { |_, request| request.fullpath.sub("/review", "/blog/review") },+ :format => false+ get "/posts" => redirect { "#{Configuration.base_url}/" }, :format => false+ %w[/posts.rss /posts.atom].each do |path|+ get path => redirect { "#{Configuration.base_url}/feed.xml" }, :format => false+ end+ get "/robots.txt" => "articles#robots",+ :defaults => {+ format: "text",+ },+ :format => false+ instance_exec(&ARTICLES)+ end++ scope Configuration::PATH_PREFIX do+ constraints(ON_PATH_PREFIX) do+ get "/review" => "review_reader#show"+ post "/review/feedback" => "review_reader#feedback"+ instance_exec(&ARTICLES)+ end++ # Temporary, since a site can drop its custom domain and browsers cache 301s.+ constraints(MOVED_TO_CUSTOM_DOMAIN) do+ get "/(*path)" =>+ redirect(status: 302) { |_, request| Routes.custom_domain_url(request) },+ :format => false+ end+ end+ end++ # The themes list is served by Blog::ThemesController, which renders the app for page loads.+ %w[appearance identity themes/new themes/import/new themes/:id/edit].each do |path|+ get "/admin/plugins/blog/#{path}" => "admin/plugins#show",+ :defaults => {+ plugin_id: "blog",+ },+ :constraints => AdminConstraint.new+ end++ mount Engine, at: "/"+ end++ private_constant :ON_BLOG_HOST, :ON_PATH_PREFIX, :MOVED_TO_CUSTOM_DOMAIN, :ARTICLES++ def self.custom_domain_url(request)+ path = request.path_info.delete_prefix(Configuration::PATH_PREFIX).presence || "/"+ path += "?#{request.query_string}" if request.query_string.present?+ "#{Configuration.base_url}#{path}"+ end+ end+enddiff --git a/plugins/blog/plugin.rb b/plugins/blog/plugin.rbindex 328a0308b5e..fef99d5268d 100644--- a/plugins/blog/plugin.rb+++ b/plugins/blog/plugin.rb@@ -114,71 +114,7 @@ after_initialize do@blog_publicationend- blog_routes =- proc do- get "/" => "blog/articles#index"- get "/archive" => "blog/articles#index", :defaults => { archive: true }- get "/tag/:tag" => "blog/articles#index"- get "/about" => "blog/articles#about"- get "/blog-theme.css" => "blog/articles#theme_css", :format => false- get "/blog-highlight/:version.js" => "blog/articles#highlight_js", :format => false- get "/blog-custom.js" => "blog/articles#theme_js", :format => false- get "/feed.xml" => "blog/articles#feed", :defaults => { format: "rss" }, :format => false- get "/sitemap" => "blog/articles#sitemap", :format => true, :constraints => { format: :xml }- end-- Discourse::Application.routes.prepend do- constraints(- ->(request) do- Blog::Configuration.blog_host?(request) && !Blog::Configuration.asset_path?(request.path)- end,- ) do- get "/session/csrf" => "blog/review_reader#csrf"- get "/blog/review" => "blog/review_reader#show"- post "/blog/review/feedback" => "blog/review_reader#feedback"- get "/review" => redirect { |_, request| request.fullpath.sub("/review", "/blog/review") },- :format => false- get "/posts" => redirect { "#{Blog::Configuration.base_url}/" }, :format => false- %w[/posts.rss /posts.atom].each do |path|- get path => redirect { "#{Blog::Configuration.base_url}/feed.xml" }, :format => false- end- instance_exec(&blog_routes)- get "/robots.txt" => "blog/articles#robots", :defaults => { format: "text" }, :format => false- get "/*path" => "blog/articles#show", :format => false- end- scope Blog::Configuration::PATH_PREFIX do- constraints(- ->(_) { UpcomingChanges.enabled?(:blog_enabled) && !Blog::Configuration.custom_domain? },- ) do- get "/review" => "blog/review_reader#show"- post "/review/feedback" => "blog/review_reader#feedback"- instance_exec(&blog_routes)- get "/*path" => "blog/articles#show", :format => false- end- constraints(- ->(_) { UpcomingChanges.enabled?(:blog_enabled) && Blog::Configuration.custom_domain? },- ) do- # Temporary, since a site can drop its custom domain and browsers cache 301s.- get "/(*path)" =>- redirect(status: 302) { |_, request|- path =- request.path_info.delete_prefix(Blog::Configuration::PATH_PREFIX).presence || "/"- path += "?#{request.query_string}" if request.query_string.present?- "#{Blog::Configuration.base_url}#{path}"- },- :format => false- end- end- # The themes list is served by Blog::ThemesController, which renders the app for page loads.- %w[appearance identity themes/new themes/import/new themes/:id/edit].each do |path|- get "/admin/plugins/blog/#{path}" => "admin/plugins#show",- :defaults => {- plugin_id: "blog",- },- :constraints => AdminConstraint.new- end- mount ::Blog::Engine, at: "/"- end+ Discourse::Application.routes.prepend(&Blog::Routes::DRAW)reloadable_patch { ::TopicView.prepend(Blog::TopicViewExtension) }end