diff --git a/app/models/comment.rb b/app/models/comment.rb index 649de1d..e9dd5a6 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -2,12 +2,25 @@ class Comment include Mongoid::Document include Mongoid::Timestamps + after_create :increase_counter_cache + before_destroy :decrease_counter_cache + field :body, :type => String index :user_id - belongs_to :err + belongs_to :err, :class_name => "Problem" belongs_to :user validates_presence_of :body + + protected + def increase_counter_cache + err.inc(:comments_count, 1) + end + + def decrease_counter_cache + err.inc(:comments_count, -1) if err + end + end diff --git a/app/models/problem.rb b/app/models/problem.rb index fb4760a..b3fb752 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -21,6 +21,7 @@ class Problem field :user_agents, :type => Array, :default => [] field :messages, :type => Array, :default => [] field :hosts, :type => Array, :default => [] + field :comments_count, :type => Integer, :default => 0 index :app_id index :app_name diff --git a/app/views/errs/_table.html.haml b/app/views/errs/_table.html.haml index d6dbb55..51095ad 100644 --- a/app/views/errs/_table.html.haml +++ b/app/views/errs/_table.html.haml @@ -26,7 +26,7 @@ %td.message = link_to trucated_err_message(problem), app_err_path(problem.app, problem) %em= problem.where - - if problem.comments.any? + - if problem.comments_count > 0 - comment = problem.comments.last %br .inline_comment diff --git a/db/migrate/20111019163257_add_problem_comments_count.rb b/db/migrate/20111019163257_add_problem_comments_count.rb new file mode 100644 index 0000000..c9f724b --- /dev/null +++ b/db/migrate/20111019163257_add_problem_comments_count.rb @@ -0,0 +1,10 @@ +class AddProblemCommentsCount < Mongoid::Migration + def self.up + Problem.all.each do |problem| + problem.update_attributes(:comments_count => problem.comments.count) + end + end + + def self.down + end +end \ No newline at end of file diff --git a/spec/factories/comment_factories.rb b/spec/factories/comment_factories.rb index 1bee372..23e27f4 100644 --- a/spec/factories/comment_factories.rb +++ b/spec/factories/comment_factories.rb @@ -1,5 +1,6 @@ Factory.define :comment do |c| c.user {|u| u.association :user} c.body 'Test comment' + c.err {|e| e.association :problem} end diff --git a/spec/models/problem_spec.rb b/spec/models/problem_spec.rb index e26b6c8..096d39f 100644 --- a/spec/models/problem_spec.rb +++ b/spec/models/problem_spec.rb @@ -276,5 +276,31 @@ describe Problem do end end + context "comment counter cache" do + before do + @app = Factory(:app) + @problem = Factory(:problem, :app => @app) + end + + it "#comments_count returns 0 by default" do + @problem.comments_count.should == 0 + end + + it "adding a comment increases #comments_count by 1" do + lambda { + Factory(:comment, :err => @problem) + }.should change(@problem, :comments_count).from(0).to(1) + end + + it "removing a comment decreases #comments_count by 1" do + comment1 = Factory(:comment, :err => @problem) + lambda { + @problem.reload.comments.first.destroy + @problem.reload + }.should change(@problem, :comments_count).from(1).to(0) + end + end + + end -- libgit2 0.21.2