Project

General

Profile

Patch #44169 » 0002-Refactor-query-preloads-and-controller-finding-logic.patch

Marius BĂLTEANU, 2026-06-17 00:21

View differences:

app/controllers/application_controller.rb
405 405
  # Find issues with a single :id param or :ids array param
406 406
  # Raises a Unauthorized exception if one of the issues is not visible
407 407
  def find_issues
408
    @issues = Issue.
409
      where(:id => (params[:id] || params[:ids])).
410
      preload(:project, :status, :tracker, :priority,
411
              :author, :assigned_to, :relations_to,
412
              {:custom_values => :custom_field}).
413
      to_a
408
    @issues = Issue.find_with_preloads(params[:id] || params[:ids])
414 409
    raise ActiveRecord::RecordNotFound if @issues.empty?
415 410
    raise Unauthorized unless @issues.all?(&:visible?)
416
    @projects = @issues.filter_map(&:project).uniq
417
    @project = @projects.first if @projects.size == 1
411
    find_project_from_items(@issues)
418 412
  rescue ActiveRecord::RecordNotFound
419 413
    render_404
420 414
  end
415
  # Helper method to find unique projects and single project from items list
416
  def find_project_from_items(items)
417
    @projects = items.filter_map(&:project).uniq
418
    @project = @projects.first if @projects.size == 1
419
  end
420

  
421

  
421 422
  def find_attachments
422 423
    if (attachments = params[:attachments]).present?
423 424
      att = attachments.values.collect do |attachment|
app/controllers/context_menus/time_entries_controller.rb
45 45
    private
46 46
    def find_time_entries
47
      @time_entries = TimeEntry.where(:id => params[:ids]).
48
        preload(:project => :time_entry_activities).
49
        preload(:user).to_a
47
      @time_entries = TimeEntry.find_with_preloads(params[:ids])
50 48
      if @time_entries.blank? || !@time_entries.all?(&:visible?)
51
        render_404;
49
        render_404
52 50
        return
53 51
      end
......
58 56
        @time_entry = @time_entries.first
59 57
      end
60
      @projects = @time_entries.filter_map(&:project).uniq
61
      @project = @projects.first if @projects.size == 1
58
      find_project_from_items(@time_entries)
62 59
    end
60

  
63 61
  end
64 62
end
app/controllers/timelog_controller.rb
276 276
  end
277 277
  def find_time_entries
278
    @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
279
      preload(:project => :time_entry_activities).
280
      preload(:user).to_a
278
    @time_entries = TimeEntry.find_with_preloads(params[:id] || params[:ids])
281 279
    raise ActiveRecord::RecordNotFound if @time_entries.empty?
282 280
    raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
283
    @projects = @time_entries.filter_map(&:project).uniq
284
    @project = @projects.first if @projects.size == 1
281
    find_project_from_items(@time_entries)
285 282
  rescue ActiveRecord::RecordNotFound
286 283
    render_404
287 284
  end
285

  
288 286
  def find_optional_issue
289 287
    if params[:issue_id].present?
290 288
      @issue = Issue.find(params[:issue_id])
app/models/issue.rb
168 168
    end
169 169
  end
170
  # Returns issues found by IDs with preloaded associations
171
  def self.find_with_preloads(ids)
172
    where(:id => ids).
173
      preload(:project, :status, :tracker, :priority,
174
              :author, :assigned_to, :relations_to,
175
              {:custom_values => :custom_field}).
176
      to_a
177
  end
178

  
170 179
  # Returns true if usr or current user is allowed to view the issue
171 180
  def visible?(usr=nil)
172 181
    (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
app/models/time_entry.rb
96 96
    end
97 97
  end
98
  # Returns time entries found by IDs with preloaded associations
99
  def self.find_with_preloads(ids)
100
    where(:id => ids).
101
      preload(:project => :time_entry_activities).
102
      preload(:user).to_a
103
  end
104

  
105

  
98 106
  # Returns true if user or current user is allowed to view the time entry
99 107
  def visible?(user=nil)
100 108
    (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
(2-2/3)