Skip to content

Commit 6249188

Browse files
author
Marc Antwertinger
committed
Fix sitemap generating correct root urls for default and additional languages
1 parent a4f801a commit 6249188

File tree

8 files changed

+45
-94
lines changed

8 files changed

+45
-94
lines changed

app/helpers/alchemy/url_helper.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,12 @@ module Alchemy
88
module UrlHelper
99
# Returns the path for rendering an alchemy page
1010
def show_alchemy_page_path(page, optional_params = {})
11-
alchemy.show_page_path(show_page_path_params(page, optional_params))
11+
page.url_path(optional_params)
1212
end
1313

1414
# Returns the url for rendering an alchemy page
1515
def show_alchemy_page_url(page, optional_params = {})
16-
alchemy.show_page_url(show_page_path_params(page, optional_params))
17-
end
18-
19-
# Returns the correct params-hash for passing to show_page_path
20-
def show_page_path_params(page, optional_params = {})
21-
raise ArgumentError, "Page is nil" if page.nil?
22-
23-
url_params = {urlname: page.urlname}.update(optional_params)
24-
prefix_locale?(page.language_code) ? url_params.update(locale: page.language_code) : url_params
16+
url_for(page.url_path(optional_params))
2517
end
2618

2719
# Returns the path for downloading an alchemy attachment

app/models/alchemy/page.rb

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

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

app/models/alchemy/page/url_path.rb

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

2930
def call
30-
if @page.language_root?
31+
path = if @page.language_root?
3132
language_root_path
3233
elsif @site.languages.count(&:public?) > 1
3334
page_path_with_language_prefix
3435
else
3536
page_path_with_leading_slash
3637
end
38+
39+
if @optional_params.present?
40+
uri = URI(path)
41+
uri.query = @optional_params.to_query
42+
uri.to_s
43+
else
44+
path
45+
end
3746
end
3847

3948
private

