Commit b36c90e33030f30d889eb37d0234d116844297fc
1 parent
695de2bb
Exists in
master
and in
1 other branch
Problem#comments counter cache
Showing
6 changed files
with
53 additions
and
2 deletions
Show diff stats
app/models/comment.rb
@@ -2,12 +2,25 @@ class Comment | @@ -2,12 +2,25 @@ class Comment | ||
2 | include Mongoid::Document | 2 | include Mongoid::Document |
3 | include Mongoid::Timestamps | 3 | include Mongoid::Timestamps |
4 | 4 | ||
5 | + after_create :increase_counter_cache | ||
6 | + before_destroy :decrease_counter_cache | ||
7 | + | ||
5 | field :body, :type => String | 8 | field :body, :type => String |
6 | index :user_id | 9 | index :user_id |
7 | 10 | ||
8 | - belongs_to :err | 11 | + belongs_to :err, :class_name => "Problem" |
9 | belongs_to :user | 12 | belongs_to :user |
10 | 13 | ||
11 | validates_presence_of :body | 14 | validates_presence_of :body |
15 | + | ||
16 | + protected | ||
17 | + def increase_counter_cache | ||
18 | + err.inc(:comments_count, 1) | ||
19 | + end | ||
20 | + | ||
21 | + def decrease_counter_cache | ||
22 | + err.inc(:comments_count, -1) if err | ||
23 | + end | ||
24 | + | ||
12 | end | 25 | end |
13 | 26 |
app/models/problem.rb
@@ -21,6 +21,7 @@ class Problem | @@ -21,6 +21,7 @@ class Problem | ||
21 | field :user_agents, :type => Array, :default => [] | 21 | field :user_agents, :type => Array, :default => [] |
22 | field :messages, :type => Array, :default => [] | 22 | field :messages, :type => Array, :default => [] |
23 | field :hosts, :type => Array, :default => [] | 23 | field :hosts, :type => Array, :default => [] |
24 | + field :comments_count, :type => Integer, :default => 0 | ||
24 | 25 | ||
25 | index :app_id | 26 | index :app_id |
26 | index :app_name | 27 | index :app_name |
app/views/errs/_table.html.haml
@@ -26,7 +26,7 @@ | @@ -26,7 +26,7 @@ | ||
26 | %td.message | 26 | %td.message |
27 | = link_to trucated_err_message(problem), app_err_path(problem.app, problem) | 27 | = link_to trucated_err_message(problem), app_err_path(problem.app, problem) |
28 | %em= problem.where | 28 | %em= problem.where |
29 | - - if problem.comments.any? | 29 | + - if problem.comments_count > 0 |
30 | - comment = problem.comments.last | 30 | - comment = problem.comments.last |
31 | %br | 31 | %br |
32 | .inline_comment | 32 | .inline_comment |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +class AddProblemCommentsCount < Mongoid::Migration | ||
2 | + def self.up | ||
3 | + Problem.all.each do |problem| | ||
4 | + problem.update_attributes(:comments_count => problem.comments.count) | ||
5 | + end | ||
6 | + end | ||
7 | + | ||
8 | + def self.down | ||
9 | + end | ||
10 | +end | ||
0 | \ No newline at end of file | 11 | \ No newline at end of file |
spec/factories/comment_factories.rb
spec/models/problem_spec.rb
@@ -276,5 +276,31 @@ describe Problem do | @@ -276,5 +276,31 @@ describe Problem do | ||
276 | end | 276 | end |
277 | end | 277 | end |
278 | 278 | ||
279 | + context "comment counter cache" do | ||
280 | + before do | ||
281 | + @app = Factory(:app) | ||
282 | + @problem = Factory(:problem, :app => @app) | ||
283 | + end | ||
284 | + | ||
285 | + it "#comments_count returns 0 by default" do | ||
286 | + @problem.comments_count.should == 0 | ||
287 | + end | ||
288 | + | ||
289 | + it "adding a comment increases #comments_count by 1" do | ||
290 | + lambda { | ||
291 | + Factory(:comment, :err => @problem) | ||
292 | + }.should change(@problem, :comments_count).from(0).to(1) | ||
293 | + end | ||
294 | + | ||
295 | + it "removing a comment decreases #comments_count by 1" do | ||
296 | + comment1 = Factory(:comment, :err => @problem) | ||
297 | + lambda { | ||
298 | + @problem.reload.comments.first.destroy | ||
299 | + @problem.reload | ||
300 | + }.should change(@problem, :comments_count).from(1).to(0) | ||
301 | + end | ||
302 | + end | ||
303 | + | ||
304 | + | ||
279 | end | 305 | end |
280 | 306 |