Add method to generate a list of links

A list of links is a very common pattern in the web, and we use it in
many places. Here we're applying it to one of the most simple ones; the
help page.

Generally speaking, I'm not a big fan of helpers, but there are methods
which IMHO qualify as helpers when:

* They do not deal with application objects but mainly strings and
  arrays
* They return text or an HTML tag
* Their logic is simple and splitting it into several methods is not
  necessary

Many Rails helpers, like `tag`, follow these principles.
This commit is contained in:
Javi Martín
2020-12-05 17:10:03 +01:00
parent 190ced4d2a
commit c156621a4c
3 changed files with 102 additions and 30 deletions

View File

@@ -0,0 +1,13 @@
module LinkListHelper
def link_list(*links, **options)
return "" if links.compact.empty?
tag.ul(options) do
safe_join(links.compact.map do |text, url, **link_options|
tag.li do
link_to text, url, link_options
end
end)
end
end
end

View File

@@ -1,34 +1,47 @@
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">
<ul class="menu-pages" data-magellan> <%= link_list(
<% if feature?(:debates) %> (
<li> [
<%= link_to t("pages.help.menu.debates"), "#debates", class: "button hollow expanded" %> t("pages.help.menu.debates"),
</li> "#debates",
<% end %> class: "button hollow expanded"
<% if feature?(:proposals) %> ] if feature?(:debates)
<li> ),
<%= link_to t("pages.help.menu.proposals"), "#proposals", class: "button hollow expanded" %> (
</li> [
<% end %> t("pages.help.menu.proposals"),
<% if feature?(:budgets) %> "#proposals",
<li> class: "button hollow expanded"
<%= link_to t("pages.help.menu.budgets"), "#budgets", class: "button hollow expanded" %> ] if feature?(:proposals)
</li> ),
<% end %> (
<% if feature?(:polls) %> [
<li> t("pages.help.menu.budgets"),
<%= link_to t("pages.help.menu.polls"), "#polls", class: "button hollow expanded" %> "#budgets",
</li> class: "button hollow expanded"
<% end %> ] if feature?(:budgets)
<% if feature?(:legislation) %> ),
<li> (
<%= link_to t("pages.help.menu.processes"), "#processes", class: "button hollow expanded" %> [
</li> t("pages.help.menu.polls"),
<% end %> "#polls",
<li> class: "button hollow expanded"
<%= link_to t("pages.help.menu.other"), "#other", class: "button hollow expanded" %> ] if feature?(:polls)
</li> ),
</ul> (
[
t("pages.help.menu.processes"),
"#processes",
class: "button hollow expanded"
]
),
[
t("pages.help.menu.other"),
"#other",
class: "button hollow expanded"
],
class: "menu-pages", "data-magellan": true
) %>
</div> </div>
</div> </div>

View File

@@ -0,0 +1,46 @@
require "rails_helper"
describe LinkListHelper do
describe "#link_list" do
it "returns an empty string with an empty list" do
expect(helper.link_list).to eq ""
end
it "returns nothing with a list of nil elements" do
expect(helper.link_list(nil, nil)).to eq ""
end
it "generates a list of links" do
list = helper.link_list(["Home", "/"], ["Info", "/info"], class: "menu")
expect(list).to eq '<ul class="menu"><li><a href="/">Home</a></li><li><a href="/info">Info</a></li></ul>'
expect(list).to be_html_safe
end
it "accepts options for links" do
render helper.link_list(["Home", "/", class: "root"], ["Info", "/info", id: "info"])
expect(page).to have_css "a", 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 nil entries" do
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 "a.root", count: 1, exact_text: "Home"
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
end
end
attr_reader :content
def render(content)
@content = content
end
def page
Capybara::Node::Simple.new(content)
end
end