app/views/alchemy/language_links/_language.html.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<%= link_to(
22
content_tag(:span, language.label(options[:linkname])).html_safe,
33
show_alchemy_page_path(
4-
language.pages.language_roots.first,
5-
locale: prefix_locale?(language.code) ? language.code : nil
4+
language.pages.language_roots.first
65
),
76
class: [
87
language.code,

spec/helpers/alchemy/pages_helper_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ module Alchemy
239239

240240
it "should render language links referring to their language root page" do
241241
code = klingon_language_root.language_code
242-
urlname = klingon_language_root.urlname
243-
expect(helper.language_links).to have_selector("a.#{code}[href='/#{code}/#{urlname}']")
242+
expect(helper.language_links).to have_selector("a.#{code}[href='/#{code}']")
244243
end
245244

246245
context "with options[:linkname]" do

spec/helpers/alchemy/url_helper_spec.rb

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,11 @@ module Alchemy
99
let(:page) { mock_model(Page, urlname: "testpage", language_code: "en") }
1010

1111
context "page path helpers" do
12-
describe "#show_page_path_params" do
13-
subject(:show_page_path_params) { helper.show_page_path_params(page) }
14-
15-
context "if prefix_locale? is false" do
16-
before do
17-
expect(helper).to receive(:prefix_locale?).with(page.language_code) { false }
18-
end
19-
20-
it "returns a Hash with urlname and no locale parameter" do
21-
expect(show_page_path_params).to include(urlname: "testpage")
22-
expect(show_page_path_params).to_not include(locale: "en")
23-
end
24-
25-
context "with additional parameters" do
26-
subject(:show_page_path_params) do
27-
helper.show_page_path_params(page, {query: "test"})
28-
end
29-
30-
it "returns a Hash with urlname, no locale and query parameter" do
31-
expect(show_page_path_params).to \
32-
include(urlname: "testpage", query: "test")
33-
expect(show_page_path_params).to_not \
34-
include(locale: "en")
35-
end
36-
end
37-
end
38-
39-
context "if prefix_locale? is false" do
40-
before do
41-
expect(helper).to receive(:prefix_locale?).with(page.language_code) { true }
42-
end
43-
44-
it "returns a Hash with urlname and locale parameter" do
45-
expect(show_page_path_params).to \
46-
include(urlname: "testpage", locale: "en")
47-
end
48-
49-
context "with additional parameters" do
50-
subject(:show_page_path_params) do
51-
helper.show_page_path_params(page, {query: "test"})
52-
end
53-
54-
it "returns a Hash with urlname, locale and query parameter" do
55-
expect(show_page_path_params).to \
56-
include(urlname: "testpage", locale: "en", query: "test")
57-
end
58-
end
59-
end
60-
end
61-
6212
describe "#show_alchemy_page_path" do
6313
context "when prefix_locale? set to true" do
6414
before do
65-
expect(helper).to receive(:prefix_locale?).with(page.language_code) { true }
15+
allow(page).to receive(:url_path).with({}).and_return("/#{page.language_code}/testpage")
16+
allow(page).to receive(:url_path).with({query: "test"}).and_return("/#{page.language_code}/testpage?query=test")
6617
end
6718

6819
it "should return the correct relative path string" do
@@ -77,7 +28,8 @@ module Alchemy
7728

7829
context "when prefix_locale? set to false" do
7930
before do
80-
expect(helper).to receive(:prefix_locale?).with(page.language_code) { false }
31+
allow(page).to receive(:url_path).with({}).and_return("/testpage")
32+
allow(page).to receive(:url_path).with({query: "test"}).and_return("/testpage?query=test")
8133
end
8234

8335
it "should return the correct relative path string" do
@@ -94,33 +46,35 @@ module Alchemy
9446
describe "#show_alchemy_page_url" do
9547
context "when prefix_locale? set to true" do
9648
before do
97-
expect(helper).to receive(:prefix_locale?).with(page.language_code) { true }
49+
allow(page).to receive(:url_path).with({}).and_return("/#{page.language_code}/testpage")
50+
allow(page).to receive(:url_path).with({query: "test"}).and_return("/#{page.language_code}/testpage?query=test")
9851
end
9952

10053
it "should return the correct url string" do
10154
expect(helper.show_alchemy_page_url(page)).to \
102-
eq("http://#{helper.request.host}/#{page.language_code}/testpage")
55+
eq("/#{page.language_code}/testpage")
10356
end
10457

10558
it "should return the correct url string with additional parameters" do
10659
expect(helper.show_alchemy_page_url(page, {query: "test"})).to \
107-
eq("http://#{helper.request.host}/#{page.language_code}/testpage?query=test")
60+
eq("/#{page.language_code}/testpage?query=test")
10861
end
10962
end
11063

11164
context "when prefix_locale? set to false" do
11265
before do
113-
expect(helper).to receive(:prefix_locale?).with(page.language_code) { false }
66+
allow(page).to receive(:url_path).with({}).and_return("/testpage")
67+
allow(page).to receive(:url_path).with({query: "test"}).and_return("/testpage?query=test")
11468
end
11569

11670
it "should return the correct url string" do
11771
expect(helper.show_alchemy_page_url(page)).to \
118-
eq("http://#{helper.request.host}/testpage")
72+
eq("/testpage")
11973
end
12074

12175
it "should return the correct url string with additional parameter" do
12276
expect(helper.show_alchemy_page_url(page, {query: "test"})).to \
123-
eq("http://#{helper.request.host}/testpage?query=test")
77+
eq("/testpage?query=test")
12478
end
12579
end
12680
end

spec/models/alchemy/page/url_path_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@
6666
end
6767
end
6868

69+
context "with optional params" do
70+
subject(:url) { described_class.new(page, {foo: "bar"}).call }
71+
72+
let(:page) { create(:alchemy_page) }
73+
74+
it "appends params as query string" do
75+
is_expected.to eq("/#{page.urlname}?foo=bar")
76+
end
77+
end
78+
6979
context "mounted on a non-root path" do
7080
let(:page) do
7181
create(:alchemy_page)

spec/requests/alchemy/sitemap_spec.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,22 @@
2424
end
2525

2626
context "in multi language mode" do
27-
let!(:root) { page.parent }
28-
let!(:pages) { [root, page] }
29-
30-
before do
31-
allow_any_instance_of(Alchemy::BaseController).to receive("prefix_locale?") { true }
32-
end
27+
let!(:klingon) { create(:alchemy_language, :klingon) }
28+
let!(:klingon_page) { create(:alchemy_page, :public, sitemap: true, language: klingon) }
3329

3430
it "links in sitemap has locale code included" do
3531
get "/sitemap.xml"
3632
xml_doc = Nokogiri::XML(response.body)
37-
xml_doc.css("urlset url").each_with_index do |node, i|
38-
page = pages[i]
39-
expect(node.css("loc").text).to match(/\/#{page.language_code}\//)
40-
end
33+
klingon_loc = xml_doc.css("urlset url loc").map(&:text).find { |loc| loc.include?(klingon_page.urlname) }
34+
expect(klingon_loc).to match(/\/#{klingon.language_code}\//)
4135
end
4236

4337
context "if the default locale is the page locale" do
44-
before do
45-
allow_any_instance_of(Alchemy::BaseController).to receive("prefix_locale?") { false }
46-
end
47-
4838
it "links in sitemap has no locale code included" do
4939
get "/sitemap.xml"
5040
xml_doc = Nokogiri::XML(response.body)
51-
xml_doc.css("urlset url").each_with_index do |node, i|
52-
page = pages[i]
53-
expect(node.css("loc").text).to_not match(/\/#{page.language_code}\//)
54-
end
41+
default_loc = xml_doc.css("urlset url loc").map(&:text).find { |loc| loc.include?(page.urlname) }
42+
expect(default_loc).to_not match(/\/#{page.language_code}\//)
5543
end
5644
end
5745
end

0 commit comments

Comments
 (0)