Merge pull request #253 from AyuntamientoMadrid/counter-cache-comments
Counter cache column for Debate's comments
This commit is contained in:
@@ -9,7 +9,7 @@ class Comment < ActiveRecord::Base
|
||||
validates :body, presence: true
|
||||
validates :user, presence: true
|
||||
|
||||
belongs_to :commentable, polymorphic: true
|
||||
belongs_to :commentable, polymorphic: true, counter_cache: true
|
||||
belongs_to :user, -> { with_hidden }
|
||||
|
||||
has_many :inappropiate_flags, :as => :flaggable
|
||||
@@ -89,4 +89,8 @@ class Comment < ActiveRecord::Base
|
||||
children.count
|
||||
end
|
||||
|
||||
def after_hide
|
||||
commentable_type.constantize.reset_counters(commentable_id, :comment_threads)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<h3><%= link_to debate.title, debate %></h3>
|
||||
<p class="debate-info">
|
||||
<i class="icon-comments"></i>
|
||||
<%= 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>
|
||||
<div class="debate-description">
|
||||
<%= link_to debate.description, debate %>
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
<%= l @debate.created_at.to_date %>
|
||||
<span class="bullet"> • </span>
|
||||
<i class="icon-comments"></i>
|
||||
<%= 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"> • </span>
|
||||
<span class="js-flag-as-inappropiate-actions">
|
||||
<%= render 'debates/flag_as_inappropiate_actions', debate: @debate %>
|
||||
@@ -79,7 +79,7 @@
|
||||
|
||||
<h2>
|
||||
<%= t("debates.show.comments_title") %>
|
||||
<span>(<%= @debate.comment_threads.count %>)</span>
|
||||
<span>(<%= @debate.comments_count %>)</span>
|
||||
</h2>
|
||||
<% if user_signed_in? %>
|
||||
<%= render 'comments/form', {parent: @debate, toggeable: false} %>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<h3><%= link_to featured_debate.title, featured_debate %></h3>
|
||||
<p class="debate-info">
|
||||
<i class="icon-comments"></i>
|
||||
<%= 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>
|
||||
<div class="debate-description">
|
||||
<%= link_to featured_debate.description, featured_debate %>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddCommentsCountToDebate < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :debates, :comments_count, :integer, default: 0
|
||||
end
|
||||
end
|
||||
15
db/schema.rb
15
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20150826112411) do
|
||||
ActiveRecord::Schema.define(version: 20150826112500) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -41,7 +41,7 @@ ActiveRecord::Schema.define(version: 20150826112411) do
|
||||
t.string "title"
|
||||
t.text "body"
|
||||
t.string "subject"
|
||||
t.integer "user_id", null: false
|
||||
t.integer "user_id", null: false
|
||||
t.integer "parent_id"
|
||||
t.integer "lft"
|
||||
t.integer "rgt"
|
||||
@@ -70,16 +70,17 @@ ActiveRecord::Schema.define(version: 20150826112411) do
|
||||
t.string "title", limit: 80
|
||||
t.text "description"
|
||||
t.integer "author_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "visit_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "hidden_at"
|
||||
t.string "visit_id"
|
||||
t.datetime "flagged_as_inappropiate_at"
|
||||
t.integer "inappropiate_flags_count", default: 0
|
||||
t.integer "cached_votes_total", default: 0
|
||||
t.integer "cached_votes_up", default: 0
|
||||
t.integer "cached_votes_down", default: 0
|
||||
t.datetime "archived_at"
|
||||
t.integer "comments_count", default: 0
|
||||
end
|
||||
|
||||
add_index "debates", ["cached_votes_down"], name: "index_debates_on_cached_votes_down", using: :btree
|
||||
@@ -178,10 +179,10 @@ ActiveRecord::Schema.define(version: 20150826112411) do
|
||||
t.string "unconfirmed_email"
|
||||
t.boolean "email_on_debate_comment", default: false
|
||||
t.boolean "email_on_comment_reply", default: false
|
||||
t.string "phone_number", limit: 30
|
||||
t.string "official_position"
|
||||
t.integer "official_level", default: 0
|
||||
t.datetime "hidden_at"
|
||||
t.string "phone_number", limit: 30
|
||||
t.string "username"
|
||||
end
|
||||
|
||||
@@ -240,4 +241,4 @@ ActiveRecord::Schema.define(version: 20150826112411) do
|
||||
add_foreign_key "inappropiate_flags", "users"
|
||||
add_foreign_key "moderators", "users"
|
||||
add_foreign_key "organizations", "users"
|
||||
end
|
||||
end
|
||||
@@ -5,11 +5,15 @@ module ActsAsParanoidAliases
|
||||
|
||||
def hide
|
||||
update_attribute(:hidden_at, Time.now)
|
||||
after_hide
|
||||
end
|
||||
|
||||
def hidden?
|
||||
deleted?
|
||||
end
|
||||
|
||||
def after_hide
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
||||
@@ -8,6 +8,15 @@ describe Comment do
|
||||
expect(comment).to be_valid
|
||||
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
|
||||
let(:comment) { create(:comment) }
|
||||
let(:debate) { comment.debate }
|
||||
|
||||
Reference in New Issue
Block a user