Skip to content

Commit 7c21a94

Browse files
committed
Extend url path class to handle wildcard params
Allow the setting of wildcard parameter in url_path class. It is introducing a new wildcard_params hash to not mix that set of params with the default optional_params hash.
1 parent 97fe30c commit 7c21a94

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

app/models/alchemy/page.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ def find_elements(options = {})
305305
# = The url_path for this page
306306
#
307307
# @see Alchemy::Page::UrlPath#call
308-
def url_path(optional_params = {})
309-
self.class.url_path_class.new(self, optional_params).call
308+
def url_path(optional_params = {}, wildcard_params: {})
309+
self.class.url_path_class.new(self, optional_params, wildcard_params: wildcard_params).call
310310
end
311311

312312
# The page's view partial is dependent from its page layout

app/models/alchemy/page/url_path.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ class Page
2020
# link_to page.url
2121
#
2222
class UrlPath
23-
def initialize(page, optional_params = {})
23+
def initialize(page, optional_params = {}, wildcard_params: {})
2424
@page = page
2525
@language = @page.language
2626
@site = @language.site
2727
@optional_params = optional_params
28+
@wildcard_params = wildcard_params
2829
end
2930

3031
def call
@@ -64,7 +65,11 @@ def language_path
6465
end
6566

6667
def page_path
67-
"#{root_path}#{@page.urlname}"
68+
urlname = @page.urlname
69+
@wildcard_params.each do |key, val|
70+
urlname = urlname.gsub(":#{key}", val.to_s)
71+
end
72+
"#{root_path}#{urlname}"
6873
end
6974

7075
def root_path

spec/models/alchemy/page/url_path_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,39 @@
7676
end
7777
end
7878

79+
context "with wildcard params" do
80+
let(:parent) { create(:alchemy_page, name: "Products") }
81+
let(:page) { create(:alchemy_page, parent: parent, name: "Product Detail", page_layout: "page_with_wildcard_url") }
82+
83+
subject(:url) { described_class.new(page, wildcard_params: {slug: 42}).call }
84+
85+
it "substitutes wildcards in the urlname" do
86+
is_expected.to eq("/products/42")
87+
end
88+
end
89+
90+
context "with wildcard params as ActionController::Parameters" do
91+
let(:parent) { create(:alchemy_page, name: "Products") }
92+
let(:page) { create(:alchemy_page, parent: parent, name: "Product Detail", page_layout: "page_with_wildcard_url") }
93+
94+
subject(:url) { described_class.new(page, wildcard_params: ActionController::Parameters.new({slug: 42})).call }
95+
96+
it "substitutes wildcards in the urlname" do
97+
is_expected.to eq("/products/42")
98+
end
99+
end
100+
101+
context "with wildcard and query params mixed" do
102+
let(:parent) { create(:alchemy_page, name: "Products") }
103+
let(:page) { create(:alchemy_page, parent: parent, name: "Product Detail", page_layout: "page_with_wildcard_url") }
104+
105+
subject(:url) { described_class.new(page, {page: 2}, wildcard_params: {slug: 42}).call }
106+
107+
it "substitutes wildcards and appends remaining params as query string" do
108+
is_expected.to eq("/products/42?page=2")
109+
end
110+
end
111+
79112
context "mounted on a non-root path" do
80113
let(:page) do
81114
create(:alchemy_page)

spec/models/alchemy/page_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ module Alchemy
344344
end
345345

346346
context "with a custom url_path_class" do
347-
let(:url_path_class) { Struct.new(:page, :params) { def call = "/bar" } }
347+
let(:url_path_class) { Struct.new(:page, :params, :wildcard_params) { def call = "/bar" } }
348348

349349
before do
350350
described_class.url_path_class = url_path_class
@@ -356,6 +356,14 @@ module Alchemy
356356

357357
it { is_expected.to eq("/bar") }
358358
end
359+
360+
context "with wildcard params" do
361+
let(:page) { create(:alchemy_page, page_layout: "page_with_wildcard_url") }
362+
363+
subject { page.url_path(wildcard_params: {slug: 42}) }
364+
365+
it { is_expected.to eq("/42") }
366+
end
359367
end
360368

361369
describe ".all_from_clipboard_for_select" do

0 commit comments

Comments
 (0)