Skip to content

Commit 0e7b1b0

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. Also delegate the wildcard_url to the page delegation.
1 parent 3291598 commit 0e7b1b0

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

app/models/alchemy/page/page_naming.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module PageNaming
88

99
RESERVED_URLNAMES = %w[admin messages new]
1010

11+
delegate :wildcard_url, to: :definition
12+
1113
included do
1214
before_validation :set_urlname,
1315
if: :renamed?,
@@ -67,13 +69,14 @@ def set_urlname
6769
end
6870

6971
# Returns the full nested urlname.
70-
#
72+
# Uses the wildcard_url pattern from the page definition if present,
73+
# otherwise converts the slug or name to a url-friendly string.
7174
def nested_url_name
72-
converted_url_name = convert_to_urlname(slug.blank? ? name : slug)
75+
url_part = wildcard_url&.pattern || convert_to_urlname(slug.blank? ? name : slug)
7376
if parent&.language_root?
74-
converted_url_name
77+
url_part
7578
else
76-
[parent&.urlname, converted_url_name].compact.join("/")
79+
[parent&.urlname, url_part].compact.join("/")
7780
end
7881
end
7982
end

spec/models/alchemy/page_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,27 @@ module Alchemy
18761876
end
18771877
end
18781878

1879+
context "with a page layout that has a wildcard_url" do
1880+
let(:parent) { create(:alchemy_page, name: "Products") }
1881+
let(:pattern_page) { create(:alchemy_page, parent: parent, name: "Product Details", page_layout: "product_detail") }
1882+
1883+
it "uses the wildcard_url pattern instead of the page name" do
1884+
expect(pattern_page.urlname).to eq("products/:id")
1885+
end
1886+
1887+
it "uses the wildcard_url pattern as slug" do
1888+
expect(pattern_page.slug).to eq(":id")
1889+
end
1890+
1891+
context "with a child page under a wildcard_url page" do
1892+
let(:child) { create(:alchemy_page, parent: pattern_page, name: "Comments") }
1893+
1894+
it "includes the parent's wildcard_url pattern in the path" do
1895+
expect(child.urlname).to eq("products/:id/comments")
1896+
end
1897+
end
1898+
end
1899+
18791900
context "if new urlname exists as a legacy url" do
18801901
it "will delete obsolete legacy_urls" do
18811902
expect(page.urlname).to eq("parentparent/parent/page")

0 commit comments

Comments
 (0)