Skip to content

Commit

Permalink
Merge pull request #2892 from AlchemyCMS/fix-internal-link-selection
Browse files Browse the repository at this point in the history
Fix link selection of internal root or locale urls
  • Loading branch information
tvdeyen authored May 24, 2024
2 parents a8c2c23 + 1fe1918 commit a337dab
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
24 changes: 21 additions & 3 deletions app/components/alchemy/admin/link_dialog/internal_tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Alchemy
module Admin
module LinkDialog
class InternalTab < BaseTab
PAGE_URL_PATTERN = /\/(?<locale>[a-z]{2})?(?<slash>\/)?(?<urlname>.*)/

def title
Alchemy.t("link_overlay_tab_label.internal")
end
Expand Down Expand Up @@ -39,12 +41,28 @@ def uri
end

def page
@_page ||= uri ? Alchemy::Page.find_by(urlname: uri.path[1..]) : nil
@_page ||= if uri&.path == "/"
Alchemy::Current.site.default_language.root_page
elsif uri
Alchemy::Page.find_by(page_attributes)
end
end

def page_attributes
locale, _slash, urlname = uri.path.match(PAGE_URL_PATTERN)&.captures

if locale && urlname.present?
{language_code: locale, urlname: urlname}
elsif locale
{language_code: locale, language_root: true}
else
{urlname: urlname}
end
end

def page_select
label = label_tag("internal_link", Alchemy.t(:page), class: "control-label")
input = text_field_tag("internal_link", is_selected? ? uri : "", id: "internal_link")
input = text_field_tag("internal_link", page && uri, id: "internal_link")
page_select = render Alchemy::Admin::PageSelect.new(page, allow_clear: true).with_content(input)
content_tag("div", label + page_select, class: "input select")
end
Expand All @@ -53,7 +71,7 @@ def dom_id_select
fragment = "##{uri.fragment}" if uri&.fragment
label = label_tag("element_anchor", Alchemy.t(:anchor), class: "control-label")
options = [[page.nil? ? Alchemy.t("Select a page first") : Alchemy.t("None"), ""]]
options += [[fragment, fragment]] if is_selected? && fragment
options += [[fragment, fragment]] if page && fragment

select = select_tag("element_anchor", options_for_select(options, fragment), is: "alchemy-select", disabled: page.nil?)
select_component = content_tag("alchemy-dom-id-api-select", select, {page: page&.id})
Expand Down
54 changes: 42 additions & 12 deletions spec/components/alchemy/admin/link_dialog/internal_tab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "rails_helper"

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

before do
Alchemy::Current.site = site
render_inline(described_class.new(url, is_selected: is_selected, link_title: link_title, link_target: link_target))
end

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

context "link field" do
context "tab not selected" do
it "should not have the value of the url" do
expect(page.find(:css, "input[name=internal_link]").value).to be_empty
end
context "with page found by url" do
it "has url value set" do
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
end

it "has hash fragment set" do
expect(page.find(:css, "select[name=element_anchor]").value).to eq("#" + fragment)
end

context "with locale in url" do
let(:url) { "/#{alchemy_page.language_code}/#{alchemy_page.urlname}" }

it "should not have the value of the hash fragment" do
expect(page.find(:css, "select[name=element_anchor]").value).to be_empty
it "has url value set" do
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
end
end

context "tab selected" do
let(:is_selected) { true }
context "with root url" do
let(:language) { create(:alchemy_language, default: true, site: site) }
let(:alchemy_page) { create(:alchemy_page, language: language) }

let(:url) { alchemy_page && "/" }

it "should have the value of the url" do
it "has url value set to root url" do
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
end
end

context "with locale root url" do
let(:language) { create(:alchemy_language, default: true, site: site) }
let(:alchemy_page) { create(:alchemy_page, language: language) }

it "should not have the value of the hash fragment" do
expect(page.find(:css, "select[name=element_anchor]").value).to eq("#" + fragment)
let(:url) { alchemy_page && "/en" }

it "has url value set to root url" do
expect(page.find(:css, "input[name=internal_link]").value).to eq(url)
end
end
end

context "with page not found by url" do
let(:url) { "/foo" }

it "has no url value set" do
expect(page.find(:css, "input[name=internal_link]").value).to be_nil
end

it "has no hash fragment set" do
expect(page.find(:css, "select[name=element_anchor]").value).to be_empty
end
end
end

0 comments on commit a337dab

Please sign in to comment.