Skip to content

Commit dfb07d3

Browse files
committed
Disable slug if a wildcard_url is set
It wouldn't work to update the slug anyway, because the urlname mechanic is different for pages with a wildcard_url. The slug form field will be disabled and the slug can also contain slashes.
1 parent 37b6588 commit dfb07d3

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

app/models/alchemy/page/page_naming.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ def update_urlname!
4242
end
4343
end
4444

45-
# Returns always the last part of a urlname path
45+
# Returns wildcard url pattern or the last part of an urlname path
4646
def slug
47-
urlname.to_s.split("/").last
47+
wildcard_url&.pattern.presence || urlname.to_s.split("/").last
48+
end
49+
50+
def has_wildcard_url?
51+
wildcard_url&.present?
4852
end
4953

5054
private

app/views/alchemy/admin/pages/_form.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
</div>
1919

2020
<%= f.input :name, autofocus: true %>
21-
<%= f.input :urlname, as: 'string', input_html: {value: @page.slug}, label: Alchemy::Page.human_attribute_name(:slug) %>
21+
<%= f.input :urlname, as: 'string', input_html: {
22+
value: @page.slug,
23+
disabled: @page.has_wildcard_url?
24+
}, label: Alchemy::Page.human_attribute_name(:slug) %>
2225

2326
<%= f.fields_for :draft_version, @page.draft_version do |v| %>
2427
<alchemy-char-counter max-chars="60">

spec/models/alchemy/page_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,22 @@ module Alchemy
19781978
expect(page.slug).to be_nil
19791979
end
19801980
end
1981+
1982+
context "with a page layout that has a wildcard_url" do
1983+
let(:page) { build(:alchemy_page, page_layout: "product_detail") }
1984+
1985+
it "returns the wildcard_url pattern instead of the urlname" do
1986+
expect(page.slug).to eq(":id")
1987+
end
1988+
end
1989+
1990+
context "with a multi-segment wildcard_url" do
1991+
let(:page) { build(:alchemy_page, page_layout: "blog_post") }
1992+
1993+
it "returns the full wildcard_url pattern" do
1994+
expect(page.slug).to eq(":year/:slug")
1995+
end
1996+
end
19811997
end
19821998

19831999
context "page status methods" do

0 commit comments

Comments
 (0)