Remove a bunch of N+1s

related to notification and comment rendering
This commit is contained in:
Mike Dalessio
2025-11-27 11:08:45 -05:00
parent b78977f563
commit 79c9ea2ce1
8 changed files with 10 additions and 9 deletions
@@ -1,6 +1,6 @@
class Notifications::TraysController < ApplicationController
def show
@notifications = Current.user.notifications.unread.ordered.limit(100)
@notifications = Current.user.notifications.preloaded.unread.ordered.limit(100)
# Invalidate on the whole set instead of the unread set since the max updated at in the unread set
# can stay the same when reading old notifications.
+1 -1
View File
@@ -15,7 +15,7 @@ module Card::Assignable
end
def assigned_to?(user)
assignments.exists? assignee: user
assignments.any? { |a| a.assignee_id == user.id }
end
def assigned?
+2 -1
View File
@@ -4,11 +4,12 @@ class Comment < ApplicationRecord
belongs_to :account, default: -> { card.account }
belongs_to :card, touch: true
belongs_to :creator, class_name: "User", default: -> { Current.user }
has_many :reactions, dependent: :delete_all
has_many :reactions, -> { order(:created_at) }, dependent: :delete_all
has_rich_text :body
scope :chronologically, -> { order created_at: :asc, id: :desc }
scope :preloaded, -> { with_rich_text_body.includes(reactions: :reacter) }
scope :by_system, -> { joins(:creator).where(creator: { role: "system" }) }
scope :by_user, -> { joins(:creator).where.not(creator: { role: "system" }) }
+1 -1
View File
@@ -14,7 +14,7 @@ class Notification < ApplicationRecord
after_destroy_commit :broadcast_read
after_create :bundle
scope :preloaded, -> { preload(:creator, :account, source: [ :board, :creator ]) }
scope :preloaded, -> { preload(:creator, :account, source: [ :board, :creator, { eventable: [ :closure, :board, :assignments ] } ]) }
delegate :notifiable_target, to: :source
delegate :card, to: :source
+1 -1
View File
@@ -1,6 +1,6 @@
<%= messages_tag(card) do %>
<% if card.published? %>
<%= render partial: "cards/comments/comment", collection: card.comments.chronologically, cached: true %>
<%= render partial: "cards/comments/comment", collection: card.comments.preloaded.chronologically, cached: true %>
<%= render "cards/comments/new", card: card %>
<%= render "cards/comments/watchers", card: card %>
@@ -1,7 +1,7 @@
<%= turbo_frame_tag comment, :reacting do %>
<div class="reactions">
<div id="<%= dom_id(comment, :reactions) %>" class="reactions__list">
<%= render partial: "cards/comments/reactions/reaction", collection: comment.reactions.includes(:reacter).ordered %>
<%= render partial: "cards/comments/reactions/reaction", collection: comment.reactions %>
</div>
<%= turbo_frame_tag comment, :new_reaction do %>
@@ -11,12 +11,12 @@ class Cards::AssignmentsControllerTest < ActionDispatch::IntegrationTest
end
test "create" do
assert_changes "cards(:logo).assigned_to?(users(:david))", from: false, to: true do
assert_changes "cards(:logo).reload.assigned_to?(users(:david))", from: false, to: true do
post card_assignments_path(cards(:logo)), params: { assignee_id: users(:david).id }, as: :turbo_stream
assert_meta_replaced(cards(:logo))
end
assert_changes "cards(:logo).assigned_to?(users(:david))", from: true, to: false do
assert_changes "cards(:logo).reload.assigned_to?(users(:david))", from: true, to: false do
post card_assignments_path(cards(:logo)), params: { assignee_id: users(:david).id }, as: :turbo_stream
assert_meta_replaced(cards(:logo))
end
+1 -1
View File
@@ -37,7 +37,7 @@ class CardTest < ActiveSupport::TestCase
assert_difference({ -> { cards(:logo).assignees.count } => -1, -> { Event.count } => +1 }) do
cards(:logo).toggle_assignment users(:kevin)
end
assert_not cards(:logo).assigned_to?(users(:kevin))
assert_not cards(:logo).reload.assigned_to?(users(:kevin))
unassign_event = Event.last
assert_equal "card_unassigned", unassign_event.action
assert_equal [ users(:kevin) ], unassign_event.assignees