From 3e2b65d7b84fa367fe3977e5d87516daf5a04f34 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sat, 28 Jan 2023 21:42:25 +0100 Subject: [PATCH] Move fetching of services to controller to avoid n+1 queries --- app/controllers/inbox_controller.rb | 17 ++++++++++------- app/views/inbox/_entry.html.haml | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/controllers/inbox_controller.rb b/app/controllers/inbox_controller.rb index 48cc5675..016e00c1 100644 --- a/app/controllers/inbox_controller.rb +++ b/app/controllers/inbox_controller.rb @@ -8,16 +8,12 @@ class InboxController < ApplicationController def show find_author find_inbox_entries - - if @author_user && @inbox_count.zero? - # rubocop disabled because of a false positive - flash[:info] = t(".author.info", author: @author) # rubocop:disable Rails/ActionControllerFlashBeforeRender - redirect_to inbox_path(last_id: params[:last_id]) - end + check_for_empty_filter @delete_id = find_delete_id - + @services = current_user.services @disabled = true if @inbox.empty? + respond_to do |format| format.html format.turbo_stream do @@ -50,6 +46,13 @@ class InboxController < ApplicationController private + def check_for_empty_filter + return unless @author_user && @inbox_count.zero? + + flash[:info] = t(".author.info", author: @author) + redirect_to inbox_path(last_id: params[:last_id]) + end + def find_author return if params[:author].blank? diff --git a/app/views/inbox/_entry.html.haml b/app/views/inbox/_entry.html.haml index e761af4f..1b41cb7c 100644 --- a/app/views/inbox/_entry.html.haml +++ b/app/views/inbox/_entry.html.haml @@ -40,9 +40,9 @@ = render "shared/format_link" .card-footer.d-none{ id: "ib-options-#{i.id}" } %h4= t(".sharing.heading") - - if current_user.services.count.positive? + - if @services.count.positive? .row - - current_user.services.each do |service| + - @services.each do |service| .col-md-3.col-sm-4.col-xs-6 %label %input{ type: "checkbox", name: "ib-share", checked: :checked, data: { ib_id: i.id, service: service.provider } }