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