Files
nairobi/app/components/shared/link_list_component.rb
Javi Martín 7abca09e03 Extract methods in link list component
Since we're simplifying the main method, we can use a view file instead
of the `call` method. This way we make the code more consistent with the
rest of our components, since we always use a separate file.

Doing so generates an extra newline at the end of the generated HTML, so
we need to change a couple of tests a little bit.
2021-07-05 22:27:39 +02:00

31 lines
578 B
Ruby

class Shared::LinkListComponent < ApplicationComponent
attr_reader :links, :options
def initialize(*links, **options)
@links = links
@options = options
end
def render?
present_links.any?
end
private
def present_links
links.select(&:present?)
end
def list_items
present_links.map do |text, url, current = false, **link_options|
tag.li(({ "aria-current": true } if current)) do
if url
link_to text, url, link_options
else
text
end
end
end
end
end