Skip to content

Commit 1846784

Browse files
committed
Disable slug if an url_pattern is set
This will not have an effect if the user is editing the url_pattern. Also change the mechanic of the slug little bit for url_pattern page layouts. They can have multiple wildcards it good to see the whole url pattern as slug instead of part behind the last slash.
1 parent 08e7426 commit 1846784

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 url pattern or the last part of an urlname path
4646
def slug
47-
urlname.to_s.split("/").last
47+
page_layout_url_pattern.presence || urlname.to_s.split("/").last
48+
end
49+
50+
def has_url_pattern?
51+
page_layout_url_pattern.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_url_pattern?
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 url_pattern" do
1983+
let(:page) { build(:alchemy_page, page_layout: "product_detail") }
1984+
1985+
it "returns the 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 url_pattern" do
1991+
let(:page) { build(:alchemy_page, page_layout: "blog_post") }
1992+
1993+
it "returns the full 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)