Commit 4bcc2aee4d02b30e24b9a28e527a8775b867453f

Authored by Sergey Nartimov
1 parent a9625392
Exists in master and in 1 other branch production

make apps comparable

app/controllers/apps_controller.rb
@@ -52,16 +52,7 @@ class AppsController < InheritedResources::Base @@ -52,16 +52,7 @@ class AppsController < InheritedResources::Base
52 52
53 protected 53 protected
54 def collection 54 def collection
55 - @apps ||= begin  
56 - apps = end_of_association_chain.all  
57 -  
58 - # Sort apps by number of unresolved errs, then problem counts.  
59 - apps.sort do |a, b|  
60 - (b.unresolved_count <=> a.unresolved_count).nonzero? ||  
61 - (b.problem_count <=> a.problem_count).nonzero? ||  
62 - a.name <=> b.name  
63 - end  
64 - end 55 + @apps ||= end_of_association_chain.all.sort
65 end 56 end
66 57
67 def initialize_subclassed_issue_tracker 58 def initialize_subclassed_issue_tracker
app/models/app.rb
1 class App 1 class App
2 include Mongoid::Document 2 include Mongoid::Document
3 include Mongoid::Timestamps 3 include Mongoid::Timestamps
  4 + include Comparable
4 5
5 field :name, :type => String 6 field :name, :type => String
6 field :api_key 7 field :api_key
@@ -177,6 +178,13 @@ class App @@ -177,6 +178,13 @@ class App
177 @problem_count ||= problems.count 178 @problem_count ||= problems.count
178 end 179 end
179 180
  181 + # Compare by number of unresolved errs, then problem counts.
  182 + def <=>(other)
  183 + (other.unresolved_count <=> unresolved_count).nonzero? ||
  184 + (other.problem_count <=> problem_count).nonzero? ||
  185 + name <=> other.name
  186 + end
  187 +
180 protected 188 protected
181 189
182 def store_cached_attributes_on_problems 190 def store_cached_attributes_on_problems
spec/models/app_spec.rb
@@ -23,6 +23,31 @@ describe App do @@ -23,6 +23,31 @@ describe App do
23 end 23 end
24 end 24 end
25 25
  26 + describe '<=>' do
  27 + it 'is compared by unresolved count' do
  28 + app_0 = stub_model(App, :name => 'app', :unresolved_count => 1, :problem_count => 1)
  29 + app_1 = stub_model(App, :name => 'app', :unresolved_count => 0, :problem_count => 1)
  30 +
  31 + app_0.should < app_1
  32 + app_1.should > app_0
  33 + end
  34 +
  35 + it 'is compared by problem count' do
  36 + app_0 = stub_model(App, :name => 'app', :unresolved_count => 0, :problem_count => 1)
  37 + app_1 = stub_model(App, :name => 'app', :unresolved_count => 0, :problem_count => 0)
  38 +
  39 + app_0.should < app_1
  40 + app_1.should > app_0
  41 + end
  42 +
  43 + it 'is compared by name' do
  44 + app_0 = stub_model(App, :name => 'app_0', :unresolved_count => 0, :problem_count => 0)
  45 + app_1 = stub_model(App, :name => 'app_1', :unresolved_count => 0, :problem_count => 0)
  46 +
  47 + app_0.should < app_1
  48 + app_1.should > app_0
  49 + end
  50 + end
26 51
27 context 'being created' do 52 context 'being created' do
28 it 'generates a new api-key' do 53 it 'generates a new api-key' do