Skip to content

Commit 08e7426

Browse files
committed
Use url_pattern as page slug
Instead of the name the url_pattern will be used during the creation of the page. This looks better and is indicating the special status of that page.
1 parent 0d3a10c commit 08e7426

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

app/models/alchemy/page/page_naming.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,20 @@ def set_urlname
6767
end
6868

6969
# Returns the full nested urlname.
70-
#
70+
# Uses the url_pattern from the page definition if present,
71+
# otherwise converts the slug or name to a url-friendly string.
7172
def nested_url_name
72-
converted_url_name = convert_to_urlname(slug.blank? ? name : slug)
73+
url_part = page_layout_url_pattern || convert_to_urlname(slug.blank? ? name : slug)
7374
if parent&.language_root?
74-
converted_url_name
75+
url_part
7576
else
76-
[parent&.urlname, converted_url_name].compact.join("/")
77+
[parent&.urlname, url_part].compact.join("/")
7778
end
7879
end
80+
81+
def page_layout_url_pattern
82+
@_page_layout_url_pattern ||= PageDefinition.get(page_layout)&.url_pattern
83+
end
7984
end
8085
end
8186
end

spec/models/alchemy/page_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,27 @@ module Alchemy
18601860
end
18611861
end
18621862

1863+
context "with a page layout that has a url_pattern" do
1864+
let(:parent) { create(:alchemy_page, name: "Products") }
1865+
let(:pattern_page) { create(:alchemy_page, parent: parent, name: "Product Details", page_layout: "product_detail") }
1866+
1867+
it "uses the url_pattern instead of the page name" do
1868+
expect(pattern_page.urlname).to eq("products/:id")
1869+
end
1870+
1871+
it "uses the url_pattern as slug" do
1872+
expect(pattern_page.slug).to eq(":id")
1873+
end
1874+
1875+
context "with a child page under a pattern page" do
1876+
let(:child) { create(:alchemy_page, parent: pattern_page, name: "Comments") }
1877+
1878+
it "includes the parent's url_pattern in the path" do
1879+
expect(child.urlname).to eq("products/:id/comments")
1880+
end
1881+
end
1882+
end
1883+
18631884
context "if new urlname exists as a legacy url" do
18641885
it "will delete obsolete legacy_urls" do
18651886
expect(page.urlname).to eq("parentparent/parent/page")

spec/services/alchemy/url_pattern_matcher_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,6 @@ def create_page(name:, layout: "standard", parent: language_root)
105105
end
106106
end
107107

108-
context "with two unconstrained sibling pages of the same layout" do
109-
let!(:second_product_by_slug_page) { create_page(name: "Second Product By Slug", layout: "product_by_slug", parent: products_page) }
110-
111-
it "matches the first sibling returned by the database" do
112-
matcher = described_class.new("products/widget")
113-
expect(matcher.page).to eq(product_by_slug_page)
114-
end
115-
end
116-
117108
context "with competing sibling patterns of different segment counts" do
118109
let!(:shared_page) { create_page(name: "Shared") }
119110
let!(:multi_segment_page) { create_page(name: "Multi Segment", layout: "blog_post", parent: shared_page) }

0 commit comments

Comments
 (0)