diff --git a/app/controllers/apps_controller.rb b/app/controllers/apps_controller.rb index 4fb7e8c..7e3a994 100644 --- a/app/controllers/apps_controller.rb +++ b/app/controllers/apps_controller.rb @@ -50,10 +50,26 @@ class AppsController < InheritedResources::Base protected def collection - # Sort apps by number of unresolved errs, descending. - # Caches the unresolved err counts while performing the sort. @unresolved_counts, @problem_counts = {}, {} - @apps ||= end_of_association_chain.all.sort{|a,b| + @apps ||= begin + apps = end_of_association_chain.all + + if apps.count > 1 + apps = sort_by_unresolved_errs(apps) + elsif apps.count > 0 + app = apps.first + @unresolved_counts[app.id] ||= app.problems.unresolved.count + @problem_counts[app.id] ||= app.problems.count + end + + apps + end + end + + # Sort apps by number of unresolved errs, descending. + # Caches the unresolved err counts while performing the sort. + def sort_by_unresolved_errs(apps) + apps.sort{|a,b| [a,b].each do |app| @unresolved_counts[app.id] ||= app.problems.unresolved.count @problem_counts[app.id] ||= app.problems.count diff --git a/spec/controllers/apps_controller_spec.rb b/spec/controllers/apps_controller_spec.rb index f242885..d4684c1 100644 --- a/spec/controllers/apps_controller_spec.rb +++ b/spec/controllers/apps_controller_spec.rb @@ -31,6 +31,17 @@ describe AppsController do assigns(:apps).should_not include(unwatched_app) end end + + context 'when there is only one app' do + it 'sets unresolved_counts and problem_counts variables' do + sign_in Fabricate(:admin) + app = Fabricate(:app) + get :index + + assigns(:unresolved_counts).should == {app.id => 0} + assigns(:problem_counts).should == {app.id => 0} + end + end end describe "GET /apps/:id" do -- libgit2 0.21.2