Skip to content

Commit a337dab

Browse files
authored
Merge pull request #2892 from AlchemyCMS/fix-internal-link-selection
Fix link selection of internal root or locale urls
2 parents a8c2c23 + 1fe1918 commit a337dab

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

app/components/alchemy/admin/link_dialog/internal_tab.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Alchemy
44
module Admin
55
module LinkDialog
66
class InternalTab < BaseTab
7+
PAGE_URL_PATTERN = /\/(?<locale>[a-z]{2})?(?<slash>\/)?(?<urlname>.*)/
8+
79
def title
810
Alchemy.t("link_overlay_tab_label.internal")
911
end
@@ -39,12 +41,28 @@ def uri
3941
end
4042

4143
def page
42-
@_page ||= uri ? Alchemy::Page.find_by(urlname: uri.path[1..]) : nil
44+
@_page ||= if uri&.path == "/"
45+
Alchemy::Current.site.default_language.root_page
46+
elsif uri
47+
Alchemy::Page.find_by(page_attributes)
48+
end
49+
end
50+
51+
def page_attributes
52+
locale, _slash, urlname = uri.path.match(PAGE_URL_PATTERN)&.captures
53+
54+
if locale && urlname.present?
55+
{language_code: locale, urlname: urlname}
56+
elsif locale
57+
{language_code: locale, language_root: true}
58+
else
59+
{urlname: urlname}
60+
end
4361
end
4462

4563
def page_select
4664
label = label_tag("internal_link", Alchemy.t(:page), class: "control-label")
47-
input = text_field_tag("internal_link", is_selected? ? uri : "", id: "internal_link")
65+
input = text_field_tag("internal_link", page && uri, id: "internal_link")
4866
page_select = render Alchemy::Admin::PageSelect.new(page, allow_clear: true).with_content(input)
4967
content_tag("div", label + page_select, class: "input select")
5068
end
@@ -53,7 +71,7 @@ def dom_id_select
5371
fragment = "##{uri.fragment}" if uri&.fragment
5472
label = label_tag("element_anchor", Alchemy.t(:anchor), class: "control-label")
5573
options = [[page.nil? ? Alchemy.t("Select a page first") : Alchemy.t("None"), ""]]
56-
options += [[fragment, fragment]] if is_selected? && fragment
74+
options += [[fragment, fragment]] if page && fragment
5775

5876
select = select_tag("element_anchor", options_for_select(options, fragment), is: "alchemy-select", disabled: page.nil?)
5977
select_component = content_tag("alchemy-dom-id-api-select", select, {page: page&.id})

spec/components/alchemy/admin/link_dialog/internal_tab_spec.rb

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "rails_helper"
44

55
RSpec.describe Alchemy::Admin::LinkDialog::InternalTab, type: :component do
6+
let(:site) { create(:alchemy_site) }
67
let(:alchemy_page) { create(:alchemy_page) }
78
let(:url) { alchemy_page.url_path + "#" + fragment }
89
let(:fragment) { "bar" }
@@ -12,33 +13,62 @@
1213
let(:link_target) { nil }
1314

1415
before do
16+
Alchemy::Current.site = site
1517
render_inline(described_class.new(url, is_selected: is_selected, link_title: link_title, link_target: link_target))
1618
end
1719

1820
it_behaves_like "a link dialog tab", :internal, "Internal"
1921
it_behaves_like "a link dialog - target select", :internal
2022

21-
context "link field" do
22-
context "tab not selected" do
23-
it "should not have the value of the url" do
24-
expect(page.find(:css, "input[name=internal_link]").value).to be_empty
25-
end
23+
context "with page found by url" do
24+
it "has url value set" do
25+
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
26+
end
27+
28+
it "has hash fragment set" do
29+
expect(page.find(:css, "select[name=element_anchor]").value).to eq("#" + fragment)
30+
end
31+
32+
context "with locale in url" do
33+
let(:url) { "/#{alchemy_page.language_code}/#{alchemy_page.urlname}" }
2634

27-
it "should not have the value of the hash fragment" do
28-
expect(page.find(:css, "select[name=element_anchor]").value).to be_empty
35+
it "has url value set" do
36+
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
2937
end
3038
end
3139

32-
context "tab selected" do
33-
let(:is_selected) { true }
40+
context "with root url" do
41+
let(:language) { create(:alchemy_language, default: true, site: site) }
42+
let(:alchemy_page) { create(:alchemy_page, language: language) }
43+
44+
let(:url) { alchemy_page && "/" }
3445

35-
it "should have the value of the url" do
46+
it "has url value set to root url" do
3647
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
3748
end
49+
end
50+
51+
context "with locale root url" do
52+
let(:language) { create(:alchemy_language, default: true, site: site) }
53+
let(:alchemy_page) { create(:alchemy_page, language: language) }
3854

39-
it "should not have the value of the hash fragment" do
40-
expect(page.find(:css, "select[name=element_anchor]").value).to eq("#" + fragment)
55+
let(:url) { alchemy_page && "/en" }
56+
57+
it "has url value set to root url" do
58+
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
4159
end
4260
end
4361
end
62+
63+
context "with page not found by url" do
64+
let(:url) { "/foo" }
65+
66+
it "has no url value set" do
67+
expect(page.find(:css, "input[name=internal_link]").value).to be_nil
68+
end
69+
70+
it "has no hash fragment set" do
71+
expect(page.find(:css, "select[name=element_anchor]").value).to be_empty
72+
end
73+
end
4474
end

0 commit comments

Comments
 (0)