Skip to content

Commit 7df6ae6

Browse files
committed
Generate the ids for the workshops a member is attending in one go
This removes a call to '#attending?' for every workshop, instead creating a single method to generate the list. Running locally with a small sample, it reduced loading time for the homepage by 70% Signed-off-by: jonathan.kerr <3410350+jonodrew@users.noreply.github.com>
1 parent 8776650 commit 7df6ae6

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

app/controllers/dashboard_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class DashboardController < ApplicationController
44

55
DEFAULT_UPCOMING_EVENTS = 5
66

7-
helper_method :year_param
7+
helper_method :year_param, :attending_workshops
88

99
def show
1010
@chapters = Chapter.active.all.order(:created_at)
@@ -13,7 +13,7 @@ def show
1313
hash[key] = EventPresenter.decorate_collection(value)
1414
hash
1515
end
16-
16+
@attending_ids = attending_workshops
1717
@testimonials = Testimonial.order(Arel.sql('RANDOM()')).limit(5).includes(:member)
1818
end
1919

@@ -84,4 +84,8 @@ def all_events(workshops)
8484

8585
[*workshops, *events, meeting].uniq.compact
8686
end
87+
88+
def attending_workshops
89+
current_user.nil? ? Set.new : current_user.workshop_invitations.accepted.pluck(:id).to_set
90+
end
8791
end

app/views/dashboard/show.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
%h2.h3.mb-4= t('homepage.events.upcoming')
7272
- @upcoming_workshops.each do |date, workshops|
7373
%h3.h5= date
74-
= render workshops
74+
= render workshops, attending_ids: @attending_ids
7575

7676
.col-lg-4.pl-lg-5
7777
%h3

app/views/events/_event.html.haml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
- if event.chapter
66
%span.badge.bg-primary.mb-3.mb-md-0
77
= link_to event.chapter.name, event.chapter.slug, class: 'text-light text-decoration-none'
8-
- if @user
9-
- if @user.attending?(event.__getobj__)
8+
- if !attending_ids.blank?
9+
- if attending_ids.include?(event.id)
1010
%span.badge.bg-success.mb-3.mb-md-0
1111
= link_to 'Attending', event.path, class: 'text-light text-decoration-none'
12+
- if @user
1213
- if @user.event_organiser?(event)
1314
%span.badge.bg-secondary.mb-3.mb-md-0
1415
= link_to 'Manage', event.admin_path, class: 'text-light text-decoration-none'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
= render partial: 'events/event', locals: { event: workshop }
1+
= render partial: 'events/event', locals: { event: workshop, attending_ids: attending_ids }

0 commit comments

Comments
 (0)