From 816a95b7b72df3a041536cb8e9dc43864b5e6efc Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 7 Jan 2016 14:19:05 +0100 Subject: [PATCH 1/3] adds comment show view --- app/controllers/comments_controller.rb | 7 ++++++- app/helpers/comments_helper.rb | 7 +++++-- app/models/abilities/everyone.rb | 1 + app/views/comments/_comment.html.erb | 13 +++++++------ app/views/comments/show.html.erb | 12 ++++++++++++ config/locales/en.yml | 2 ++ config/locales/es.yml | 2 ++ config/routes.rb | 2 +- spec/features/comments/debates_spec.rb | 17 ++++++++++++++++- spec/features/comments/proposals_spec.rb | 15 +++++++++++++++ spec/models/abilities/everyone_spec.rb | 2 ++ 11 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 app/views/comments/show.html.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 35406faf0..050962414 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,5 +1,5 @@ class CommentsController < ApplicationController - before_action :authenticate_user! + before_action :authenticate_user!, only: :create before_action :load_commentable, only: :create before_action :build_comment, only: :create @@ -14,6 +14,11 @@ class CommentsController < ApplicationController end end + def show + @comment = Comment.find(params[:id]) + set_comment_flags(@comment.subtree) + end + def vote @comment.vote_by(voter: current_user, vote: params[:value]) respond_with @comment diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 9ac29e4da..9d9d08075 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -13,8 +13,11 @@ module CommentsHelper end def child_comments_of(parent) - return [] unless @comment_tree - @comment_tree.children_of(parent) + if @comment_tree.present? + @comment_tree.ordered_children_of(parent) + else + parent.children + end end def user_level_class(comment) diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index d6b2a6d57..122d5db2a 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -5,6 +5,7 @@ module Abilities def initialize(user) can :read, Debate can :read, Proposal + can :read, Comment can :read, Legislation can :read, User can [:search, :read], Annotation diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 9178f0b9b..44e15362b 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,9 +1,9 @@ -<% cache [locale_and_user_status(comment), comment, commentable_cache_key(@commentable), comment.author, (@comment_flags[comment.id] if @comment_flags)] do %> +<% cache [locale_and_user_status(comment), comment, commentable_cache_key(comment.commentable), comment.author, (@comment_flags[comment.id] if @comment_flags)] do %>
<% if comment.hidden? || comment.user.hidden? %> - <% if child_comments_of(comment).size > 0 %> + <% if comment.children.size > 0 %>

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

