Commit a96253929e817e48366a4d48d7e0a8b988d774e2
1 parent
c00f1ebe
Exists in
master
and in
1 other branch
use methods with memoization instead of temporary instance vars
Showing
5 changed files
with
13 additions
and
25 deletions
Show diff stats
app/controllers/apps_controller.rb
| ... | ... | @@ -52,20 +52,13 @@ class AppsController < InheritedResources::Base |
| 52 | 52 | |
| 53 | 53 | protected |
| 54 | 54 | def collection |
| 55 | - @unresolved_counts, @problem_counts = {}, {} | |
| 56 | 55 | @apps ||= begin |
| 57 | 56 | apps = end_of_association_chain.all |
| 58 | 57 | |
| 59 | - # Cache counts for unresolved errs and problems | |
| 60 | - apps.each do |app| | |
| 61 | - @unresolved_counts[app.id] ||= app.problems.unresolved.count | |
| 62 | - @problem_counts[app.id] ||= app.problems.count | |
| 63 | - end | |
| 64 | - | |
| 65 | 58 | # Sort apps by number of unresolved errs, then problem counts. |
| 66 | - apps.sort do |a,b| | |
| 67 | - (@unresolved_counts[b.id] <=> @unresolved_counts[a.id]).nonzero? || | |
| 68 | - (@problem_counts[b.id] <=> @problem_counts[a.id]).nonzero? || | |
| 59 | + apps.sort do |a, b| | |
| 60 | + (b.unresolved_count <=> a.unresolved_count).nonzero? || | |
| 61 | + (b.problem_count <=> a.problem_count).nonzero? || | |
| 69 | 62 | a.name <=> b.name |
| 70 | 63 | end |
| 71 | 64 | end | ... | ... |
app/models/app.rb
| ... | ... | @@ -169,6 +169,14 @@ class App |
| 169 | 169 | end |
| 170 | 170 | end |
| 171 | 171 | |
| 172 | + def unresolved_count | |
| 173 | + @unresolved_count ||= problems.unresolved.count | |
| 174 | + end | |
| 175 | + | |
| 176 | + def problem_count | |
| 177 | + @problem_count ||= problems.count | |
| 178 | + end | |
| 179 | + | |
| 172 | 180 | protected |
| 173 | 181 | |
| 174 | 182 | def store_cached_attributes_on_problems | ... | ... |
app/views/apps/index.html.haml
| ... | ... | @@ -47,8 +47,8 @@ |
| 47 | 47 | - revision = app.deploys.last.short_revision |
| 48 | 48 | = link_to( app.last_deploy_at.to_s(:micro) << (revision.present? ? " (#{revision})" : ""), app_deploys_path(app)) |
| 49 | 49 | %td.count |
| 50 | - - if @problem_counts[app.id] > 0 | |
| 51 | - - unresolved = @unresolved_counts[app.id] | |
| 50 | + - if app.problem_count > 0 | |
| 51 | + - unresolved = app.unresolved_count | |
| 52 | 52 | = link_to unresolved, app_path(app), :class => (unresolved == 0 ? "resolved" : nil) |
| 53 | 53 | - if @apps.none? |
| 54 | 54 | %tr | ... | ... |
spec/controllers/apps_controller_spec.rb
| ... | ... | @@ -31,17 +31,6 @@ describe AppsController do |
| 31 | 31 | assigns(:apps).should_not include(unwatched_app) |
| 32 | 32 | end |
| 33 | 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 | |
| 45 | 34 | end |
| 46 | 35 | |
| 47 | 36 | describe "GET /apps/:id" do | ... | ... |
spec/views/apps/index.html.haml_spec.rb
| ... | ... | @@ -4,8 +4,6 @@ describe "apps/index.html.haml" do |
| 4 | 4 | before do |
| 5 | 5 | app = stub_model(App, :deploys => [stub_model(Deploy, :created_at => Time.now, :revision => "123456789abcdef")]) |
| 6 | 6 | assign :apps, [app] |
| 7 | - assign :problem_counts, {app.id => 0} | |
| 8 | - assign :unresolved_counts, {app.id => 0} | |
| 9 | 7 | controller.stub(:current_user) { stub_model(User) } |
| 10 | 8 | end |
| 11 | 9 | ... | ... |