uses cache counter for debate's comments

This commit is contained in:
Juanjo Bazán
2015-08-26 17:17:53 +02:00
parent 5d112ac2bf
commit dacbe27fda
5 changed files with 18 additions and 5 deletions

View File

@@ -9,7 +9,7 @@ class Comment < ActiveRecord::Base
validates :body, presence: true validates :body, presence: true
validates :user, presence: true validates :user, presence: true
belongs_to :commentable, polymorphic: true belongs_to :commentable, polymorphic: true, counter_cache: true
belongs_to :user, -> { with_hidden } belongs_to :user, -> { with_hidden }
has_many :inappropiate_flags, :as => :flaggable has_many :inappropiate_flags, :as => :flaggable
@@ -88,4 +88,8 @@ class Comment < ActiveRecord::Base
children.count children.count
end end
def after_hide
commentable_type.constantize.reset_counters(commentable_id, :comment_threads)
end
end end

View File

@@ -9,7 +9,7 @@
<h3><%= link_to debate.title, debate %></h3> <h3><%= link_to debate.title, debate %></h3>
<p class="debate-info"> <p class="debate-info">
<i class="icon-comments"></i>&nbsp; <i class="icon-comments"></i>&nbsp;
<%= link_to t("debates.debate.comments", count: debate.comment_threads.count), debate_path(debate, anchor: "comments") %> <%= link_to t("debates.debate.comments", count: debate.comments_count), debate_path(debate, anchor: "comments") %>
</p> </p>
<div class="debate-description"> <div class="debate-description">
<%= link_to debate.description, debate %> <%= link_to debate.description, debate %>

View File

@@ -40,7 +40,7 @@
<%= l @debate.created_at.to_date %> <%= l @debate.created_at.to_date %>
<span class="bullet">&nbsp;&bullet;&nbsp;</span> <span class="bullet">&nbsp;&bullet;&nbsp;</span>
<i class="icon-comments"></i>&nbsp; <i class="icon-comments"></i>&nbsp;
<%= link_to t("debates.show.comments", count: @debate.comment_threads.count), "#comments" %> <%= link_to t("debates.show.comments", count: @debate.comments_count), "#comments" %>
<span class="bullet">&nbsp;&bullet;&nbsp;</span> <span class="bullet">&nbsp;&bullet;&nbsp;</span>
<span class="js-flag-as-inappropiate-actions"> <span class="js-flag-as-inappropiate-actions">
<%= render 'debates/flag_as_inappropiate_actions', debate: @debate %> <%= render 'debates/flag_as_inappropiate_actions', debate: @debate %>
@@ -79,7 +79,7 @@
<h2> <h2>
<%= t("debates.show.comments_title") %> <%= t("debates.show.comments_title") %>
<span>(<%= @debate.comment_threads.count %>)</span> <span>(<%= @debate.comments_count %>)</span>
</h2> </h2>
<% if user_signed_in? %> <% if user_signed_in? %>
<%= render 'comments/form', {parent: @debate, toggeable: false} %> <%= render 'comments/form', {parent: @debate, toggeable: false} %>

View File

@@ -8,7 +8,7 @@
<h3><%= link_to featured_debate.title, featured_debate %></h3> <h3><%= link_to featured_debate.title, featured_debate %></h3>
<p class="debate-info"> <p class="debate-info">
<i class="icon-comments"></i>&nbsp; <i class="icon-comments"></i>&nbsp;
<%= link_to t("debates.show.comments", count: featured_debate.comment_threads.count), debate_path(featured_debate, anchor: "comments") %> <%= link_to t("debates.show.comments", count: featured_debate.comments_count), debate_path(featured_debate, anchor: "comments") %>
</p> </p>
<div class="debate-description"> <div class="debate-description">
<%= link_to featured_debate.description, featured_debate %> <%= link_to featured_debate.description, featured_debate %>

View File

@@ -8,6 +8,15 @@ describe Comment do
expect(comment).to be_valid expect(comment).to be_valid
end end
it "should update cache_counter in debate after hide" do
debate = create(:debate)
comment = create(:comment, commentable: debate)
expect(debate.reload.comments_count).to eq(1)
comment.hide
expect(debate.reload.comments_count).to eq(0)
end
describe "#children_count" do describe "#children_count" do
let(:comment) { create(:comment) } let(:comment) { create(:comment) }
let(:debate) { comment.debate } let(:debate) { comment.debate }