From 7775e1fbd7f8a87ba31cfd79a8e93b31a4375cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C4=82LTEANU?= Date: Wed, 17 Jun 2026 01:15:48 +0300 Subject: [PATCH 2/2] Refactor query preloads and controller finding logic (#44169) --- app/controllers/application_controller.rb | 17 +++++++++-------- .../context_menus/time_entries_controller.rb | 10 ++++------ app/controllers/timelog_controller.rb | 8 +++----- app/models/issue.rb | 9 +++++++++ app/models/time_entry.rb | 8 ++++++++ 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d06344c0a..1f63f645d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -405,21 +405,22 @@ class ApplicationController < ActionController::Base # Find issues with a single :id param or :ids array param # Raises a Unauthorized exception if one of the issues is not visible def find_issues - @issues = Issue. - where(:id => (params[:id] || params[:ids])). - preload(:project, :status, :tracker, :priority, - :author, :assigned_to, :relations_to, - {:custom_values => :custom_field}). - to_a + @issues = Issue.find_with_preloads(params[:id] || params[:ids]) raise ActiveRecord::RecordNotFound if @issues.empty? raise Unauthorized unless @issues.all?(&:visible?) - @projects = @issues.filter_map(&:project).uniq - @project = @projects.first if @projects.size == 1 + find_project_from_items(@issues) rescue ActiveRecord::RecordNotFound render_404 end + # Helper method to find unique projects and single project from items list + def find_project_from_items(items) + @projects = items.filter_map(&:project).uniq + @project = @projects.first if @projects.size == 1 + end + + def find_attachments if (attachments = params[:attachments]).present? att = attachments.values.collect do |attachment| diff --git a/app/controllers/context_menus/time_entries_controller.rb b/app/controllers/context_menus/time_entries_controller.rb index 3e0fd1477..e1d49ee12 100644 --- a/app/controllers/context_menus/time_entries_controller.rb +++ b/app/controllers/context_menus/time_entries_controller.rb @@ -45,12 +45,10 @@ module ContextMenus private def find_time_entries - @time_entries = TimeEntry.where(:id => params[:ids]). - preload(:project => :time_entry_activities). - preload(:user).to_a + @time_entries = TimeEntry.find_with_preloads(params[:ids]) if @time_entries.blank? || !@time_entries.all?(&:visible?) - render_404; + render_404 return end @@ -58,8 +56,8 @@ module ContextMenus @time_entry = @time_entries.first end - @projects = @time_entries.filter_map(&:project).uniq - @project = @projects.first if @projects.size == 1 + find_project_from_items(@time_entries) end + end end diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb index 0195c8ced..afd21a475 100644 --- a/app/controllers/timelog_controller.rb +++ b/app/controllers/timelog_controller.rb @@ -276,19 +276,17 @@ class TimelogController < ApplicationController end def find_time_entries - @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]). - preload(:project => :time_entry_activities). - preload(:user).to_a + @time_entries = TimeEntry.find_with_preloads(params[:id] || params[:ids]) raise ActiveRecord::RecordNotFound if @time_entries.empty? raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)} - @projects = @time_entries.filter_map(&:project).uniq - @project = @projects.first if @projects.size == 1 + find_project_from_items(@time_entries) rescue ActiveRecord::RecordNotFound render_404 end + def find_optional_issue if params[:issue_id].present? @issue = Issue.find(params[:issue_id]) diff --git a/app/models/issue.rb b/app/models/issue.rb index 4c16fb6af..ed9b5dbd3 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -168,6 +168,15 @@ class Issue < ApplicationRecord end end + # Returns issues found by IDs with preloaded associations + def self.find_with_preloads(ids) + where(:id => ids). + preload(:project, :status, :tracker, :priority, + :author, :assigned_to, :relations_to, + {:custom_values => :custom_field}). + to_a + end + # Returns true if usr or current user is allowed to view the issue def visible?(usr=nil) (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user| diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index cae3c7a1b..57e2bdd91 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -96,6 +96,14 @@ class TimeEntry < ApplicationRecord end end + # Returns time entries found by IDs with preloaded associations + def self.find_with_preloads(ids) + where(:id => ids). + preload(:project => :time_entry_activities). + preload(:user).to_a + end + + # Returns true if user or current user is allowed to view the time entry def visible?(user=nil) (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user| -- 2.50.1 (Apple Git-155)