From 1af047089ffc843f3e6d194f25e45cea581d323a Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Wed, 18 Jan 2017 16:39:06 +0100 Subject: [PATCH 1/5] Add form to publish a new first level comment from the comments box --- app/assets/javascripts/legislation.js.coffee | 2 +- .../legislation/annotations_controller.rb | 16 ++++++++- app/models/abilities/everyone.rb | 2 +- .../annotations/_comments_box.html.erb | 34 +++++++++++++++++-- .../legislation/annotations/comments.js.erb | 9 +++++ config/routes.rb | 1 + 6 files changed, 58 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/legislation.js.coffee b/app/assets/javascripts/legislation.js.coffee index cd4e90bc3..3eb53e6d2 100644 --- a/app/assets/javascripts/legislation.js.coffee +++ b/app/assets/javascripts/legislation.js.coffee @@ -4,7 +4,7 @@ App.Legislation = $('#js-toggle-debate').on click: -> $('#debate-show').toggle() - + $('#js-toggle-small-debate').on click: -> $('#debate-show').toggle() diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index 6f5069763..c97165e2a 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -1,7 +1,7 @@ class Legislation::AnnotationsController < ApplicationController skip_before_action :verify_authenticity_token - before_action :authenticate_user!, only: [:create] + before_action :authenticate_user!, only: [:create, :new_comment] before_action :convert_ranges_parameters, only: [:create] load_and_authorize_resource :process @@ -43,6 +43,7 @@ class Legislation::AnnotationsController < ApplicationController def comments @annotation = Legislation::Annotation.find(params[:annotation_id]) + @comment = @annotation.comments.new end def new @@ -51,6 +52,19 @@ class Legislation::AnnotationsController < ApplicationController end end + def new_comment + @draft_version = Legislation::DraftVersion.find(params[:draft_version_id]) + @annotation = @draft_version.annotations.find(params[:annotation_id]) + @comment = @annotation.comments.new(body: params[:comment][:body], user: current_user) + if @comment.save + @comment = @annotation.comments.new + end + + respond_to do |format| + format.js { render :comments } + end + end + private def annotation_params diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index ae3dc432d..4d44f14eb 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -20,7 +20,7 @@ module Abilities can [:read, :changes, :go_to_version], Legislation::DraftVersion can [:read], Legislation::Question can [:create], Legislation::Answer - can [:search, :comments, :read, :create], Legislation::Annotation + can [:search, :comments, :read, :create, :new_comment], Legislation::Annotation end end end diff --git a/app/views/legislation/annotations/_comments_box.html.erb b/app/views/legislation/annotations/_comments_box.html.erb index 4f33d45ef..2a543566c 100644 --- a/app/views/legislation/annotations/_comments_box.html.erb +++ b/app/views/legislation/annotations/_comments_box.html.erb @@ -1,12 +1,12 @@
-
<%= t('legislation.annotations.comments.comments_count', count: annotation.comments_count) %>
+
<%= t('legislation.annotations.comments.comments_count', count: annotation.comments.roots.count) %>
<%= link_to legislation_process_draft_version_annotation_path(annotation.draft_version.process, annotation.draft_version, annotation) do %> <% end %>
- <% annotation.comments.roots.limit(5).each do |comment| %> + <% annotation.comments.roots.sort_by_newest.limit(Legislation::Annotation::COMMENTS_PAGE_SIZE).each do |comment| %>

<%= truncate comment.body, length: 250 %>

