Simplify code to generate "See more" link

We can skip the `link_to(*options) if options` part if we accept anchor
tags in the `link_list` helper.
This commit is contained in:
Javi Martín
2021-02-02 01:08:10 +01:00
parent fb611d91ca
commit 7c4515d6ce
5 changed files with 31 additions and 23 deletions

View File

@@ -16,11 +16,10 @@ module SDG::TagList
count = count_out_of_limit(collection) count = count_out_of_limit(collection)
if count > 0 if count > 0
[ link_to "#{count}+",
"#{count}+", polymorphic_path(record),
polymorphic_path(record), class: "more-#{i18n_namespace}",
class: "more-#{i18n_namespace}", title: t("sdg.#{i18n_namespace}.filter.more", count: count) title: t("sdg.#{i18n_namespace}.filter.more", count: count)
]
end end
end end

View File

@@ -8,13 +8,7 @@ class SDG::Goals::PlainTagListComponent < ApplicationComponent
end end
def tags def tags
[*goal_tags, see_more_link].compact [*goal_tags, see_more_link(goals)].compact
end
def see_more_link
options = super(goals)
link_to(*options) if options.present?
end end
def goal_tags def goal_tags

View File

@@ -8,13 +8,7 @@ class SDG::Targets::PlainTagListComponent < ApplicationComponent
end end
def tags def tags
[*target_tags, see_more_link].compact [*target_tags, see_more_link(targets)].compact
end
def see_more_link
options = super(targets)
link_to(*options) if options.present?
end end
def target_tags def target_tags

View File

@@ -1,11 +1,15 @@
module LinkListHelper module LinkListHelper
def link_list(*links, **options) def link_list(*links, **options)
return "" if links.compact.empty? return "" if links.select(&:present?).empty?
tag.ul(options) do tag.ul(options) do
safe_join(links.compact.map do |text, url, current = false, **link_options| safe_join(links.select(&:present?).map do |text, url, current = false, **link_options|
tag.li(({ "aria-current": true } if current)) do tag.li(({ "aria-current": true } if current)) do
link_to text, url, link_options if url
link_to text, url, link_options
else
text
end
end end
end, "\n") end, "\n")
end end

View File

@@ -19,6 +19,15 @@ describe LinkListHelper do
expect(list).to be_html_safe expect(list).to be_html_safe
end end
it "accepts anchor tags" do
list = helper.link_list(link_to("Home", "/"), ["Info", "/info"], class: "menu")
expect(list).to eq '<ul class="menu">' +
'<li><a href="/">Home</a></li>' + "\n" +
'<li><a href="/info">Info</a></li></ul>'
expect(list).to be_html_safe
end
it "accepts options for links" do it "accepts options for links" do
render helper.link_list(["Home", "/", class: "root"], ["Info", "/info", id: "info"]) render helper.link_list(["Home", "/", class: "root"], ["Info", "/info", id: "info"])
@@ -30,7 +39,15 @@ describe LinkListHelper do
it "ignores nil entries" do it "ignores nil entries" do
render helper.link_list(["Home", "/", class: "root"], nil, ["Info", "/info", id: "info"]) render helper.link_list(["Home", "/", class: "root"], nil, ["Info", "/info", id: "info"])
expect(page).to have_css "a", count: 2 expect(page).to have_css "li", count: 2
expect(page).to have_css "a.root", count: 1, exact_text: "Home"
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
end
it "ignores empty entries" do
render helper.link_list(["Home", "/", class: "root"], "", ["Info", "/info", id: "info"])
expect(page).to have_css "li", count: 2
expect(page).to have_css "a.root", count: 1, exact_text: "Home" expect(page).to have_css "a.root", count: 1, exact_text: "Home"
expect(page).to have_css "a#info", count: 1, exact_text: "Info" expect(page).to have_css "a#info", count: 1, exact_text: "Info"
end end