Skip to content

Commit 37b6588

Browse files
committed
Use wildcard_url for url naming
Prevent the creation of multiple pages with the same wildcard_url under the same parent page. Alchemy will validate the slug and will prevent creating the same slug twice.
1 parent ceed8a5 commit 37b6588

2 files changed

Lines changed: 30 additions & 4 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 wildcard_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 = wildcard_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 wildcard_url
82+
@_wildcard_url ||= PageDefinition.get(page_layout)&.wildcard_url
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 wildcard_url" 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 wildcard_url pattern instead of the page name" do
1868+
expect(pattern_page.urlname).to eq("products/:id")
1869+
end
1870+
1871+
it "uses the wildcard_url pattern as slug" do
1872+
expect(pattern_page.slug).to eq(":id")
1873+
end
1874+
1875+
context "with a child page under a wildcard_url page" do
1876+
let(:child) { create(:alchemy_page, parent: pattern_page, name: "Comments") }
1877+
1878+
it "includes the parent's wildcard_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")

0 commit comments

Comments
 (0)