Skip to content

Commit

Permalink
Merge pull request AlchemyCMS#1877 from tvdeyen/preview-per-site
Browse files Browse the repository at this point in the history
Configure edit page preview per site
  • Loading branch information
tvdeyen authored Jun 11, 2020
2 parents 5cbe155 + 102d9c1 commit 1e7c025
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 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
33 changes: 27 additions & 6 deletions lib/alchemy/admin/preview_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
65 changes: 65 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,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

0 comments on commit 1e7c025

Please sign in to comment.