Remove duplication in group switcher component

We can extract a method to reduce the amount of ERB code and remove the
duplication in the link texts. We also make the list consistent; now we
always use a <strong> tag in the group name, no matter whether there are
many groups or only one.
This commit is contained in:
Javi Martín
2025-03-07 23:01:57 +01:00
parent 3484c6b7b8
commit a03037f0ba
2 changed files with 11 additions and 13 deletions

View File

@@ -2,21 +2,10 @@
<% if other_groups.one? %> <% if other_groups.one? %>
<p> <p>
<%= currently_showing_text %> <%= currently_showing_text %>
<%= link_to t("admin.budget_headings.group_switcher.the_other_group", group: other_groups.first.name), <%= link_to_group(other_groups.first) %>
headings_path(other_groups.first) %>
</p> </p>
<% else %> <% else %>
<p><%= currently_showing_text %></p> <p><%= currently_showing_text %></p>
<ul> <%= link_list(*other_groups.map { |group| link_to_group(group) }) %>
<% other_groups.each do |other_group| %>
<li>
<%= link_to sanitize(t(
"admin.budget_headings.group_switcher.the_other_group",
group: tag.strong(other_group.name)
)),
headings_path(other_group) %>
</li>
<% end %>
</ul>
<% end %> <% end %>
</div> </div>

View File

@@ -1,5 +1,6 @@
class Admin::BudgetsWizard::Headings::GroupSwitcherComponent < ApplicationComponent class Admin::BudgetsWizard::Headings::GroupSwitcherComponent < ApplicationComponent
attr_reader :group attr_reader :group
use_helpers :link_list
def initialize(group) def initialize(group)
@group = group @group = group
@@ -26,4 +27,12 @@ class Admin::BudgetsWizard::Headings::GroupSwitcherComponent < ApplicationCompon
def currently_showing_text def currently_showing_text
sanitize(t("admin.budget_headings.group_switcher.currently_showing", group: group.name)) sanitize(t("admin.budget_headings.group_switcher.currently_showing", group: group.name))
end end
def link_to_group(group)
link_to(link_to_group_text(group), headings_path(group))
end
def link_to_group_text(group)
sanitize(t("admin.budget_headings.group_switcher.the_other_group", group: tag.strong(group.name)))
end
end end