diff --git a/app/models/comment.rb b/app/models/comment.rb index db8fb35fc..bbfeca4f5 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index 4dd866df2..2d1e2deb4 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -9,7 +9,7 @@

<%= link_to debate.title, debate %>

  - <%= 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") %>

<%= link_to debate.description, debate %> diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 3b91edc8d..af25f28b1 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -40,7 +40,7 @@ <%= l @debate.created_at.to_date %>  •    - <%= link_to t("debates.show.comments", count: @debate.comment_threads.count), "#comments" %> + <%= link_to t("debates.show.comments", count: @debate.comments_count), "#comments" %>  •  <%= render 'debates/flag_as_inappropiate_actions', debate: @debate %> @@ -79,7 +79,7 @@

<%= t("debates.show.comments_title") %> - (<%= @debate.comment_threads.count %>) + (<%= @debate.comments_count %>)

<% if user_signed_in? %> <%= render 'comments/form', {parent: @debate, toggeable: false} %> diff --git a/app/views/welcome/_featured_debate.html.erb b/app/views/welcome/_featured_debate.html.erb index 11cb3b733..7ad53608b 100644 --- a/app/views/welcome/_featured_debate.html.erb +++ b/app/views/welcome/_featured_debate.html.erb @@ -8,7 +8,7 @@

<%= link_to featured_debate.title, featured_debate %>

  - <%= 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") %>

<%= link_to featured_debate.description, featured_debate %> diff --git a/db/migrate/20150826112500_add_comments_count_to_debate.rb b/db/migrate/20150826112500_add_comments_count_to_debate.rb new file mode 100644 index 000000000..bf416b669 --- /dev/null +++ b/db/migrate/20150826112500_add_comments_count_to_debate.rb @@ -0,0 +1,5 @@ +class AddCommentsCountToDebate < ActiveRecord::Migration + def change + add_column :debates, :comments_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 783e3dbea..ad2a412ff 100644 --- a/db/schema.rb +++ b/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 \ No newline at end of file diff --git a/lib/acts_as_paranoid_aliases.rb b/lib/acts_as_paranoid_aliases.rb index dbb0d8350..b4faa19b9 100644 --- a/lib/acts_as_paranoid_aliases.rb +++ b/lib/acts_as_paranoid_aliases.rb @@ -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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 98420c000..4115fedcf 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -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 }