From df5ef2dc29e0bf8fe5c03b69a745dd9e06975c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 2 Sep 2015 17:06:11 +0200 Subject: [PATCH 01/10] makes helper independent of commentable class --- app/helpers/comments_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 5c41b3432..b6d066935 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -1,11 +1,11 @@ module CommentsHelper def comment_link_text(parent) - parent.class == Debate ? t("comments_helper.comment_link") : t("comments_helper.reply_link") + parent.is_a? Comment ? t("comments_helper.reply_link") : t("comments_helper.comment_link") end def comment_button_text(parent) - parent.class == Debate ? t("comments_helper.comment_button") : t("comments_helper.reply_button") + parent.is_a? Comment ? t("comments_helper.reply_button") : t("comments_helper.comment_button") end end \ No newline at end of file From eb689d7c7e39b4b3ceed5b2b83a160f409bbee0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Wed, 2 Sep 2015 17:19:32 +0200 Subject: [PATCH 02/10] refactors comment form --- app/helpers/comments_helper.rb | 8 ++++---- app/views/comments/_comment.html.erb | 4 ++-- app/views/comments/_form.html.erb | 27 ++++++++++++++------------- app/views/debates/show.html.erb | 2 +- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index b6d066935..14dabf49c 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -1,11 +1,11 @@ module CommentsHelper - def comment_link_text(parent) - parent.is_a? Comment ? t("comments_helper.reply_link") : t("comments_helper.comment_link") + def comment_link_text(parent_id) + parent_id.present? ? t("comments_helper.reply_link") : t("comments_helper.comment_link") end - def comment_button_text(parent) - parent.is_a? Comment ? t("comments_helper.reply_button") : t("comments_helper.comment_button") + def comment_button_text(parent_id) + parent_id.present? ? t("comments_helper.reply_button") : t("comments_helper.comment_button") end end \ No newline at end of file diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index e21b148bc..e02cb4c48 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -88,14 +88,14 @@ <%= render 'comments/actions', comment: comment %> - <%= render 'comments/form', {parent: comment, toggeable: true} %> + <%= render 'comments/form', {commentable: @debate, parent_id: comment.id, toggeable: true} %> <% end %> <% end %>
- <%= render comment.children.for_render.reorder('id DESC, lft') %> + <%= render comment.children.for_render.reorder('id DESC') %>
diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 71735eaaa..70e600df0 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,22 +1,23 @@ -
> - <%= form_for [@debate, Comment.new], remote: true do |f| %> - <%= label_tag "comment-body-#{dom_id(parent)}", t("comments.form.leave_comment") %> - <%= f.text_area :body, id: "comment-body-#{dom_id(parent)}", label: false %> - <%= f.hidden_field :commentable_type, value: parent.class %> - <%= f.hidden_field :commentable_id, value: parent.id %> +
> + <%= form_for [commentable, Comment.new], remote: true do |f| %> + <%= label_tag "comment-body-#{dom_id(commentable)}", t("comments.form.leave_comment") %> + <%= f.text_area :body, id: "comment-body-#{dom_id(commentable)}", label: false %> + <%= f.hidden_field :commentable_type, value: commentable.class %> + <%= f.hidden_field :commentable_id, value: commentable.id %> + <%= f.hidden_field :parent_id, value: parent_id %> - <%= f.submit comment_button_text(parent), class: "button radius small inline-block" %> + <%= f.submit comment_button_text(parent_id), class: "button radius small inline-block" %> - <% if can? :comment_as_moderator, @debate %> + <% if can? :comment_as_moderator, commentable %>º
- <%= f.check_box :as_moderator, id: "comment-as-moderator-#{dom_id(parent)}", label: false %> - <%= label_tag "comment-as-moderator-#{dom_id(parent)}", t("comments.form.comment_as_moderator"), class: "checkbox" %> + <%= f.check_box :as_moderator, id: "comment-as-moderator-#{dom_id(commentable)}", label: false %> + <%= label_tag "comment-as-moderator-#{dom_id(commentable)}", t("comments.form.comment_as_moderator"), class: "checkbox" %>
<% end %> - <% if can? :comment_as_administrator, @debate %> + <% if can? :comment_as_administrator, commentable %>
- <%= f.check_box :as_administrator, id: "comment-as-administrator-#{dom_id(parent)}",label: false %> - <%= label_tag "comment-as-administrator-#{dom_id(parent)}", t("comments.form.comment_as_admin"), class: "checkbox" %> + <%= f.check_box :as_administrator, id: "comment-as-administrator-#{dom_id(commentable)}",label: false %> + <%= label_tag "comment-as-administrator-#{dom_id(commentable)}", t("comments.form.comment_as_admin"), class: "checkbox" %>
<% end %> <% end %> diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 8fab71128..d5bce5bef 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -82,7 +82,7 @@ (<%= @debate.comments_count %>) <% if user_signed_in? %> - <%= render 'comments/form', {parent: @debate, toggeable: false} %> + <%= render 'comments/form', {commentable: @debate, parent_id: nil, toggeable: false} %> <% else %>
From e0c67d8404a91b8a292055e6ffaba1a2359f0801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Wed, 2 Sep 2015 17:24:14 +0200 Subject: [PATCH 03/10] adds ancestry column to comments --- db/migrate/20150902114558_add_ancestry_to_comments.rb | 7 +++++++ db/schema.rb | 10 ++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20150902114558_add_ancestry_to_comments.rb diff --git a/db/migrate/20150902114558_add_ancestry_to_comments.rb b/db/migrate/20150902114558_add_ancestry_to_comments.rb new file mode 100644 index 000000000..7037c7e68 --- /dev/null +++ b/db/migrate/20150902114558_add_ancestry_to_comments.rb @@ -0,0 +1,7 @@ +class AddAncestryToComments < ActiveRecord::Migration + def change + add_column :comments, :ancestry, :string + + add_index :comments, :ancestry + end +end diff --git a/db/schema.rb b/db/schema.rb index 883d2a7e1..8b8dd88e9 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: 20150830212600) do +ActiveRecord::Schema.define(version: 20150902114558) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -62,9 +62,6 @@ ActiveRecord::Schema.define(version: 20150830212600) do t.text "body" t.string "subject" t.integer "user_id", null: false - t.integer "parent_id" - t.integer "lft" - t.integer "rgt" t.datetime "created_at" t.datetime "updated_at" t.integer "children_count", default: 0 @@ -77,8 +74,13 @@ ActiveRecord::Schema.define(version: 20150830212600) do t.integer "cached_votes_up", default: 0 t.integer "cached_votes_down", default: 0 t.datetime "confirmed_hide_at" + t.string "ancestry" + t.integer "rgt" + t.integer "lft" + t.integer "parent_id" end + add_index "comments", ["ancestry"], name: "index_comments_on_ancestry", using: :btree add_index "comments", ["cached_votes_down"], name: "index_comments_on_cached_votes_down", using: :btree add_index "comments", ["cached_votes_total"], name: "index_comments_on_cached_votes_total", using: :btree add_index "comments", ["cached_votes_up"], name: "index_comments_on_cached_votes_up", using: :btree From 3eae5dff1f8e5abf86df05b04fe1f3fcc10c3a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 2 Sep 2015 19:21:15 +0200 Subject: [PATCH 04/10] adds ancestry --- Gemfile | 2 +- Gemfile.lock | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 70a599bf6..a986bf6e2 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ gem 'omniauth-facebook' gem 'omniauth-google-oauth2' gem 'kaminari' -gem 'acts_as_commentable_with_threading' +gem 'ancestry' gem 'acts-as-taggable-on' gem "responders" gem 'foundation-rails' diff --git a/Gemfile.lock b/Gemfile.lock index ca0eb3d31..26b825c0d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -38,10 +38,6 @@ GEM tzinfo (~> 1.1) acts-as-taggable-on (3.5.0) activerecord (>= 3.2, < 5) - acts_as_commentable_with_threading (2.0.0) - activerecord (>= 4.0) - activesupport (>= 4.0) - awesome_nested_set (>= 3.0) acts_as_votable (0.10.0) addressable (2.3.8) ahoy_matey (1.2.1) @@ -57,9 +53,9 @@ GEM akami (1.3.1) gyoku (>= 0.4.0) nokogiri + ancestry (2.1.0) + activerecord (>= 3.0.0) arel (6.0.3) - awesome_nested_set (3.0.2) - activerecord (>= 4.0.0, < 5) bcrypt (3.1.10) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) @@ -392,9 +388,9 @@ PLATFORMS DEPENDENCIES acts-as-taggable-on - acts_as_commentable_with_threading acts_as_votable ahoy_matey (~> 1.2.1) + ancestry bullet byebug cancancan From dee2d0196bea891903bef0893aab2d430234713e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 2 Sep 2015 19:23:09 +0200 Subject: [PATCH 05/10] ancestries moves to ancestry from acts_as_commentable_with_threading --- app/controllers/comments_controller.rb | 24 +++++++----------------- app/controllers/debates_controller.rb | 5 +++-- app/helpers/comments_helper.rb | 4 ++++ app/models/comment.rb | 19 +++++++++++++------ app/models/debate.rb | 2 +- app/views/comments/_comment.html.erb | 8 ++++---- app/views/comments/_form.html.erb | 17 +++++++++-------- app/views/comments/create.js.erb | 9 +++++---- app/views/comments/new.js.erb | 5 ++++- db/schema.rb | 5 +---- spec/features/comments_spec.rb | 3 +-- spec/models/comment_spec.rb | 10 ++++------ spec/support/common_actions.rb | 5 +++-- 13 files changed, 59 insertions(+), 57 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 076b33fce..f07507600 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,15 +1,13 @@ class CommentsController < ApplicationController before_action :authenticate_user! + before_action :load_commentable, only: :create before_action :build_comment, only: :create - before_action :parent, only: :create load_and_authorize_resource respond_to :html, :js def create if @comment.save - @comment.move_to_child_of(parent) if reply? - Mailer.comment(@comment).deliver_now if email_on_debate_comment? Mailer.reply(@comment).deliver_now if email_on_comment_reply? else @@ -37,11 +35,11 @@ class CommentsController < ApplicationController private def comment_params - params.require(:comment).permit(:commentable_type, :commentable_id, :body, :as_moderator, :as_administrator) + params.require(:comment).permit(:commentable_type, :commentable_id, :parent_id, :body, :as_moderator, :as_administrator) end def build_comment - @comment = Comment.build(debate, current_user, comment_params[:body]) + @comment = Comment.build(@commentable, current_user, comment_params[:body], comment_params[:parent_id].presence) check_for_special_comments end @@ -53,24 +51,16 @@ class CommentsController < ApplicationController end end - def debate - @debate ||= Debate.find(params[:debate_id]) - end - - def parent - @parent ||= Comment.find_parent(comment_params) - end - - def reply? - parent.class == Comment + def load_commentable + @commentable = Comment.find_commentable(comment_params[:commentable_type], comment_params[:commentable_id]) end def email_on_debate_comment? - @comment.debate.author.email_on_debate_comment? + @comment.commentable.author.email_on_debate_comment? end def email_on_comment_reply? - reply? && parent.author.email_on_comment_reply? + @comment.reply? && @comment.parent.author.email_on_comment_reply? end def administrator_comment? diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 451d8e893..865eadced 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -14,9 +14,10 @@ class DebatesController < ApplicationController def show set_debate_votes(@debate) - @comments = @debate.root_comments.recent.page(params[:page]).for_render + @commentable = @debate + @comments = @debate.comments.roots.recent.page(params[:page]).for_render # TODO limit this list to the paginated root comment's children once we have ancestry - all_visible_comments = @debate.comment_threads + all_visible_comments = @debate.comments set_comment_flags(all_visible_comments) end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 14dabf49c..300a07921 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -8,4 +8,8 @@ module CommentsHelper parent_id.present? ? t("comments_helper.reply_button") : t("comments_helper.comment_button") end + def parent_or_commentable_dom_id(parent_id, commentable) + parent_id.blank? ? dom_id(commentable) : "comment_#{parent_id}" + end + end \ No newline at end of file diff --git a/app/models/comment.rb b/app/models/comment.rb index 77cbd166e..fe57ee95f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,13 +1,15 @@ class Comment < ActiveRecord::Base - acts_as_nested_set scope: [:commentable_id, :commentable_type], counter_cache: :children_count + #acts_as_nested_set scope: [:commentable_id, :commentable_type], counter_cache: :children_count acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases acts_as_votable + has_ancestry attr_accessor :as_moderator, :as_administrator validates :body, presence: true validates :user, presence: true + validates_inclusion_of :commentable_type, in: ["Debate"] belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true belongs_to :user, -> { with_hidden } @@ -23,14 +25,15 @@ class Comment < ActiveRecord::Base scope :for_render, -> { with_hidden.includes(user: :organization) } - def self.build(commentable, user, body) + def self.build(commentable, user, body, p_id=nil) new commentable: commentable, user_id: user.id, - body: body + body: body, + parent_id: p_id end - def self.find_parent(params) - params[:commentable_type].constantize.find(params[:commentable_id]) + def self.find_commentable(c_type, c_id) + c_type.constantize.find(c_id) end def debate @@ -86,7 +89,11 @@ class Comment < ActiveRecord::Base end def after_hide - commentable_type.constantize.reset_counters(commentable_id, :comment_threads) + commentable_type.constantize.reset_counters(commentable_id, :comments) + end + + def reply? + !root? end end diff --git a/app/models/debate.rb b/app/models/debate.rb index 9c96c812c..6f0543caf 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -6,13 +6,13 @@ class Debate < ActiveRecord::Base TITLE_LENGTH = Debate.columns.find { |c| c.name == 'title' }.limit acts_as_votable - acts_as_commentable acts_as_taggable acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' has_many :flags, :as => :flaggable + has_many :comments, as: :commentable validates :title, presence: true validates :description, presence: true diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index e02cb4c48..ba9e99151 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -49,7 +49,7 @@ <%= t("shared.collective") %> <% end %> - <% if comment.user_id == @debate.author_id %> + <% if comment.user_id == @commentable.author_id %>  •  <%= t("debates.comment.author") %> @@ -65,11 +65,11 @@

