From 5795da26f98cf231cd5f07c1ac588c3163deced6 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 11 Jun 2020 14:24:25 +0200 Subject: [PATCH 1/2] Allow previe url to be configured per site You can now configure the preview url per site. preview: My site name: host: https://www.my-static-site.com auth: username: <%= ENV["BASIC_AUTH_USERNAME"] %> password: <%= ENV["BASIC_AUTH_PASSWORD"] %> --- config/alchemy/config.yml | 10 +++++ lib/alchemy/admin/preview_url.rb | 31 +++++++++++--- spec/libraries/admin/preview_url_spec.rb | 51 ++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/config/alchemy/config.yml b/config/alchemy/config.yml index fa6188e04e..5c063790bf 100644 --- a/config/alchemy/config.yml +++ b/config/alchemy/config.yml @@ -55,6 +55,16 @@ items_per_page: 15 # auth: # username: <%= ENV["BASIC_AUTH_USERNAME"] %> # password: <%= ENV["BASIC_AUTH_PASSWORD"] %> +# +# Preview config per site is supported as well. +# +# preview: +# My site name: +# host: https://www.my-static-site.com +# auth: +# username: <%= ENV["BASIC_AUTH_USERNAME"] %> +# password: <%= ENV["BASIC_AUTH_PASSWORD"] %> +# # === Picture rendering settings # diff --git a/lib/alchemy/admin/preview_url.rb b/lib/alchemy/admin/preview_url.rb index a599e56beb..3c8ddece66 100644 --- a/lib/alchemy/admin/preview_url.rb +++ b/lib/alchemy/admin/preview_url.rb @@ -19,16 +19,28 @@ module Admin # username: <%= ENV["BASIC_AUTH_USERNAME"] %> # password: <%= ENV["BASIC_AUTH_PASSWORD"] %> # + # Preview config per site is supported as well. + # + # == Example config/alchemy/config.yml + # + # preview: + # My site name: + # host: https://www.my-static-site.com + # auth: + # username: <%= ENV["BASIC_AUTH_USERNAME"] %> + # password: <%= ENV["BASIC_AUTH_PASSWORD"] %> + # class PreviewUrl class MissingProtocolError < StandardError; end def initialize(routes:) @routes = routes.url_helpers - @preview_config = Alchemy::Config.get(:preview) end def url_for(page) - if preview_config + @preview_config = preview_config_for(page) + + if @preview_config && uri uri_class.build( host: uri.host, path: "/#{page.urlname}", @@ -41,10 +53,19 @@ def url_for(page) private - attr_reader :preview_config, :routes + attr_reader :routes + + def preview_config_for(page) + preview_config = Alchemy::Config.get(:preview) + return unless preview_config + + preview_config[page.site.name] || preview_config + end def uri - URI(preview_config["host"]) + return unless @preview_config["host"] + + URI(@preview_config["host"]) end def uri_class @@ -56,7 +77,7 @@ def uri_class end def userinfo - auth = preview_config["auth"] + auth = @preview_config["auth"] auth ? "#{auth["username"]}:#{auth["password"]}" : nil end end diff --git a/spec/libraries/admin/preview_url_spec.rb b/spec/libraries/admin/preview_url_spec.rb index c2bdf9de5c..2827fec62e 100644 --- a/spec/libraries/admin/preview_url_spec.rb +++ b/spec/libraries/admin/preview_url_spec.rb @@ -70,6 +70,57 @@ is_expected.to eq "https://foo:baz@www.example.com/#{page.urlname}" end end + + context "for a site" do + before do + stub_alchemy_config(:preview, config) + end + + context "that matches the pages site name" do + let(:config) do + { + page.site.name => { + "host" => "http://new.example.com", + }, + } + end + + it "returns the configured preview url for that site" do + is_expected.to eq "http://new.example.com/#{page.urlname}" + end + end + + context "that does not match the pages site name" do + context "with a default configured" do + let(:config) do + { + "Not matching site name" => { + "host" => "http://new.example.com", + }, + "host" => "http://www.example.com", + } + end + + it "returns the default configured preview url" do + is_expected.to eq "http://www.example.com/#{page.urlname}" + end + end + + context "without a default configured" do + let(:config) do + { + "Not matching site name" => { + "host" => "http://new.example.com", + }, + } + end + + it "returns the internal preview url" do + is_expected.to eq "/admin/pages/#{page.id}" + end + end + end + end end end end From 102d9c12c36d316b4a326206b7dfdc988097a9b9 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 11 Jun 2020 14:26:26 +0200 Subject: [PATCH 2/2] Use pages url path in page preview for external sites If the page preview renders an external site instead of the internal one we pass the url-path instead of the urlname so we make sure that root page urls are ` /` and the language code is prefixed. --- lib/alchemy/admin/preview_url.rb | 2 +- spec/libraries/admin/preview_url_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/alchemy/admin/preview_url.rb b/lib/alchemy/admin/preview_url.rb index 3c8ddece66..2b2325e88a 100644 --- a/lib/alchemy/admin/preview_url.rb +++ b/lib/alchemy/admin/preview_url.rb @@ -43,7 +43,7 @@ def url_for(page) if @preview_config && uri uri_class.build( host: uri.host, - path: "/#{page.urlname}", + path: page.url_path, userinfo: userinfo, ).to_s else diff --git a/spec/libraries/admin/preview_url_spec.rb b/spec/libraries/admin/preview_url_spec.rb index 2827fec62e..8c2b9e91f7 100644 --- a/spec/libraries/admin/preview_url_spec.rb +++ b/spec/libraries/admin/preview_url_spec.rb @@ -121,6 +121,20 @@ end end end + + context "with page being the language root page" do + let(:page) { create(:alchemy_page, :language_root) } + + before do + stub_alchemy_config(:preview, { + "host" => "https://www.example.com", + }) + end + + it "returns the preview url without urlname" do + is_expected.to eq "https://www.example.com/" + end + end end end end