Skip to content

Commit

Permalink
Allow previe url to be configured per site
Browse files Browse the repository at this point in the history
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"] %>
  • Loading branch information
tvdeyen committed Jun 11, 2020
1 parent 5cbe155 commit 5795da2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
10 changes: 10 additions & 0 deletions config/alchemy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
31 changes: 26 additions & 5 deletions lib/alchemy/admin/preview_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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
Expand All @@ -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
Expand Down
51 changes: 51 additions & 0 deletions spec/libraries/admin/preview_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5795da2

Please sign in to comment.