Skip to content

Commit c0a7f60

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 45f78f6 commit c0a7f60

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
@@ -1994,6 +1994,22 @@ module Alchemy
19941994
expect(page.slug).to be_nil
19951995
end
19961996
end
1997+
1998+
context "with a page layout that has a wildcard_url" do
1999+
let(:page) { build(:alchemy_page, page_layout: "product_detail") }
2000+
2001+
it "returns the wildcard_url pattern instead of the urlname" do
2002+
expect(page.slug).to eq(":id")
2003+
end
2004+
end
2005+
2006+
context "with a multi-segment wildcard_url" do
2007+
let(:page) { build(:alchemy_page, page_layout: "blog_post") }
2008+
2009+
it "returns the full wildcard_url pattern" do
2010+
expect(page.slug).to eq(":year/:slug")
2011+
end
2012+
end
19972013
end
19982014

19992015
context "page status methods" do

0 commit comments

Comments
 (0)