diff --git a/app/controllers/apps_controller.rb b/app/controllers/apps_controller.rb index 93ae780..6cdd8c5 100644 --- a/app/controllers/apps_controller.rb +++ b/app/controllers/apps_controller.rb @@ -52,16 +52,7 @@ class AppsController < InheritedResources::Base protected def collection - @apps ||= begin - apps = end_of_association_chain.all - - # Sort apps by number of unresolved errs, then problem counts. - apps.sort do |a, b| - (b.unresolved_count <=> a.unresolved_count).nonzero? || - (b.problem_count <=> a.problem_count).nonzero? || - a.name <=> b.name - end - end + @apps ||= end_of_association_chain.all.sort end def initialize_subclassed_issue_tracker diff --git a/app/models/app.rb b/app/models/app.rb index fc5095b..951a708 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -1,6 +1,7 @@ class App include Mongoid::Document include Mongoid::Timestamps + include Comparable field :name, :type => String field :api_key @@ -177,6 +178,13 @@ class App @problem_count ||= problems.count end + # Compare by number of unresolved errs, then problem counts. + def <=>(other) + (other.unresolved_count <=> unresolved_count).nonzero? || + (other.problem_count <=> problem_count).nonzero? || + name <=> other.name + end + protected def store_cached_attributes_on_problems diff --git a/spec/models/app_spec.rb b/spec/models/app_spec.rb index e55f037..76f7933 100644 --- a/spec/models/app_spec.rb +++ b/spec/models/app_spec.rb @@ -23,6 +23,31 @@ describe App do end end + describe '<=>' do + it 'is compared by unresolved count' do + app_0 = stub_model(App, :name => 'app', :unresolved_count => 1, :problem_count => 1) + app_1 = stub_model(App, :name => 'app', :unresolved_count => 0, :problem_count => 1) + + app_0.should < app_1 + app_1.should > app_0 + end + + it 'is compared by problem count' do + app_0 = stub_model(App, :name => 'app', :unresolved_count => 0, :problem_count => 1) + app_1 = stub_model(App, :name => 'app', :unresolved_count => 0, :problem_count => 0) + + app_0.should < app_1 + app_1.should > app_0 + end + + it 'is compared by name' do + app_0 = stub_model(App, :name => 'app_0', :unresolved_count => 0, :problem_count => 0) + app_1 = stub_model(App, :name => 'app_1', :unresolved_count => 0, :problem_count => 0) + + app_0.should < app_1 + app_1.should > app_0 + end + end context 'being created' do it 'generates a new api-key' do -- libgit2 0.21.2