Merge pull request #1014 from Retrospring/fix/inbox-services-query

Move fetching of services to controller to avoid n+1 queries
This commit is contained in:
Karina Kwiatek 2023-01-29 23:07:46 +01:00 committed by GitHub
commit 4dc5a65f74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 14 deletions

View file

@ -5,7 +5,7 @@ class InboxController < ApplicationController
after_action :mark_inbox_entries_as_read, only: %i[show] after_action :mark_inbox_entries_as_read, only: %i[show]
def show def show # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
find_author find_author
find_inbox_entries find_inbox_entries
@ -13,15 +13,17 @@ class InboxController < ApplicationController
# rubocop disabled because of a false positive # rubocop disabled because of a false positive
flash[:info] = t(".author.info", author: @author) # rubocop:disable Rails/ActionControllerFlashBeforeRender flash[:info] = t(".author.info", author: @author) # rubocop:disable Rails/ActionControllerFlashBeforeRender
redirect_to inbox_path(last_id: params[:last_id]) redirect_to inbox_path(last_id: params[:last_id])
return
end end
@delete_id = find_delete_id @delete_id = find_delete_id
@disabled = true if @inbox.empty? @disabled = true if @inbox.empty?
services = current_user.services
respond_to do |format| respond_to do |format|
format.html format.html { render "show", locals: { services: } }
format.turbo_stream do format.turbo_stream do
render "show", layout: false, status: :see_other render "show", locals: { services: }, layout: false, status: :see_other
# rubocop disabled as just flipping a flag doesn't need to have validations to be run # rubocop disabled as just flipping a flag doesn't need to have validations to be run
@inbox.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations @inbox.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations

View file

@ -40,9 +40,9 @@
= render "shared/format_link" = render "shared/format_link"
.card-footer.d-none{ id: "ib-options-#{i.id}" } .card-footer.d-none{ id: "ib-options-#{i.id}" }
%h4= t(".sharing.heading") %h4= t(".sharing.heading")
- if current_user.services.count.positive? - if services.count.positive?
.row .row
- current_user.services.each do |service| - services.each do |service|
.col-md-3.col-sm-4.col-xs-6 .col-md-3.col-sm-4.col-xs-6
%label %label
%input{ type: "checkbox", name: "ib-share", checked: :checked, data: { ib_id: i.id, service: service.provider } } %input{ type: "checkbox", name: "ib-share", checked: :checked, data: { ib_id: i.id, service: service.provider } }

View file

@ -1,6 +1,6 @@
#entries #entries
- @inbox.each do |i| - @inbox.each do |i|
= render "inbox/entry", i: = render "inbox/entry", services:, i:
- if @inbox.empty? - if @inbox.empty?
%p.empty= t(".empty") %p.empty= t(".empty")

View file

@ -1,6 +1,6 @@
= turbo_stream.append "entries" do = turbo_stream.append "entries" do
- @inbox.each do |i| - @inbox.each do |i|
= render "inbox/entry", i: = render "inbox/entry", services:, i:
= turbo_stream.update "paginator" do = turbo_stream.update "paginator" do
- if @more_data_available - if @more_data_available

View file

@ -179,9 +179,7 @@ describe InboxController, type: :controller do
inbox: [], inbox: [],
inbox_last_id: nil, inbox_last_id: nil,
more_data_available: false, more_data_available: false,
inbox_count: 0, inbox_count: 0
delete_id: "ib-delete-all",
disabled: true
} }
end end
end end
@ -213,9 +211,7 @@ describe InboxController, type: :controller do
inbox: [], inbox: [],
inbox_last_id: nil, inbox_last_id: nil,
more_data_available: false, more_data_available: false,
inbox_count: 0, inbox_count: 0
delete_id: "ib-delete-all",
disabled: true
} }
end end
end end