Skip to content

Commit 26224c7

Browse files
committed
Refactor event host partial to render a mixed array of hosts instead of directly iterating through the event_host records.
1 parent 2e914ff commit 26224c7

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

app/helpers/better_together/events_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
module BetterTogether
44
# View helpers for events
55
module EventsHelper
6+
# Return hosts for an event that the current user is authorized to view.
7+
# Keeps view markup small and centralizes the policy logic for testing.
8+
def visible_event_hosts(event)
9+
return [] unless event.respond_to?(:event_hosts)
10+
11+
event.event_hosts.map { |eh| eh.host if policy(eh.host).show? }.compact
12+
end
613
end
714
end

app/views/better_together/events/_event_host.html.erb

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%# locals: (event:, visible_hosts: visible_event_hosts(event)) %>
2+
3+
<% if visible_hosts.any? %>
4+
<%# visible_hosts is now a mixed array of host instances instead of an EventHostCollection %>
5+
<div id="<%= dom_id(event, :event_hosts) %>">
6+
<h3><%= t('better_together.events.hosted_by') %></h3>
7+
8+
<div class="mt-2 row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
9+
<%= render visible_hosts %>
10+
</div>
11+
</div>
12+
<% end %>

app/views/better_together/events/show.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@
8888
<%= @resource.description.presence || 'No description available.' %>
8989
</p>
9090

91-
<div>
92-
<%= render partial: 'better_together/events/event_host', locals: {hosts: @event.event_hosts} %>
93-
</div>
91+
<%= render 'better_together/events/event_hosts', event: @event %>
9492
</div>
9593
</section>
9694
</div>

0 commit comments

Comments
 (0)