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..2b2325e88a 100644 --- a/lib/alchemy/admin/preview_url.rb +++ b/lib/alchemy/admin/preview_url.rb @@ -19,19 +19,31 @@ 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}", + path: page.url_path, userinfo: userinfo, ).to_s else @@ -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..8c2b9e91f7 100644 --- a/spec/libraries/admin/preview_url_spec.rb +++ b/spec/libraries/admin/preview_url_spec.rb @@ -70,6 +70,71 @@ 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 + + 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