-
-
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.
Introduce page.url_path and use it for alchemyPageSelect (#1859)
* Introduce page url_path service class Use this class to generate the url_path to a page. It takes several circumstances into account 1. It returns just a slash for language root pages of the default langauge 2. It returns a url path with a leading slash for regular pages 3. It returns a url path with a leading slash and language code prefix for pages not having the default language 4. It returns a url path with a leading slash and the language code for language root pages of a non-default language * Return the page url_path with API responses We want to use the newly introduced url_path attribute on the page selector in the admin. It in general makes sense to have the correct url to a page from the API anyway. * Remove @url_prefix This variable is not used anymore. since we generate the url in page links via the API response. * Use Page#url_path in alchemyPageSelect We do not need to care about adding leading slashes in the template anymore and it fixes a lot of missing features for multi language pages. * Use page.url_path in node.url Instead of having (buggy) page url generating code in the node model, we use the newly introduced page.url_path attribute that takes all necessities into account. * Appease Rubocop * Use page.url_path in page link dialog And with that fix bugs for wrongly generated urls in multi language environments.
- Loading branch information
Showing
11 changed files
with
169 additions
and
17 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 |
---|---|---|
|
@@ -105,6 +105,7 @@ def page_includes | |
[ | ||
:tags, | ||
{ | ||
language: :site, | ||
elements: [ | ||
{ | ||
nested_elements: [ | ||
|
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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class Page | ||
# = The url_path for this page | ||
# | ||
# Use this to build relative links to this page | ||
# | ||
# It takes several circumstances into account: | ||
# | ||
# 1. It returns just a slash for language root pages of the default langauge | ||
# 2. It returns a url path with a leading slash for regular pages | ||
# 3. It returns a url path with a leading slash and language code prefix for pages not having the default language | ||
# 4. It returns a url path with a leading slash and the language code for language root pages of a non-default language | ||
# | ||
# == Examples | ||
# | ||
# Using Rails' link_to helper | ||
# | ||
# link_to page.url | ||
# | ||
class UrlPath | ||
ROOT_PATH = "/" | ||
|
||
def initialize(page) | ||
@page = page | ||
@language = @page.language | ||
@site = @language.site | ||
end | ||
|
||
def call | ||
return @page.urlname if @page.definition["redirects_to_external"] | ||
|
||
if @page.language_root? | ||
language_root_path | ||
elsif @site.languages.select(&:public?).length > 1 | ||
page_path_with_language_prefix | ||
else | ||
page_path_with_leading_slash | ||
end | ||
end | ||
|
||
private | ||
|
||
def language_root_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 | ||
end | ||
|
||
def language_path | ||
"/#{@page.language_code}" | ||
end | ||
|
||
def page_path | ||
"/#{@page.urlname}" | ||
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
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,76 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::Page::UrlPath do | ||
subject(:url) { described_class.new(page).call } | ||
|
||
let(:site) { create(:alchemy_site) } | ||
|
||
context "if the page redirects to external" do | ||
let(:page) do | ||
create(:alchemy_page, page_layout: "external", site: site, urlname: "https://example.com") | ||
end | ||
|
||
it { is_expected.to eq("https://example.com") } | ||
end | ||
|
||
context "on a single language site" do | ||
context "for the language root page" do | ||
let(:page) { create(:alchemy_page, :language_root, site: site) } | ||
|
||
it { is_expected.to eq("/") } | ||
end | ||
|
||
context "for a regular page" do | ||
let(:page) { create(:alchemy_page, site: site) } | ||
|
||
it { is_expected.to eq("/#{page.urlname}") } | ||
end | ||
end | ||
|
||
context "on a multi language site" do | ||
let!(:default_language) { site.default_language } | ||
let!(:language) { create(:alchemy_language, :klingon, site: site) } | ||
|
||
context "for the language root page" do | ||
context "and page having the default language" do | ||
let(:page) do | ||
create(:alchemy_page, :language_root, site: site, language: default_language) | ||
end | ||
|
||
it { is_expected.to eq("/") } | ||
end | ||
|
||
context "and page having a non-default language" do | ||
let(:page) do | ||
create(:alchemy_page, :language_root, site: site, language: language) | ||
end | ||
|
||
it do | ||
is_expected.to eq("/#{page.language_code}") | ||
end | ||
end | ||
end | ||
|
||
context "for a regular page" do | ||
context "and page having the default language" do | ||
let(:page) do | ||
create(:alchemy_page, site: site, language: default_language) | ||
end | ||
|
||
it { is_expected.to eq("/#{page.urlname}") } | ||
end | ||
|
||
context "and page having a non-default language" do | ||
let(:page) do | ||
create(:alchemy_page, site: site, language: language) | ||
end | ||
|
||
it do | ||
is_expected.to eq("/#{page.language_code}/#{page.urlname}") | ||
end | ||
end | ||
end | ||
end | ||
end |