<%= comment.body %>

<% elsif comment.as_moderator? %>

<%= comment.body %>

- <% elsif comment.user.official? && comment.user_id == @debate.author_id %> + <% elsif comment.user.official? && comment.user_id == @commentable.author_id %>

<%= comment.body %>

<% elsif comment.user.official? %>

<%= comment.body %>

- <% elsif comment.user_id == @debate.author_id %> + <% elsif comment.user_id == @commentable.author_id %>

<%= comment.body %>

<% else %>

<%= comment.body %>

@@ -88,7 +88,7 @@ <%= render 'comments/actions', comment: comment %> - <%= render 'comments/form', {commentable: @debate, parent_id: comment.id, toggeable: true} %> + <%= render 'comments/form', {commentable: @commentable, parent_id: comment.id, toggeable: true} %> <% end %>
diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 70e600df0..9a7d26554 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,8 +1,9 @@ -
> +<% css_id = parent_or_commentable_dom_id(parent_id, commentable) %> +
> <%= form_for [commentable, Comment.new], remote: true do |f| %> - <%= label_tag "comment-body-#{dom_id(commentable)}", t("comments.form.leave_comment") %> - <%= f.text_area :body, id: "comment-body-#{dom_id(commentable)}", label: false %> - <%= f.hidden_field :commentable_type, value: commentable.class %> + <%= label_tag "comment-body-#{css_id}", t("comments.form.leave_comment") %> + <%= f.text_area :body, id: "comment-body-#{css_id}", label: false %> + <%= f.hidden_field :commentable_type, value: commentable.class.name %> <%= f.hidden_field :commentable_id, value: commentable.id %> <%= f.hidden_field :parent_id, value: parent_id %> @@ -10,14 +11,14 @@ <% if can? :comment_as_moderator, commentable %>º
- <%= f.check_box :as_moderator, id: "comment-as-moderator-#{dom_id(commentable)}", label: false %> - <%= label_tag "comment-as-moderator-#{dom_id(commentable)}", t("comments.form.comment_as_moderator"), class: "checkbox" %> + <%= f.check_box :as_moderator, id: "comment-as-moderator-#{css_id}", label: false %> + <%= label_tag "comment-as-moderator-#{css_id}", t("comments.form.comment_as_moderator"), class: "checkbox" %>
<% end %> <% if can? :comment_as_administrator, commentable %>
- <%= f.check_box :as_administrator, id: "comment-as-administrator-#{dom_id(commentable)}",label: false %> - <%= label_tag "comment-as-administrator-#{dom_id(commentable)}", t("comments.form.comment_as_admin"), class: "checkbox" %> + <%= f.check_box :as_administrator, id: "comment-as-administrator-#{css_id}",label: false %> + <%= label_tag "comment-as-administrator-#{css_id}", t("comments.form.comment_as_admin"), class: "checkbox" %>
<% end %> <% end %> diff --git a/app/views/comments/create.js.erb b/app/views/comments/create.js.erb index ed3e9c8f9..ef2400172 100644 --- a/app/views/comments/create.js.erb +++ b/app/views/comments/create.js.erb @@ -1,10 +1,11 @@ -var parent_id = "<%= dom_id(@parent) %>"; var comment_html = "<%= j(render @comment) %>" -<% if @parent.is_a?(Debate) -%> - App.Comments.reset_form(parent_id); - App.Comments.add_comment(parent_id, comment_html); +<% if @comment.root? -%> + var commentable_id = '<%= dom_id(@commentable) %>'; + App.Comments.reset_form(commentable_id); + App.Comments.add_comment(commentable_id, comment_html); <% else -%> + var parent_id = '<%= "comment_#{@comment.parent_id}" %>'; App.Comments.reset_and_hide_form(parent_id); App.Comments.add_reply(parent_id, comment_html); <% end -%> diff --git a/app/views/comments/new.js.erb b/app/views/comments/new.js.erb index 819686e72..963cdf16a 100644 --- a/app/views/comments/new.js.erb +++ b/app/views/comments/new.js.erb @@ -1,2 +1,5 @@ -var field_with_errors = "#js-comment-form-<%= dom_id(@parent) %> #comment-body-<%= dom_id(@parent) %>"; +<% dom_id = parent_or_commentable_dom_id(@comment.parent_id, @commentable) %> + +var field_with_errors = "#js-comment-form-<%= dom_id %> #comment-body-<%= dom_id %>"; App.Comments.display_error(field_with_errors, "<%= j render('comments/errors') %>"); + diff --git a/db/schema.rb b/db/schema.rb index 8b8dd88e9..fb5abfd98 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: 20150902114558) do +ActiveRecord::Schema.define(version: 20150902120006) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -75,9 +75,6 @@ ActiveRecord::Schema.define(version: 20150902114558) do t.integer "cached_votes_down", default: 0 t.datetime "confirmed_hide_at" t.string "ancestry" - t.integer "rgt" - t.integer "lft" - t.integer "parent_id" end add_index "comments", ["ancestry"], name: "index_comments_on_ancestry", using: :btree diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index c40863534..cb847e40e 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -124,8 +124,7 @@ feature 'Comments' do parent = create(:comment, commentable: debate) 7.times do - create(:comment, commentable: debate). - move_to_child_of(parent) + create(:comment, commentable: debate, parent: parent) parent = parent.children.first end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 4115fedcf..dbb254063 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -25,24 +25,22 @@ describe Comment do parent = comment 3.times do - create(:comment, commentable: debate). - move_to_child_of(parent) + create(:comment, commentable: debate, parent: parent) parent = parent.children.first end expect(comment.children_count).to eq(1) - expect(debate.comment_threads.count).to eq(4) + expect(debate.comments.count).to eq(4) end it "should increase children count" do expect do - create(:comment, commentable: debate). - move_to_child_of(comment) + create(:comment, commentable: debate, parent: comment) end.to change { comment.children_count }.from(0).to(1) end it "should decrease children count" do - new_comment = create(:comment, commentable: debate).move_to_child_of(comment) + new_comment = create(:comment, commentable: debate, parent: comment) expect { new_comment.destroy }.to change { comment.children_count }.from(1).to(0) end diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index b773ca269..d8b45ffc2 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -34,13 +34,14 @@ module CommonActions end def comment_on(debate) - user2 = create(:user) + user = create(:user) - login_as(user2) + login_as(user) visit debate_path(debate) fill_in "comment-body-debate_#{debate.id}", with: 'Have you thought about...?' click_button 'Publish comment' + expect(page).to have_content 'Have you thought about...?' end From 73f0b4b74e89b1929126f9ca5682092d120311e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 2 Sep 2015 19:24:57 +0200 Subject: [PATCH 06/10] adds migration to remove AaCwT columns it migrates existing comments to ancestry --- .../20150902120006_remove_parent_id_from_comments.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 db/migrate/20150902120006_remove_parent_id_from_comments.rb diff --git a/db/migrate/20150902120006_remove_parent_id_from_comments.rb b/db/migrate/20150902120006_remove_parent_id_from_comments.rb new file mode 100644 index 000000000..219196533 --- /dev/null +++ b/db/migrate/20150902120006_remove_parent_id_from_comments.rb @@ -0,0 +1,9 @@ +class RemoveParentIdFromComments < ActiveRecord::Migration + def change + Comment.build_ancestry_from_parent_ids! rescue nil + + remove_column :comments, :parent_id, :integer + remove_column :comments, :lft, :integer + remove_column :comments, :rgt, :integer + end +end From 136c1e73ed780d43aa2ea480bf5332f575478429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 3 Sep 2015 16:32:54 +0200 Subject: [PATCH 07/10] uses ancestry to retrieve debate comments --- app/controllers/debates_controller.rb | 7 ++++--- app/helpers/comments_helper.rb | 5 +++++ app/models/comment.rb | 9 --------- app/views/comments/_comment.html.erb | 6 +++--- app/views/debates/show.html.erb | 4 ++-- spec/features/comments_spec.rb | 2 +- spec/models/comment_spec.rb | 29 --------------------------- 7 files changed, 15 insertions(+), 47 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 865eadced..a9e7325eb 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -15,9 +15,10 @@ class DebatesController < ApplicationController def show set_debate_votes(@debate) @commentable = @debate - @comments = @debate.comments.roots.recent.page(params[:page]).for_render - # TODO limit this list to the paginated root comment's children once we have ancestry - all_visible_comments = @debate.comments + @root_comments = @debate.comments.roots.recent.page(params[:page]).per(10).for_render + @comments = @root_comments.inject([]){|all, root| all + root.descendants} + + all_visible_comments = @root_comments + @comments set_comment_flags(all_visible_comments) end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 300a07921..314518e0b 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -12,4 +12,9 @@ module CommentsHelper parent_id.blank? ? dom_id(commentable) : "comment_#{parent_id}" end + def children_of_in(parent, comments) + return [] if comments.blank? + comments.select{|c| c.parent_id == parent.id} + end + end \ No newline at end of file diff --git a/app/models/comment.rb b/app/models/comment.rb index fe57ee95f..422d39f8d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,4 @@ class Comment < ActiveRecord::Base - #acts_as_nested_set scope: [:commentable_id, :commentable_type], counter_cache: :children_count acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases acts_as_votable @@ -80,14 +79,6 @@ class Comment < ActiveRecord::Base moderator_id.present? end - # TODO: faking counter cache since there is a bug with acts_as_nested_set :counter_cache - # Remove when https://github.com/collectiveidea/awesome_nested_set/issues/294 is fixed - # and reset counters using - # > Comment.find_each { |comment| Comment.reset_counters(comment.id, :children) } - def children_count - children.count - end - def after_hide commentable_type.constantize.reset_counters(commentable_id, :comments) end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index ba9e99151..37a80d830 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -2,7 +2,7 @@
<% if comment.hidden? || comment.user.hidden? %> - <% if comment.children_count > 0 %> + <% if children_of_in(comment, @comments).size > 0 %>