@@ -49,7 +49,7 @@ <%= t("shared.collective") %> <% end %> - <% if comment.user_id == @commentable.author_id %> + <% if comment.user_id == comment.commentable.author_id %>  •  <%= t("comments.comment.author") %> @@ -63,7 +63,7 @@
+ <%= comment_author_class comment, comment.commentable.author_id %>"> <%= simple_format text_with_links comment.body %>
@@ -72,7 +72,7 @@
- <%= t("comments.comment.responses", count: child_comments_of(comment).size) %> + <%= t("comments.comment.responses", count: comment.children.size) %> <% if user_signed_in? %>  |  @@ -81,11 +81,12 @@ <%= render 'comments/actions', comment: comment %> - <%= render 'comments/form', {commentable: @commentable, parent_id: comment.id, toggeable: true} %> + <%= render 'comments/form', {commentable: comment.commentable, parent_id: comment.id, toggeable: true} %> <% end %>
<% end %> +
<% child_comments_of(comment).each do |child| %> <%= render 'comments/comment', comment: child %> diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb new file mode 100644 index 000000000..b0c3dfb3f --- /dev/null +++ b/app/views/comments/show.html.erb @@ -0,0 +1,12 @@ +
+ <%= link_to t("comments.show.return_to_commentable") + @comment.commentable.title, + @comment.commentable %> +
+ +
+
+
+ <%= render @comment %> +
+
+
\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 3ee698c91..57a9fb92e 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -244,6 +244,8 @@ en: form: submit_button: "Save changes" comments: + show: + return_to_commentable: "Go back to " select_order: "Sort by" orders: most_voted: "Most voted" diff --git a/config/locales/es.yml b/config/locales/es.yml index 46bb3b88b..65eb1eb8b 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -244,6 +244,8 @@ es: form: submit_button: "Guardar cambios" comments: + show: + return_to_commentable: "Volver a " select_order: "Ordenar por" orders: most_voted: "Más votados" diff --git a/config/routes.rb b/config/routes.rb index 3472f1c4e..2da32d479 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,7 +53,7 @@ Rails.application.routes.draw do end end - resources :comments, only: :create, shallow: true do + resources :comments, only: [:create, :show], shallow: true do member do post :vote put :flag diff --git a/spec/features/comments/debates_spec.rb b/spec/features/comments/debates_spec.rb index 5adfa218c..7d807a6c0 100644 --- a/spec/features/comments/debates_spec.rb +++ b/spec/features/comments/debates_spec.rb @@ -20,6 +20,21 @@ feature 'Commenting debates' do end end + scenario 'Show' do + parent_comment = create(:comment, commentable: debate) + first_child = create(:comment, commentable: debate, parent: parent_comment) + second_child = create(:comment, commentable: debate, parent: parent_comment) + + visit comment_path(parent_comment) + + expect(page).to have_css(".comment", count: 3) + expect(page).to have_content parent_comment.body + expect(page).to have_content first_child.body + expect(page).to have_content second_child.body + + expect(page).to have_link "Go back to #{debate.title}", debate_path(debate) + end + scenario 'Comment order' do c1 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.now - 2) c2 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1) @@ -254,7 +269,7 @@ feature 'Commenting debates' do fill_in "comment-body-debate_#{debate.id}", with: 'Testing submit button!' click_button 'Publish comment' - + # The button's text should now be "..." # This should be checked before the Ajax request is finished expect(page).to_not have_button 'Publish comment' diff --git a/spec/features/comments/proposals_spec.rb b/spec/features/comments/proposals_spec.rb index 1c277cf4d..d919f0b51 100644 --- a/spec/features/comments/proposals_spec.rb +++ b/spec/features/comments/proposals_spec.rb @@ -20,6 +20,21 @@ feature 'Commenting proposals' do end end + scenario 'Show' do + parent_comment = create(:comment, commentable: proposal) + first_child = create(:comment, commentable: proposal, parent: parent_comment) + second_child = create(:comment, commentable: proposal, parent: parent_comment) + + visit comment_path(parent_comment) + + expect(page).to have_css(".comment", count: 3) + expect(page).to have_content parent_comment.body + expect(page).to have_content first_child.body + expect(page).to have_content second_child.body + + expect(page).to have_link "Go back to #{proposal.title}", proposal_path(proposal) + end + scenario 'Comment order' do c1 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.now - 2) c2 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1) diff --git a/spec/models/abilities/everyone_spec.rb b/spec/models/abilities/everyone_spec.rb index 4c532b7a4..dacac25e4 100644 --- a/spec/models/abilities/everyone_spec.rb +++ b/spec/models/abilities/everyone_spec.rb @@ -21,4 +21,6 @@ describe "Abilities::Everyone" do it { should_not be_able_to(:vote, Proposal) } it { should_not be_able_to(:flag, Proposal) } it { should_not be_able_to(:unflag, Proposal) } + + it { should be_able_to(:show, Comment) } end From 35704f2be906a1ef9306dba29c44f6a39d117b7f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 7 Jan 2016 15:18:45 +0100 Subject: [PATCH 2/3] refactors comment tree --- lib/comment_tree.rb | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/comment_tree.rb b/lib/comment_tree.rb index 8d0e072dd..f2eadb8ab 100644 --- a/lib/comment_tree.rb +++ b/lib/comment_tree.rb @@ -2,24 +2,34 @@ class CommentTree ROOT_COMMENTS_PER_PAGE = 10 - attr_accessor :root_comments, :comments + attr_accessor :root_comments, :comments, :commentable, :page, :order def initialize(commentable, page, order = 'confidence_score') - @root_comments = commentable.comments.roots.send("sort_by_#{order}").page(page).per(ROOT_COMMENTS_PER_PAGE).for_render - - root_descendants = @root_comments.each_with_object([]) do |root, col| - col.concat(Comment.descendants_of(root).send("sort_descendants_by_#{order}").for_render.to_a) - end + @commentable = commentable + @page = page + @order = order @comments = root_comments + root_descendants + end - @comments_by_parent_id = @comments.each_with_object({}) do |comment, col| - (col[comment.parent_id] ||= []) << comment + def root_comments + commentable.comments.roots.send("sort_by_#{order}").page(page).per(ROOT_COMMENTS_PER_PAGE).for_render + end + + def root_descendants + root_comments.each_with_object([]) do |root, array| + array.concat(Comment.descendants_of(root).send("sort_descendants_by_#{order}").for_render.to_a) end end - def children_of(parent) - @comments_by_parent_id[parent.id] || [] + def ordered_children_of(parent) + comments_by_parent_id[parent.id] || [] + end + + def comments_by_parent_id + comments.each_with_object({}) do |comment, array| + (array[comment.parent_id] ||= []) << comment + end end def comment_authors From 5ef9f4de873cf6d507f58e30e716495e86d2523a Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Fri, 8 Jan 2016 13:41:18 +0100 Subject: [PATCH 3/3] Improves styles for comments show --- app/assets/stylesheets/layout.scss | 10 +++------- app/views/comments/_comment.html.erb | 8 ++++---- app/views/comments/show.html.erb | 12 ++++++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index d47189d18..ffb2c6c76 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -1582,7 +1582,6 @@ table { font-family: $font-sans; font-size: $small-font-size; line-height: $line-height; - margin: rem-calc(10) $line-height/2 $line-height/4 0; a { color: $text-light; @@ -1603,15 +1602,12 @@ table { .comment-body { margin-left: rem-calc(42); - p { - font-size: $small-font-size; - } - .reply { background: white; border: 1px solid $border; - font-family: $font-sans; - font-size: rem-calc(12); + border-left: 0; + border-right: 0; + font-size: $small-font-size; margin: rem-calc(6) 0; padding: rem-calc(6); diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 44e15362b..c001da74e 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -67,11 +67,11 @@ <%= simple_format text_with_links comment.body %>
- - <%= render 'comments/votes', comment: comment %> - -
+ + <%= render 'comments/votes', comment: comment %> + + <%= t("comments.comment.responses", count: comment.children.size) %> <% if user_signed_in? %> diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb index b0c3dfb3f..4fcbdead4 100644 --- a/app/views/comments/show.html.erb +++ b/app/views/comments/show.html.erb @@ -1,9 +1,13 @@ -
- <%= link_to t("comments.show.return_to_commentable") + @comment.commentable.title, - @comment.commentable %> +
+
+ <%= link_to @comment.commentable, class: "left back" do %> + + <%= t("comments.show.return_to_commentable") + @comment.commentable.title %> + <% end %> +
-
+
<%= render @comment %>