-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By default Alchemy uses its internal page preview renderer, but you can configure it now to be any URL instead. Basic Auth is supported as well. Example config/alchemy/config.yml preview: host: https://www.my-static-site.com auth: username: <%= ENV["BASIC_AUTH_USERNAME"] %> password: <%= ENV["BASIC_AUTH_PASSWORD"] %> This is great for static sites.
- Loading branch information
Showing
7 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require "uri" | ||
|
||
module Alchemy | ||
module Admin | ||
# = Preview window URL configuration | ||
# | ||
# By default Alchemy uses its internal page preview renderer, | ||
# but you can configure it to be any URL instead. | ||
# | ||
# Basic Auth is supported. | ||
# | ||
# == Example config/alchemy/config.yml | ||
# | ||
# preview: | ||
# 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 | ||
uri_class.build( | ||
host: uri.host, | ||
path: "/#{page.urlname}", | ||
userinfo: userinfo, | ||
).to_s | ||
else | ||
routes.admin_page_path(page) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :preview_config, :routes | ||
|
||
def uri | ||
URI(preview_config["host"]) | ||
end | ||
|
||
def uri_class | ||
if uri.class == URI::Generic | ||
raise MissingProtocolError.new("Please provide the protocol with preview['host']") | ||
else | ||
uri.class | ||
end | ||
end | ||
|
||
def userinfo | ||
auth = preview_config["auth"] | ||
auth ? "#{auth["username"]}:#{auth["password"]}" : nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::Admin::PreviewUrl do | ||
let(:preview_url) do | ||
described_class.new(routes: Alchemy::Engine.routes) | ||
end | ||
|
||
describe "#url_for" do | ||
let(:page) { create(:alchemy_page) } | ||
|
||
subject { preview_url.url_for(page) } | ||
|
||
context "without preview configured" do | ||
it "returns the admin pages preview url" do | ||
is_expected.to eq "/admin/pages/#{page.id}" | ||
end | ||
end | ||
|
||
context "with preview configured" do | ||
context "without protocol" do | ||
before do | ||
stub_alchemy_config(:preview, { | ||
"host" => "www.example.com", | ||
}) | ||
end | ||
|
||
it "returns the configured preview url" do | ||
expect { subject }.to raise_error(Alchemy::Admin::PreviewUrl::MissingProtocolError) | ||
end | ||
end | ||
|
||
context "as http url" do | ||
before do | ||
stub_alchemy_config(:preview, { | ||
"host" => "http://www.example.com", | ||
}) | ||
end | ||
|
||
it "returns the configured preview url" do | ||
is_expected.to eq "http://www.example.com/#{page.urlname}" | ||
end | ||
end | ||
|
||
context "as https url" do | ||
before do | ||
stub_alchemy_config(:preview, { | ||
"host" => "https://www.example.com", | ||
}) | ||
end | ||
|
||
it "returns the configured preview url with https" do | ||
is_expected.to eq "https://www.example.com/#{page.urlname}" | ||
end | ||
end | ||
|
||
context "and with basic auth configured" do | ||
before do | ||
stub_alchemy_config(:preview, { | ||
"host" => "https://www.example.com", | ||
"auth" => { | ||
"username" => "foo", | ||
"password" => "baz", | ||
}, | ||
}) | ||
end | ||
|
||
it "returns the configured preview url with userinfo" do | ||
is_expected.to eq "https://foo:baz@www.example.com/#{page.urlname}" | ||
end | ||
end | ||
end | ||
end | ||
end |