<%= t("debates.comment.deleted") %>

@@ -79,7 +79,7 @@
- <%= t("debates.comment.responses", count: comment.children_count) %> + <%= t("debates.comment.responses", count: children_of_in(comment, @comments).size) %> <% if user_signed_in? %>  |  @@ -95,7 +95,7 @@ <% end %>
- <%= render comment.children.for_render.reorder('id DESC') %> + <%= render children_of_in(comment, @comments) if @comments.present? %>
diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index d5bce5bef..1e29894c4 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -92,8 +92,8 @@
<% end %> - <%= render @comments %> - <%= paginate @comments %> + <%= render @root_comments %> + <%= paginate @root_comments %>
diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index cb847e40e..b7a53eeb7 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -21,7 +21,7 @@ feature 'Comments' do scenario 'Paginated comments' do debate = create(:debate) - per_page = Kaminari.config.default_per_page + per_page = 10 (per_page + 2).times { create(:comment, commentable: debate)} visit debate_path(debate) diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index dbb254063..490a5cfd1 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -17,35 +17,6 @@ describe Comment do expect(debate.reload.comments_count).to eq(0) end - describe "#children_count" do - let(:comment) { create(:comment) } - let(:debate) { comment.debate } - - it "should count first level children" do - parent = comment - - 3.times do - create(:comment, commentable: debate, parent: parent) - parent = parent.children.first - end - - expect(comment.children_count).to eq(1) - expect(debate.comments.count).to eq(4) - end - - it "should increase children count" do - expect do - create(:comment, commentable: debate, parent: comment) - end.to change { comment.children_count }.from(0).to(1) - end - - it "should decrease children count" do - new_comment = create(:comment, commentable: debate, parent: comment) - - expect { new_comment.destroy }.to change { comment.children_count }.from(1).to(0) - end - end - describe "#as_administrator?" do it "should be true if comment has administrator_id, false otherway" do expect(comment).not_to be_as_administrator From b7518fe98f9ddc3560eed6822003a911bcbb6ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 3 Sep 2015 16:33:13 +0200 Subject: [PATCH 08/10] updates dev_seeds.rb --- db/dev_seeds.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index bbd42d55e..58a1da128 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -85,8 +85,8 @@ puts "Commenting Comments" comment = Comment.create!(user: author, commentable_id: parent.commentable_id, commentable_type: parent.commentable_type, - body: Faker::Lorem.sentence) - comment.move_to_child_of(parent) + body: Faker::Lorem.sentence, + parent: parent) end From bf017a85572626fba68f70f22a0e99b65cae573b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 3 Sep 2015 16:33:29 +0200 Subject: [PATCH 09/10] removes children_count from Comment --- .../20150903142924_remove_children_count_from_comments.rb | 5 +++++ db/schema.rb | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20150903142924_remove_children_count_from_comments.rb diff --git a/db/migrate/20150903142924_remove_children_count_from_comments.rb b/db/migrate/20150903142924_remove_children_count_from_comments.rb new file mode 100644 index 000000000..6e4937335 --- /dev/null +++ b/db/migrate/20150903142924_remove_children_count_from_comments.rb @@ -0,0 +1,5 @@ +class RemoveChildrenCountFromComments < ActiveRecord::Migration + def change + remove_column :comments, :children_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index fb5abfd98..dce574423 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: 20150902120006) do +ActiveRecord::Schema.define(version: 20150903142924) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -64,7 +64,6 @@ ActiveRecord::Schema.define(version: 20150902120006) do t.integer "user_id", null: false t.datetime "created_at" t.datetime "updated_at" - t.integer "children_count", default: 0 t.datetime "hidden_at" t.integer "flags_count", default: 0 t.datetime "ignored_flag_at" From dfc1ccb108da882646fe82109d5bf26b487adb94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 3 Sep 2015 17:13:19 +0200 Subject: [PATCH 10/10] renames helper method --- app/helpers/comments_helper.rb | 2 +- app/views/comments/_comment.html.erb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 314518e0b..36952af7d 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -12,7 +12,7 @@ module CommentsHelper parent_id.blank? ? dom_id(commentable) : "comment_#{parent_id}" end - def children_of_in(parent, comments) + def select_children(comments, parent) return [] if comments.blank? comments.select{|c| c.parent_id == parent.id} end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 37a80d830..3ee61e9f4 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -2,7 +2,7 @@
<% if comment.hidden? || comment.user.hidden? %> - <% if children_of_in(comment, @comments).size > 0 %> + <% if select_children(@comments, comment).size > 0 %>

<%= t("debates.comment.deleted") %>

@@ -79,7 +79,7 @@
- <%= t("debates.comment.responses", count: children_of_in(comment, @comments).size) %> + <%= t("debates.comment.responses", count: select_children(@comments, comment).size) %> <% if user_signed_in? %>  |  @@ -95,7 +95,7 @@ <% end %>
- <%= render children_of_in(comment, @comments) if @comments.present? %> + <%= render select_children(@comments, comment) if @comments.present? %>