ancestries
moves to ancestry from acts_as_commentable_with_threading
This commit is contained in:
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
<%= t("shared.collective") %>
|
||||
</span>
|
||||
<% end %>
|
||||
<% if comment.user_id == @debate.author_id %>
|
||||
<% if comment.user_id == @commentable.author_id %>
|
||||
•
|
||||
<span class="label round is-author">
|
||||
<%= t("debates.comment.author") %>
|
||||
@@ -65,11 +65,11 @@
|
||||
<p class="comment-user is-admin"><%= comment.body %></p>
|
||||
<% elsif comment.as_moderator? %>
|
||||
<p class="comment-user is-moderator"><%= comment.body %></p>
|
||||
<% elsif comment.user.official? && comment.user_id == @debate.author_id %>
|
||||
<% elsif comment.user.official? && comment.user_id == @commentable.author_id %>
|
||||
<p class="comment-user level-<%= comment.user.official_level %> is-author"><%= comment.body %></p>
|
||||
<% elsif comment.user.official? %>
|
||||
<p class="comment-user level-<%= comment.user.official_level %>"><%= comment.body %></p>
|
||||
<% elsif comment.user_id == @debate.author_id %>
|
||||
<% elsif comment.user_id == @commentable.author_id %>
|
||||
<p class="comment-user is-author"><%= comment.body %></p>
|
||||
<% else %>
|
||||
<p class="comment-user"><%= comment.body %></p>
|
||||
@@ -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 %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
<div id="js-comment-form-<%= dom_id(commentable) %>" <%= "style='display:none'".html_safe if toggeable %>>
|
||||
<% css_id = parent_or_commentable_dom_id(parent_id, commentable) %>
|
||||
<div id="js-comment-form-<%= css_id %>" <%= "style='display:none'".html_safe if toggeable %>>
|
||||
<%= 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 %>º
|
||||
<div class="right">
|
||||
<%= 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" %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% if can? :comment_as_administrator, commentable %>
|
||||
<div class="right">
|
||||
<%= 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" %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
@@ -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 -%>
|
||||
|
||||
@@ -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') %>");
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user