uses ancestry to retrieve debate comments
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div id="<%= dom_id(comment) %>" class="comment small-12 column">
|
||||
|
||||
<% if comment.hidden? || comment.user.hidden? %>
|
||||
<% if comment.children_count > 0 %>
|
||||
<% if children_of_in(comment, @comments).size > 0 %>
|
||||
<div class="is-deleted">
|
||||
<p><%= t("debates.comment.deleted") %></p>
|
||||
</div>
|
||||
@@ -79,7 +79,7 @@
|
||||
</span>
|
||||
|
||||
<div class="reply">
|
||||
<%= t("debates.comment.responses", count: comment.children_count) %>
|
||||
<%= t("debates.comment.responses", count: children_of_in(comment, @comments).size) %>
|
||||
|
||||
<% if user_signed_in? %>
|
||||
<span class="divider"> | </span>
|
||||
@@ -95,7 +95,7 @@
|
||||
<% end %>
|
||||
|
||||
<div class="comment-children">
|
||||
<%= render comment.children.for_render.reorder('id DESC') %>
|
||||
<%= render children_of_in(comment, @comments) if @comments.present? %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -92,8 +92,8 @@
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= render @comments %>
|
||||
<%= paginate @comments %>
|
||||
<%= render @root_comments %>
|
||||
<%= paginate @root_comments %>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user