Skip to content

Commit

Permalink
Add support for custom mount points in Page::UrlPath (#1921)
Browse files Browse the repository at this point in the history
If an app mounts Alchemy at a non-root path we need to return that as prefix.

Closes #1918
  • Loading branch information
tvdeyen authored Aug 7, 2020
1 parent 504f864 commit 9ece5d4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/models/alchemy/page/url_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Page
# link_to page.url
#
class UrlPath
ROOT_PATH = "/"

def initialize(page)
@page = page
@language = @page.language
Expand All @@ -41,23 +39,27 @@ def call
private

def language_root_path
@language.default? ? ROOT_PATH : language_path
@language.default? ? root_path : language_path
end

def page_path_with_language_prefix
@language.default? ? page_path : language_path + page_path
end

def page_path_with_leading_slash
@page.language_root? ? ROOT_PATH : page_path
@page.language_root? ? root_path : page_path
end

def language_path
"/#{@page.language_code}"
"#{root_path}#{@page.language_code}"
end

def page_path
"/#{@page.urlname}"
"#{root_path}#{@page.urlname}"
end

def root_path
Engine.routes.url_helpers.root_path
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/models/alchemy/page/url_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@
end
end
end

context "mounted on a non-root path" do
let(:page) do
create(:alchemy_page)
end

before do
expect(Alchemy::Engine.routes).to receive(:url_helpers) do
double(root_path: "/pages/")
end
end

it "prefixes the path" do
is_expected.to eq("/pages/#{page.urlname}")
end
end
end

0 comments on commit 9ece5d4

Please sign in to comment.