Commit 02696d83c7751d787675ae9737e997c19ed7067a
1 parent
86f245d1
Exists in
master
and in
1 other branch
fix error when only one app is present
It fixes ActionView::Template::Error (undefined method `>' for nil:NilClass) error. This error was showing when there is only one app was present. In AppsController when we were trying to sort our apps and if apps.count == 1 the sort method actually doesn't call block so we had empty @unresolved_counts, @problem_counts assigns.
Showing
2 changed files
with
30 additions
and
3 deletions
Show diff stats
app/controllers/apps_controller.rb
| @@ -50,10 +50,26 @@ class AppsController < InheritedResources::Base | @@ -50,10 +50,26 @@ class AppsController < InheritedResources::Base | ||
| 50 | 50 | ||
| 51 | protected | 51 | protected |
| 52 | def collection | 52 | def collection |
| 53 | - # Sort apps by number of unresolved errs, descending. | ||
| 54 | - # Caches the unresolved err counts while performing the sort. | ||
| 55 | @unresolved_counts, @problem_counts = {}, {} | 53 | @unresolved_counts, @problem_counts = {}, {} |
| 56 | - @apps ||= end_of_association_chain.all.sort{|a,b| | 54 | + @apps ||= begin |
| 55 | + apps = end_of_association_chain.all | ||
| 56 | + | ||
| 57 | + if apps.count > 1 | ||
| 58 | + apps = sort_by_unresolved_errs(apps) | ||
| 59 | + elsif apps.count > 0 | ||
| 60 | + app = apps.first | ||
| 61 | + @unresolved_counts[app.id] ||= app.problems.unresolved.count | ||
| 62 | + @problem_counts[app.id] ||= app.problems.count | ||
| 63 | + end | ||
| 64 | + | ||
| 65 | + apps | ||
| 66 | + end | ||
| 67 | + end | ||
| 68 | + | ||
| 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| | ||
| 57 | [a,b].each do |app| | 73 | [a,b].each do |app| |
| 58 | @unresolved_counts[app.id] ||= app.problems.unresolved.count | 74 | @unresolved_counts[app.id] ||= app.problems.unresolved.count |
| 59 | @problem_counts[app.id] ||= app.problems.count | 75 | @problem_counts[app.id] ||= app.problems.count |
spec/controllers/apps_controller_spec.rb
| @@ -31,6 +31,17 @@ describe AppsController do | @@ -31,6 +31,17 @@ describe AppsController do | ||
| 31 | assigns(:apps).should_not include(unwatched_app) | 31 | assigns(:apps).should_not include(unwatched_app) |
| 32 | end | 32 | end |
| 33 | end | 33 | end |
| 34 | + | ||
| 35 | + context 'when there is only one app' do | ||
| 36 | + it 'sets unresolved_counts and problem_counts variables' do | ||
| 37 | + sign_in Fabricate(:admin) | ||
| 38 | + app = Fabricate(:app) | ||
| 39 | + get :index | ||
| 40 | + | ||
| 41 | + assigns(:unresolved_counts).should == {app.id => 0} | ||
| 42 | + assigns(:problem_counts).should == {app.id => 0} | ||
| 43 | + end | ||
| 44 | + end | ||
| 34 | end | 45 | end |
| 35 | 46 | ||
| 36 | describe "GET /apps/:id" do | 47 | describe "GET /apps/:id" do |