Commit 135597648fd2fd2051be8cf8a55a520c0801dc64

Authored by Nathan Broadbent
1 parent ca3640b8
Exists in master and in 1 other branch production

Separate count caching and sorting to simplify code in AppsController#collection.

Showing 1 changed file with 8 additions and 17 deletions   Show diff stats
app/controllers/apps_controller.rb
@@ -54,31 +54,22 @@ class AppsController < InheritedResources::Base @@ -54,31 +54,22 @@ class AppsController < InheritedResources::Base
54 @apps ||= begin 54 @apps ||= begin
55 apps = end_of_association_chain.all 55 apps = end_of_association_chain.all
56 56
57 - if apps.count > 1  
58 - apps = sort_by_unresolved_errs(apps)  
59 - elsif apps.count > 0  
60 - app = apps.first 57 + # Cache counts for unresolved errs and problems
  58 + apps.each do |app|
61 @unresolved_counts[app.id] ||= app.problems.unresolved.count 59 @unresolved_counts[app.id] ||= app.problems.unresolved.count
62 @problem_counts[app.id] ||= app.problems.count 60 @problem_counts[app.id] ||= app.problems.count
63 end 61 end
64 62
  63 + # Sort apps by number of unresolved errs, then problem counts.
  64 + apps.sort! do |a,b|
  65 + unresolved = @unresolved_counts[b.id] <=> @unresolved_counts[a.id]
  66 + unresolved != 0 ? unresolved : @problem_counts[b.id] <=> @problem_counts[a.id]
  67 + end
  68 +
65 apps 69 apps
66 end 70 end
67 end 71 end
68 72
69 - # Sort apps by number of unresolved errs, descending.  
70 - # Caches the unresolved err counts while performing the sort.  
71 - def sort_by_unresolved_errs(apps)  
72 - apps.sort{|a,b|  
73 - [a,b].each do |app|  
74 - @unresolved_counts[app.id] ||= app.problems.unresolved.count  
75 - @problem_counts[app.id] ||= app.problems.count  
76 - end  
77 - unresolved = @unresolved_counts[b.id] <=> @unresolved_counts[a.id]  
78 - unresolved != 0 ? unresolved : @problem_counts[b.id] <=> @problem_counts[a.id]  
79 - }  
80 - end  
81 -  
82 def initialize_subclassed_issue_tracker 73 def initialize_subclassed_issue_tracker
83 if params[:app][:issue_tracker_attributes] && tracker_type = params[:app][:issue_tracker_attributes][:type] 74 if params[:app][:issue_tracker_attributes] && tracker_type = params[:app][:issue_tracker_attributes][:type]
84 if IssueTracker.subclasses.map(&:name).concat(["IssueTracker"]).include?(tracker_type) 75 if IssueTracker.subclasses.map(&:name).concat(["IssueTracker"]).include?(tracker_type)