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 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 672336ae2..3845afebf 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -16,9 +16,11 @@ class DebatesController < ApplicationController def show set_debate_votes(@debate) - @comments = @debate.root_comments.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 + @commentable = @debate + @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 5c41b3432..36952af7d 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -1,11 +1,20 @@ module CommentsHelper - def comment_link_text(parent) - parent.class == Debate ? t("comments_helper.comment_link") : t("comments_helper.reply_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.class == Debate ? t("comments_helper.comment_button") : t("comments_helper.reply_button") + def comment_button_text(parent_id) + 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 + + def select_children(comments, parent) + 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 77cbd166e..422d39f8d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,13 +1,14 @@ 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 + 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 +24,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 @@ -77,16 +79,12 @@ 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 + def after_hide + commentable_type.constantize.reset_counters(commentable_id, :comments) end - def after_hide - commentable_type.constantize.reset_counters(commentable_id, :comment_threads) + def reply? + !root? end end diff --git a/app/models/debate.rb b/app/models/debate.rb index c5cacf0f0..61e6546b4 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 e21b148bc..3ee61e9f4 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -2,7 +2,7 @@
<%= t("debates.comment.deleted") %>
<%= 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 %>
@@ -79,7 +79,7 @@