@@ -36,11 +36,39 @@ <% end %>
+ + diff --git a/app/views/legislation/annotations/comments.js.erb b/app/views/legislation/annotations/comments.js.erb index 75d8f35a0..c68cfc120 100644 --- a/app/views/legislation/annotations/comments.js.erb +++ b/app/views/legislation/annotations/comments.js.erb @@ -1,2 +1,11 @@ $("#comments-box").html("<%= j render('comments_box', annotation: @annotation) %>").show(); +$('a.publish-comment').on({ + click: function(e) { + e.preventDefault(); + $('a.publish-comment').hide(); + $('#js-comment-form-annotation').toggle(); + return; + } +}); + diff --git a/config/routes.rb b/config/routes.rb index 774efb37c..f5b65420e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -114,6 +114,7 @@ Rails.application.routes.draw do resources :annotations do get :search, on: :collection get :comments + post :new_comment end end end From 47b35a8149a5b4498e053ce1dd8e69dcf35db1a2 Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Fri, 20 Jan 2017 11:32:31 +0100 Subject: [PATCH 2/5] Add spec for publishing a comment from the comments box for a draft --- .../annotations/_comments_box.html.erb | 2 +- .../legislation/draft_versions_spec.rb | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/views/legislation/annotations/_comments_box.html.erb b/app/views/legislation/annotations/_comments_box.html.erb index 2a543566c..3fa3e61c5 100644 --- a/app/views/legislation/annotations/_comments_box.html.erb +++ b/app/views/legislation/annotations/_comments_box.html.erb @@ -45,7 +45,7 @@ <% end %> <% if @process.open_phase?(:allegations) %> - Publicar comentario + <%= t('legislation.annotations.comments.publish_comment') %> <% end %> <% if @process.open_phase?(:allegations) %> diff --git a/spec/features/legislation/draft_versions_spec.rb b/spec/features/legislation/draft_versions_spec.rb index d0f303743..fc6a1b88f 100644 --- a/spec/features/legislation/draft_versions_spec.rb +++ b/spec/features/legislation/draft_versions_spec.rb @@ -171,7 +171,7 @@ feature 'Legislation Draft Versions' do expect(page).to have_content "this is my annotation" end - scenario 'Search' do + scenario 'View annotations and comments' do draft_version = create(:legislation_draft_version, :published, body: Faker::Lorem.paragraph) annotation1 = create(:legislation_annotation, draft_version: draft_version, text: "my annotation", ranges: [{"start"=>"/p[1]", "startOffset"=>5, "end"=>"/p[1]", "endOffset"=>10}]) annotation2 = create(:legislation_annotation, draft_version: draft_version, text: "my other annotation", ranges: [{"start"=>"/p[1]", "startOffset"=>12, "end"=>"/p[1]", "endOffset"=>19}]) @@ -185,6 +185,22 @@ feature 'Legislation Draft Versions' do all(".annotator-hl")[1].trigger('click') expect(page).to have_content "my other annotation" end + + scenario "Publish new comment for an annotation from comments box" do + draft_version = create(:legislation_draft_version, :published, body: Faker::Lorem.paragraph) + annotation = create(:legislation_annotation, draft_version: draft_version, text: "my annotation", ranges: [{"start"=>"/p[1]", "startOffset"=>5, "end"=>"/p[1]", "endOffset"=>10}]) + + visit legislation_process_draft_version_path(draft_version.process, draft_version) + + expect(page).to have_css ".annotator-hl" + first(:css, ".annotator-hl").click + expect(page).to have_content "my annotation" + + click_link "Publish Comment" + fill_in "comment[body]", with: "My interesting comment" + click_button "Publish comment" + expect(page).to have_content "My interesting comment" + end end context "Annotations page" do From 2a5d21f8d457953167e3be3e8379b01ecdfeb606 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Fri, 20 Jan 2017 18:28:15 +0100 Subject: [PATCH 3/5] Focus on textarea when showing the comments box --- app/views/legislation/annotations/comments.js.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/legislation/annotations/comments.js.erb b/app/views/legislation/annotations/comments.js.erb index c68cfc120..7718b0465 100644 --- a/app/views/legislation/annotations/comments.js.erb +++ b/app/views/legislation/annotations/comments.js.erb @@ -5,6 +5,7 @@ $('a.publish-comment').on({ e.preventDefault(); $('a.publish-comment').hide(); $('#js-comment-form-annotation').toggle(); + $('#js-comment-form-annotation textarea').focus(); return; } }); From 3d8724b13b8e6b31ac2cb71919b236e3c27fec8e Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Fri, 20 Jan 2017 18:33:07 +0100 Subject: [PATCH 4/5] Remove unnecessary hidden fields --- app/views/legislation/annotations/_comments_box.html.erb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/views/legislation/annotations/_comments_box.html.erb b/app/views/legislation/annotations/_comments_box.html.erb index 3fa3e61c5..8844fca38 100644 --- a/app/views/legislation/annotations/_comments_box.html.erb +++ b/app/views/legislation/annotations/_comments_box.html.erb @@ -54,11 +54,7 @@ From 63a986d31c34e945e31286b747869da1fce23b14 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Fri, 20 Jan 2017 18:35:34 +0100 Subject: [PATCH 5/5] If the comment has errors, show the textarea with the error message --- app/views/legislation/annotations/comments.js.erb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/views/legislation/annotations/comments.js.erb b/app/views/legislation/annotations/comments.js.erb index 7718b0465..8074f6040 100644 --- a/app/views/legislation/annotations/comments.js.erb +++ b/app/views/legislation/annotations/comments.js.erb @@ -10,3 +10,8 @@ $('a.publish-comment').on({ } }); +<% if @comment.errors.any? %> + $('a.publish-comment').hide(); + $('#js-comment-form-annotation').toggle(); + $('#js-comment-form-annotation textarea').focus(); +<% end %>