From c6578fc1e2f14d22ac82c2b40e3419eb8407b6c3 Mon Sep 17 00:00:00 2001 From: Joshua Young Date: Fri, 30 Aug 2024 23:29:45 +1000 Subject: [PATCH] Eliminate some N+1s --- Gemfile.lock | 1 + app/controllers/application_controller.rb | 4 +++- app/controllers/schedules_controller.rb | 2 +- app/controllers/sessions_controller.rb | 4 ++-- app/views/layouts/_bottom_navbar.html.erb | 2 +- app/views/schedules/show.html.erb | 2 +- app/views/sessions/_card.html.erb | 4 ++-- app/views/sessions/index.html.erb | 2 +- app/views/sessions/show.html.erb | 4 ++-- 9 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a054b2a8..3b3b1f4c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -625,6 +625,7 @@ PLATFORMS arm64-darwin-21 arm64-darwin-22 arm64-darwin-23 + arm64-darwin-24 x86_64-linux DEPENDENCIES diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2c2fbf3c..6df0c7c9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -12,7 +12,9 @@ class ApplicationController < ActionController::Base def current_profile = current_user&.profile # TODO: Must change after implementing multi-conference support - def current_conference = Conference.last + def current_conference + @_current_conference ||= Conference.last + end def vapid_public_key Base64.urlsafe_decode64(ENV["VAPID_PUBLIC_KEY"]).bytes.to_json diff --git a/app/controllers/schedules_controller.rb b/app/controllers/schedules_controller.rb index 67aa3c74..f3d070f9 100644 --- a/app/controllers/schedules_controller.rb +++ b/app/controllers/schedules_controller.rb @@ -3,7 +3,7 @@ def show @sessions = SessionQuery.new( relation: current_user.sessions.where(conference: current_conference), params: filter_params - ).call.includes(:attendees, :location, :speakers, :tags).order(:starts_at) + ).call.includes(:location, :tags, speakers: [profile: :image_attachment]).order(:starts_at) end private diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 40ce9583..c4d810cb 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,11 +4,11 @@ def index @sessions = SessionQuery.new( relation: sessions.joins(:location).distinct, params: filter_params - ).call.includes(:attendees, :tags).order(:starts_at) + ).call.includes(:location, :tags, speakers: [profile: :image_attachment]).order(:starts_at) end def show - @session = sessions.friendly.find(params[:id]) + @session = sessions.friendly.includes(:location, :tags, speakers: [profile: :image_attachment]).find(params[:id]) end private diff --git a/app/views/layouts/_bottom_navbar.html.erb b/app/views/layouts/_bottom_navbar.html.erb index 1f7b5b69..a40c4d5a 100644 --- a/app/views/layouts/_bottom_navbar.html.erb +++ b/app/views/layouts/_bottom_navbar.html.erb @@ -54,7 +54,7 @@ <% if unread_notifications.present? %>
- <%= unread_notifications.count < 10 ? unread_notifications.count : "+9" %> + <%= unread_notifications.length < 10 ? unread_notifications.length : "+9" %>
<% end %> diff --git a/app/views/schedules/show.html.erb b/app/views/schedules/show.html.erb index beaee2a3..df6e2d9c 100644 --- a/app/views/schedules/show.html.erb +++ b/app/views/schedules/show.html.erb @@ -32,7 +32,7 @@
-
<%= pluralize(sessions.count, "Session") %>
+
<%= pluralize(sessions.length, "Session") %>
<% end %> <% sessions.each do |session| %> <%= render( diff --git a/app/views/sessions/_card.html.erb b/app/views/sessions/_card.html.erb index f17d1470..d005cfe1 100644 --- a/app/views/sessions/_card.html.erb +++ b/app/views/sessions/_card.html.erb @@ -58,7 +58,7 @@ :div, class: [ "flex flex-col ml-2", - "max-w-[200px]": session.speakers.count > 1 + "max-w-[200px]": session.speakers.length > 1 ] ) do %>

<%= speaker.name %>

@@ -71,7 +71,7 @@ <% end %>
- <% if session.tags.exists? %> + <% if session.tags.present? %>
<% session.tags.each do |tag| %>
diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb index 546d1d87..77a73ef8 100644 --- a/app/views/sessions/index.html.erb +++ b/app/views/sessions/index.html.erb @@ -32,7 +32,7 @@
-
<%= pluralize(sessions.count, "Session") %>
+
<%= pluralize(sessions.length, "Session") %>
<% end %> <% sessions.each do |session| %> <%= render( diff --git a/app/views/sessions/show.html.erb b/app/views/sessions/show.html.erb index d1dd13ed..9d691a2c 100644 --- a/app/views/sessions/show.html.erb +++ b/app/views/sessions/show.html.erb @@ -48,7 +48,7 @@ <%= content_tag(:div, class: [ "flex flex-col ml-2", - "max-w-[200px]": @session.speakers.count > 1 + "max-w-[200px]": @session.speakers.length > 1 ] ) do %>

<%= speaker.name %>

<%= speaker.job_title %>

@@ -94,7 +94,7 @@

- <%= "About the #{'speaker'.pluralize(@session.speakers.count)}" %> + <%= "About the #{'speaker'.pluralize(@session.speakers.length)}" %>

<% @session.speakers.each do |speaker| %>