From 7abca09e038a3aa653e9c39962c206bdae0fcba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 26 Jun 2021 23:14:45 +0200 Subject: [PATCH] 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. --- .../shared/link_list_component.html.erb | 1 + app/components/shared/link_list_component.rb | 16 ++++++++++------ .../shared/link_list_component_spec.rb | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 app/components/shared/link_list_component.html.erb diff --git a/app/components/shared/link_list_component.html.erb b/app/components/shared/link_list_component.html.erb new file mode 100644 index 000000000..86239013b --- /dev/null +++ b/app/components/shared/link_list_component.html.erb @@ -0,0 +1 @@ +<%= tag.ul(options) { safe_join(list_items, "\n") } %> diff --git a/app/components/shared/link_list_component.rb b/app/components/shared/link_list_component.rb index 840fafe47..683bc80db 100644 --- a/app/components/shared/link_list_component.rb +++ b/app/components/shared/link_list_component.rb @@ -7,12 +7,17 @@ class Shared::LinkListComponent < ApplicationComponent end def render? - links.select(&:present?).any? + present_links.any? end - def call - tag.ul(options) do - safe_join(links.select(&:present?).map do |text, url, current = false, **link_options| + 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 @@ -20,7 +25,6 @@ class Shared::LinkListComponent < ApplicationComponent text end end - end, "\n") + end end - end end diff --git a/spec/components/shared/link_list_component_spec.rb b/spec/components/shared/link_list_component_spec.rb index 2db5e3012..7f951fb30 100644 --- a/spec/components/shared/link_list_component_spec.rb +++ b/spec/components/shared/link_list_component_spec.rb @@ -21,7 +21,7 @@ describe Shared::LinkListComponent, type: :component do expect(list).to eq '" + '
  • Info
  • ' + "\n\n" end it "accepts anchor tags" do @@ -32,7 +32,7 @@ describe Shared::LinkListComponent, type: :component do expect(list).to eq '" + '
  • Info
  • ' + "\n\n" end it "accepts options for links" do