From 4431d95d31c5facf343bc09d34a802a32f442f94 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Tue, 10 Jan 2017 18:21:37 +0100 Subject: [PATCH 01/10] Basic annotation creation --- app/assets/javascripts/application.js | 2 +- app/assets/javascripts/custom.js | 3 +- .../legislation_annotatable.js.coffee | 22 ++++++++++++- .../legislation/annotations_controller.rb | 15 +++++++++ .../legislation/annotations/_form.html.erb | 31 +++++++++++++++++++ app/views/legislation/annotations/new.js.erb | 2 ++ config/locales/en.yml | 2 ++ config/locales/es.yml | 2 ++ 8 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 app/views/legislation/annotations/_form.html.erb create mode 100644 app/views/legislation/annotations/new.js.erb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 939705c51..19c081b54 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -53,8 +53,8 @@ //= require cocoon //= require legislation //= require legislation_allegations -//= require legislation_annotatable //= require custom +//= require legislation_annotatable var initialize_modules = function() { App.Comments.initialize(); diff --git a/app/assets/javascripts/custom.js b/app/assets/javascripts/custom.js index 6c880b3a9..50903021a 100644 --- a/app/assets/javascripts/custom.js +++ b/app/assets/javascripts/custom.js @@ -1,7 +1,6 @@ // Overrides and adds customized javascripts in this file -// Read more on documentation: +// Read more on documentation: // * English: https://github.com/consul/consul/blob/master/CUSTOMIZE_EN.md#javascript // * Spanish: https://github.com/consul/consul/blob/master/CUSTOMIZE_ES.md#javascript // -// diff --git a/app/assets/javascripts/legislation_annotatable.js.coffee b/app/assets/javascripts/legislation_annotatable.js.coffee index f1df2927a..dd861c6ef 100644 --- a/app/assets/javascripts/legislation_annotatable.js.coffee +++ b/app/assets/javascripts/legislation_annotatable.js.coffee @@ -19,7 +19,23 @@ App.LegislationAnnotatable = annotation_url: $(event.target).closest(".legislation-annotatable").data("legislation-annotatable-base-url") offset: $(event.target).offset()["top"] + customShow: (position) -> + $(@element).html '' + # Clean comments section and open it + $('#comments-box').html '' + App.LegislationAllegations.show_comments() + annotation_url = $('[data-legislation-annotatable-base-url]').data('legislation-annotatable-base-url') + $.ajax( + method: 'GET' + url: annotation_url + '/annotations/new' + dataType: 'script').done (-> + $('#new_annotation #annotation_quote').val(@annotation.quote) + $('#new_annotation #annotation_ranges').val(JSON.stringify(@annotation.ranges)) + ).bind(this) + + editorExtension: (editor) -> + editor.show = App.LegislationAnnotatable.customShow scrollToAnchor: -> annotationsLoaded: (annotations) -> @@ -62,7 +78,11 @@ App.LegislationAnnotatable = ann["legislation_draft_version_id"] = ann_id ann.permissions = ann.permissions || {} ann.permissions.admin = [] - .include(annotator.ui.main, { element: this, viewerExtensions: [App.LegislationAnnotatable.viewerExtension] }) + .include(annotator.ui.main, { + element: this, + viewerExtensions: [App.LegislationAnnotatable.viewerExtension], + editorExtensions: [App.LegislationAnnotatable.editorExtension] + }) .include(App.LegislationAnnotatable.scrollToAnchor) .include(annotator.storage.http, { prefix: base_url, urls: { search: "/annotations/search" } }) diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index f1c4e4401..861eb9d14 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -2,6 +2,7 @@ class Legislation::AnnotationsController < ApplicationController skip_before_action :verify_authenticity_token before_action :authenticate_user!, only: [:create] + before_action :convert_ranges_parameters, only: [:create] load_and_authorize_resource :process load_and_authorize_resource :draft_version, through: :process @@ -44,6 +45,13 @@ class Legislation::AnnotationsController < ApplicationController @annotation = Legislation::Annotation.find(params[:annotation_id]) end + def new + respond_to do |format| + format.js + end + end + + private def annotation_params @@ -58,4 +66,11 @@ class Legislation::AnnotationsController < ApplicationController "legislation_draft_version_id": @draft_version.id end + def convert_ranges_parameters + if params[:annotation] && params[:annotation][:ranges] + params[:annotation][:ranges] = JSON.parse(params[:annotation][:ranges]) + end + rescue JSON::ParserError + end + end diff --git a/app/views/legislation/annotations/_form.html.erb b/app/views/legislation/annotations/_form.html.erb new file mode 100644 index 000000000..81605e648 --- /dev/null +++ b/app/views/legislation/annotations/_form.html.erb @@ -0,0 +1,31 @@ +
+ +
<%= t('legislation.annotations.comments.comments_count', count: 0) %>
+ <%= link_to '#' do %> + + <% end %> +
+ +
+
+ <%= form_for [@process, @draft_version, Annotation.new], url: legislation_process_draft_version_annotations_path(@process, @draft_version), remote: true do |f| %> + <%= f.text_area :text, autofocus: true %> + +
+ <%= t('legislation.annotations.comments.cancel') %> + <%= f.submit t('legislation.annotations.comments.publish_comment'), class: 'button strong publish-comment' %> + <% if false %> + <%= t('legislation.annotations.comments.publish_comment') %> + <% end %> +
+ + <%= f.hidden_field :quote %> + <%= f.hidden_field :ranges %> + <% end %> +
+ + +
+ diff --git a/app/views/legislation/annotations/new.js.erb b/app/views/legislation/annotations/new.js.erb new file mode 100644 index 000000000..9b719814c --- /dev/null +++ b/app/views/legislation/annotations/new.js.erb @@ -0,0 +1,2 @@ +$("#comments-box").html("<%= j render('form') %>"); + diff --git a/config/locales/en.yml b/config/locales/en.yml index 760fa67c8..09eac149c 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -235,6 +235,8 @@ en: replies_count: one: "%{count} reply" other: "%{count} replies" + cancel: Cancel + publish_comment: Publish Comment index: title: Comments comments_about: Comments about diff --git a/config/locales/es.yml b/config/locales/es.yml index 81716214b..1a9626cc6 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -235,6 +235,8 @@ es: replies_count: one: "%{count} respuesta" other: "%{count} respuestas" + cancel: Cancelar + publish_comment: Publicar Comentario index: title: Comentarios see_in_context: Ver en contexto From 17a68f5c14233cae3a6b52a1273e46d69d84d924 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Wed, 11 Jan 2017 17:20:00 +0100 Subject: [PATCH 02/10] Load annotations on click event --- .../legislation_annotatable.js.coffee | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/legislation_annotatable.js.coffee b/app/assets/javascripts/legislation_annotatable.js.coffee index dd861c6ef..343c638ad 100644 --- a/app/assets/javascripts/legislation_annotatable.js.coffee +++ b/app/assets/javascripts/legislation_annotatable.js.coffee @@ -9,15 +9,21 @@ App.LegislationAnnotatable = url: event.annotation_url + "/annotations/" + event.annotation_id + "/comments" dataType: 'script' + onClick: (event) -> + event.preventDefault() + event.stopPropagation() + + App.LegislationAllegations.show_comments() + $("#comments-box").show() + $.event.trigger + type: "renderLegislationAnnotation" + annotation_id: $(event.target).data("annotation-id") + annotation_url: $(event.target).closest(".legislation-annotatable").data("legislation-annotatable-base-url") + offset: $(event.target).offset()["top"] + viewerExtension: (viewer) -> viewer._onHighlightMouseover = (event) -> - App.LegislationAllegations.show_comments() - $("#comments-box").show() - $.event.trigger - type: "renderLegislationAnnotation" - annotation_id: $(event.target).data("annotation-id") - annotation_url: $(event.target).closest(".legislation-annotatable").data("legislation-annotatable-base-url") - offset: $(event.target).offset()["top"] + return customShow: (position) -> $(@element).html '' @@ -52,6 +58,7 @@ App.LegislationAnnotatable = initialize: -> $(document).on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments) + $(document).on('click', '[data-annotation-id]', App.LegislationAnnotatable.onClick) current_user_id = $('html').data('current-user-id') if current_user_id == "" From 72ff1de2ab62e93a1cab97580030dae15514a34d Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Thu, 12 Jan 2017 19:36:16 +0100 Subject: [PATCH 03/10] CSS fix --- app/assets/stylesheets/legislation_process.scss | 1 - 1 file changed, 1 deletion(-) diff --git a/app/assets/stylesheets/legislation_process.scss b/app/assets/stylesheets/legislation_process.scss index d5ab74130..e7fc4dcc1 100644 --- a/app/assets/stylesheets/legislation_process.scss +++ b/app/assets/stylesheets/legislation_process.scss @@ -831,7 +831,6 @@ textarea { border-radius: 0; box-shadow: none; - border-top: none; border-bottom: 1px solid #D0D0D0; border-right: 1px solid #D0D0D0; border-left: 1px solid #D0D0D0; From 755ddab2cb434f170446023c0a2abfc637afc6f6 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Thu, 12 Jan 2017 19:36:24 +0100 Subject: [PATCH 04/10] Amend translations --- app/views/legislation/annotations/_form.html.erb | 11 ++--------- config/locales/activerecord.en.yml | 6 ++++-- config/locales/activerecord.es.yml | 6 ++++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/views/legislation/annotations/_form.html.erb b/app/views/legislation/annotations/_form.html.erb index 81605e648..5662cafe2 100644 --- a/app/views/legislation/annotations/_form.html.erb +++ b/app/views/legislation/annotations/_form.html.erb @@ -8,24 +8,17 @@
- <%= form_for [@process, @draft_version, Annotation.new], url: legislation_process_draft_version_annotations_path(@process, @draft_version), remote: true do |f| %> + <%= form_for Legislation::Annotation.new, url: legislation_process_draft_version_annotations_path(@process, @draft_version), remote: true do |f| %> <%= f.text_area :text, autofocus: true %>
- <%= t('legislation.annotations.comments.cancel') %> + <%= t('legislation.annotations.comments.cancel') %> <%= f.submit t('legislation.annotations.comments.publish_comment'), class: 'button strong publish-comment' %> - <% if false %> - <%= t('legislation.annotations.comments.publish_comment') %> - <% end %>
<%= f.hidden_field :quote %> <%= f.hidden_field :ranges %> <% end %>
- -
diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index e873abc26..51ee2190b 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -139,10 +139,12 @@ en: status: Status final_version: Final version legislation/question: - title: "Title" + title: Title question_options: Options legislation/question_option: - value: "Value" + value: Value + legislation/annotation: + text: Comment errors: models: user: diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index e8f2a42eb..aa7e05875 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -134,10 +134,12 @@ es: status: Estado final_version: Versión final legislation/question: - title: "Título" + title: Título question_options: Respuestas legislation/question_option: - value: "Valor" + value: Valor + legislation/annotation: + text: Comentario errors: models: user: From d253c4c09ffbf8ec81ca9feae2953a980a5a329f Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Sat, 14 Jan 2017 10:17:21 +0100 Subject: [PATCH 05/10] Restore custom position --- app/assets/javascripts/application.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 19c081b54..939705c51 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -53,8 +53,8 @@ //= require cocoon //= require legislation //= require legislation_allegations -//= require custom //= require legislation_annotatable +//= require custom var initialize_modules = function() { App.Comments.initialize(); From f47cef5a93d8be448774d3da5dadbfa829c4008d Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Sat, 14 Jan 2017 12:52:14 +0100 Subject: [PATCH 06/10] Highlight text when creating a comment --- .../legislation_annotatable.js.coffee | 62 ++++++++++++++++++- .../legislation/annotations_controller.rb | 9 ++- .../legislation/annotations/_form.html.erb | 2 +- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/legislation_annotatable.js.coffee b/app/assets/javascripts/legislation_annotatable.js.coffee index 343c638ad..f1e2d50d2 100644 --- a/app/assets/javascripts/legislation_annotatable.js.coffee +++ b/app/assets/javascripts/legislation_annotatable.js.coffee @@ -2,6 +2,42 @@ _t = (key) -> new Gettext().gettext(key) App.LegislationAnnotatable = + makeEditableAndHighlight: (colour) -> + sel = window.getSelection() + if sel.rangeCount and sel.getRangeAt + range = sel.getRangeAt(0) + document.designMode = 'on' + if range + sel.removeAllRanges() + sel.addRange range + # Use HiliteColor since some browsers apply BackColor to the whole block + if !document.execCommand('HiliteColor', false, colour) + document.execCommand 'BackColor', false, colour + document.designMode = 'off' + return + + highlight: (colour) -> + range = undefined + sel = undefined + if window.getSelection + # IE9 and non-IE + try + if !document.execCommand('BackColor', false, colour) + App.LegislationAnnotatable.makeEditableAndHighlight colour + catch ex + App.LegislationAnnotatable.makeEditableAndHighlight colour + else if document.selection and document.selection.createRange + # IE <= 8 case + range = document.selection.createRange() + range.execCommand 'BackColor', false, colour + return + + remove_highlight: -> + $('[data-legislation-draft-version-id] span[style]').replaceWith(-> + return $(this).contents() + ) + return + renderAnnotationComments: (event) -> $('.comment-box').offset(top: event.offset) $.ajax @@ -30,14 +66,29 @@ App.LegislationAnnotatable = # Clean comments section and open it $('#comments-box').html '' App.LegislationAllegations.show_comments() + $('#comments-box').show() annotation_url = $('[data-legislation-annotatable-base-url]').data('legislation-annotatable-base-url') $.ajax( method: 'GET' url: annotation_url + '/annotations/new' dataType: 'script').done (-> - $('#new_annotation #annotation_quote').val(@annotation.quote) - $('#new_annotation #annotation_ranges').val(JSON.stringify(@annotation.ranges)) + $('#new_legislation_annotation #legislation_annotation_quote').val(@annotation.quote) + $('#new_legislation_annotation #legislation_annotation_ranges').val(JSON.stringify(@annotation.ranges)) + $('#comments-box').css({top: $('.annotator-adder').position().top}) + + App.LegislationAnnotatable.highlight('#7fff9a') + $('#comments-box textarea').focus() + + $("#new_legislation_annotation").on("ajax:complete", (e, data, status, xhr) -> + App.LegislationAnnotatable.remove_highlight() + $("#comments-box").html("").hide(); + return true + ).on("ajax:error", (e, data, status, xhr) -> + console.log(data) + return false + ) + return ).bind(this) editorExtension: (editor) -> @@ -59,6 +110,13 @@ App.LegislationAnnotatable = initialize: -> $(document).on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments) $(document).on('click', '[data-annotation-id]', App.LegislationAnnotatable.onClick) + $(document).on('click', '[data-cancel-annotation]', (e) -> + e.preventDefault() + $('#comments-box').html('') + $('#comments-box').hide() + App.LegislationAnnotatable.remove_highlight() + return + ) current_user_id = $('html').data('current-user-id') if current_user_id == "" diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index 861eb9d14..664cd6252 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -31,7 +31,7 @@ class Legislation::AnnotationsController < ApplicationController track_event render json: @annotation.to_json else - render json: {}, status: :unprocessable_entity + render json: @annotation.errors.full_messages, status: :unprocessable_entity end end @@ -51,12 +51,11 @@ class Legislation::AnnotationsController < ApplicationController end end - private def annotation_params params - .require(:annotation) + .require(:legislation_annotation) .permit(:quote, :text, ranges: [:start, :startOffset, :end, :endOffset]) end @@ -67,8 +66,8 @@ class Legislation::AnnotationsController < ApplicationController end def convert_ranges_parameters - if params[:annotation] && params[:annotation][:ranges] - params[:annotation][:ranges] = JSON.parse(params[:annotation][:ranges]) + if params[:legislation_annotation] && params[:legislation_annotation][:ranges] + params[:legislation_annotation][:ranges] = JSON.parse(params[:legislation_annotation][:ranges]) end rescue JSON::ParserError end diff --git a/app/views/legislation/annotations/_form.html.erb b/app/views/legislation/annotations/_form.html.erb index 5662cafe2..7fa52cd5a 100644 --- a/app/views/legislation/annotations/_form.html.erb +++ b/app/views/legislation/annotations/_form.html.erb @@ -9,7 +9,7 @@
<%= form_for Legislation::Annotation.new, url: legislation_process_draft_version_annotations_path(@process, @draft_version), remote: true do |f| %> - <%= f.text_area :text, autofocus: true %> + <%= f.text_area :text %>
<%= t('legislation.annotations.comments.cancel') %> From 046c021c0c40b489b2afaff72fa629dc8b3dfbfd Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Sat, 14 Jan 2017 16:06:45 +0100 Subject: [PATCH 07/10] Complete create comment details --- .../legislation_annotatable.js.coffee | 27 ++++++++++--------- .../legislation/annotations/_form.html.erb | 24 +++++++++++------ .../legislation/annotations/comments.js.erb | 2 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/legislation_annotatable.js.coffee b/app/assets/javascripts/legislation_annotatable.js.coffee index f1e2d50d2..4baa1a725 100644 --- a/app/assets/javascripts/legislation_annotatable.js.coffee +++ b/app/assets/javascripts/legislation_annotatable.js.coffee @@ -17,8 +17,6 @@ App.LegislationAnnotatable = return highlight: (colour) -> - range = undefined - sel = undefined if window.getSelection # IE9 and non-IE try @@ -39,7 +37,7 @@ App.LegislationAnnotatable = return renderAnnotationComments: (event) -> - $('.comment-box').offset(top: event.offset) + $('#comments-box').css({top: event.offset - $('.calc-comments').offset().top}) $.ajax method: "GET" url: event.annotation_url + "/annotations/" + event.annotation_id + "/comments" @@ -75,18 +73,23 @@ App.LegislationAnnotatable = dataType: 'script').done (-> $('#new_legislation_annotation #legislation_annotation_quote').val(@annotation.quote) $('#new_legislation_annotation #legislation_annotation_ranges').val(JSON.stringify(@annotation.ranges)) - $('#comments-box').css({top: $('.annotator-adder').position().top}) + $('#comments-box').css({top: position.top - $('.calc-comments').offset().top}) App.LegislationAnnotatable.highlight('#7fff9a') $('#comments-box textarea').focus() $("#new_legislation_annotation").on("ajax:complete", (e, data, status, xhr) -> - App.LegislationAnnotatable.remove_highlight() - $("#comments-box").html("").hide(); + if data.status == 200 + App.LegislationAnnotatable.remove_highlight() + $("#comments-box").html("").hide() + $.ajax + method: "GET" + url: annotation_url + "/annotations/" + data.responseJSON.id + "/comments" + dataType: 'script' + else + $(e.target).find('label').addClass('error') + $('' + data.responseJSON[0] + '').insertAfter($(e.target).find('textarea')) return true - ).on("ajax:error", (e, data, status, xhr) -> - console.log(data) - return false ) return ).bind(this) @@ -108,9 +111,9 @@ App.LegislationAnnotatable = offset: el.offset()["top"] initialize: -> - $(document).on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments) - $(document).on('click', '[data-annotation-id]', App.LegislationAnnotatable.onClick) - $(document).on('click', '[data-cancel-annotation]', (e) -> + $(document).off("renderLegislationAnnotation").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments) + $(document).off('click', '[data-annotation-id]').on('click', '[data-annotation-id]', App.LegislationAnnotatable.onClick) + $(document).off('click', '[data-cancel-annotation]').on('click', '[data-cancel-annotation]', (e) -> e.preventDefault() $('#comments-box').html('') $('#comments-box').hide() diff --git a/app/views/legislation/annotations/_form.html.erb b/app/views/legislation/annotations/_form.html.erb index 7fa52cd5a..317a712c8 100644 --- a/app/views/legislation/annotations/_form.html.erb +++ b/app/views/legislation/annotations/_form.html.erb @@ -8,16 +8,24 @@
- <%= form_for Legislation::Annotation.new, url: legislation_process_draft_version_annotations_path(@process, @draft_version), remote: true do |f| %> - <%= f.text_area :text %> + <% if user_signed_in? %> + <%= form_for Legislation::Annotation.new, url: legislation_process_draft_version_annotations_path(@process, @draft_version), remote: true do |f| %> + <%= f.text_area :text %> -
- <%= t('legislation.annotations.comments.cancel') %> - <%= f.submit t('legislation.annotations.comments.publish_comment'), class: 'button strong publish-comment' %> -
+
+ <%= t('legislation.annotations.comments.cancel') %> + <%= f.submit t('legislation.annotations.comments.publish_comment'), class: 'button strong publish-comment' %> +
- <%= f.hidden_field :quote %> - <%= f.hidden_field :ranges %> + <%= f.hidden_field :quote %> + <%= f.hidden_field :ranges %> + <% end %> + <% else %> +
+ <%= t("debates.show.login_to_comment", + signin: link_to(t("votes.signin"), new_user_session_path), + signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %> +
<% end %>
diff --git a/app/views/legislation/annotations/comments.js.erb b/app/views/legislation/annotations/comments.js.erb index 6ee83560e..75d8f35a0 100644 --- a/app/views/legislation/annotations/comments.js.erb +++ b/app/views/legislation/annotations/comments.js.erb @@ -1,2 +1,2 @@ -$("#comments-box").html("<%= j render('comments_box', annotation: @annotation) %>"); +$("#comments-box").html("<%= j render('comments_box', annotation: @annotation) %>").show(); From f1be212eeb052b5431c6abab53d4d4b9a1a3ead9 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Sat, 14 Jan 2017 16:56:55 +0100 Subject: [PATCH 08/10] Fix and complete controller specs --- .../legislation/annotations_controller.rb | 2 +- .../annotations_controller_spec.rb | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index 664cd6252..6f5069763 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -66,7 +66,7 @@ class Legislation::AnnotationsController < ApplicationController end def convert_ranges_parameters - if params[:legislation_annotation] && params[:legislation_annotation][:ranges] + if params[:legislation_annotation] && params[:legislation_annotation][:ranges] && params[:legislation_annotation][:ranges].is_a?(String) params[:legislation_annotation][:ranges] = JSON.parse(params[:legislation_annotation][:ranges]) end rescue JSON::ParserError diff --git a/spec/controllers/legislation/annotations_controller_spec.rb b/spec/controllers/legislation/annotations_controller_spec.rb index 2713fc273..4ffd91357 100644 --- a/spec/controllers/legislation/annotations_controller_spec.rb +++ b/spec/controllers/legislation/annotations_controller_spec.rb @@ -15,7 +15,7 @@ describe Legislation::AnnotationsController do post :create, process_id: @process.id, draft_version_id: @draft_version.id, - annotation: { + legislation_annotation: { "quote"=>"Ordenación Territorial", "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}], "text": "una anotacion" @@ -29,7 +29,7 @@ describe Legislation::AnnotationsController do post :create, process_id: @process.id, draft_version_id: @final_version.id, - annotation: { + legislation_annotation: { "quote"=>"Ordenación Territorial", "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}], "text": "una anotacion" @@ -44,7 +44,7 @@ describe Legislation::AnnotationsController do expect do xhr :post, :create, process_id: @process.id, draft_version_id: @draft_version.id, - annotation: { + legislation_annotation: { "quote"=>"Ordenación Territorial", "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}], "text": "una anotacion" @@ -59,12 +59,27 @@ describe Legislation::AnnotationsController do expect do xhr :post, :create, process_id: @process.id, draft_version_id: @draft_version.id, - annotation: { + legislation_annotation: { "quote"=>"Ordenación Territorial", "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}], "text": "una anotacion" } end.to_not change { @draft_version.annotations.count } end + + it 'should create an annotation by parsing parameters in JSON' do + sign_in @user + + expect do + xhr :post, :create, process_id: @process.id, + draft_version_id: @draft_version.id, + legislation_annotation: { + "quote"=>"Ordenación Territorial", + "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}].to_json, + "text": "una anotacion" + } + end.to change { @draft_version.annotations.count }.by(1) + end + end end From 819c9efcc3b15dc999ab2b10a3dd78c6c800d9bf Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Sat, 14 Jan 2017 17:19:27 +0100 Subject: [PATCH 09/10] Fix and complete feature specs --- .../legislation/draft_versions_spec.rb | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/spec/features/legislation/draft_versions_spec.rb b/spec/features/legislation/draft_versions_spec.rb index 6c39d11ce..d0f303743 100644 --- a/spec/features/legislation/draft_versions_spec.rb +++ b/spec/features/legislation/draft_versions_spec.rb @@ -135,6 +135,18 @@ feature 'Legislation Draft Versions' do let(:user) { create(:user) } background { login_as user } + scenario 'Visit as anonymous' do + logout + draft_version = create(:legislation_draft_version, :published, body: Faker::Lorem.paragraph) + + visit legislation_process_draft_version_path(draft_version.process, draft_version) + + page.find(:css, ".legislation-annotatable").double_click + page.find(:css, ".annotator-adder button").click + expect(page).to_not have_css('#legislation_annotation_text') + expect(page).to have_content "ou must Sign in or Sign up to leave a comment." + end + scenario 'Create' do draft_version = create(:legislation_draft_version, :published, body: Faker::Lorem.paragraph) @@ -142,8 +154,11 @@ feature 'Legislation Draft Versions' do page.find(:css, ".legislation-annotatable").double_click page.find(:css, ".annotator-adder button").click - fill_in 'annotator-field-0', with: 'this is my annotation' - page.find(:css, ".annotator-controls a[href='#save']").click + page.click_button "Publish Comment" + expect(page).to have_content "Comment can't be blank" + + fill_in 'legislation_annotation_text', with: 'this is my annotation' + page.click_button "Publish Comment" expect(page).to have_css ".annotator-hl" first(:css, ".annotator-hl").click @@ -167,7 +182,7 @@ feature 'Legislation Draft Versions' do first(:css, ".annotator-hl").click expect(page).to have_content "my annotation" - all(".annotator-hl")[1].trigger('mouseover') + all(".annotator-hl")[1].trigger('click') expect(page).to have_content "my other annotation" end end From 65fcd83ee59f17da8da9f97407f0ee4dcaa7073b Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Mon, 16 Jan 2017 17:42:32 +0100 Subject: [PATCH 10/10] =?UTF-8?q?Don=E2=80=99t=20show=20new=20annotation/c?= =?UTF-8?q?omment=20form=20if=20allegations=20phase=20is=20not=20open?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/custom.js | 3 ++- .../javascripts/legislation_annotatable.js.coffee | 15 ++------------- app/views/legislation/annotations/_form.html.erb | 14 +++++++++----- .../legislation/draft_versions/show.html.erb | 1 + config/locales/en.yml | 5 +++++ config/locales/es.yml | 5 +++++ 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/custom.js b/app/assets/javascripts/custom.js index 50903021a..6c880b3a9 100644 --- a/app/assets/javascripts/custom.js +++ b/app/assets/javascripts/custom.js @@ -1,6 +1,7 @@ // Overrides and adds customized javascripts in this file -// Read more on documentation: +// Read more on documentation: // * English: https://github.com/consul/consul/blob/master/CUSTOMIZE_EN.md#javascript // * Spanish: https://github.com/consul/consul/blob/master/CUSTOMIZE_ES.md#javascript // +// diff --git a/app/assets/javascripts/legislation_annotatable.js.coffee b/app/assets/javascripts/legislation_annotatable.js.coffee index 4baa1a725..cc238835e 100644 --- a/app/assets/javascripts/legislation_annotatable.js.coffee +++ b/app/assets/javascripts/legislation_annotatable.js.coffee @@ -122,17 +122,6 @@ App.LegislationAnnotatable = ) current_user_id = $('html').data('current-user-id') - if current_user_id == "" - annotator.ui.editor.Editor.template = [ - '
', - '
', - ' ' + _t('Unregistered'), - '
', - ' ' + _t('Cancel') + '', - '
', - '
', - '
' - ].join('\n') $(".legislation-annotatable").each -> $this = $(this) @@ -146,8 +135,8 @@ App.LegislationAnnotatable = ann["legislation_draft_version_id"] = ann_id ann.permissions = ann.permissions || {} ann.permissions.admin = [] - .include(annotator.ui.main, { - element: this, + .include(annotator.ui.main, { + element: this, viewerExtensions: [App.LegislationAnnotatable.viewerExtension], editorExtensions: [App.LegislationAnnotatable.editorExtension] }) diff --git a/app/views/legislation/annotations/_form.html.erb b/app/views/legislation/annotations/_form.html.erb index 317a712c8..a757b4612 100644 --- a/app/views/legislation/annotations/_form.html.erb +++ b/app/views/legislation/annotations/_form.html.erb @@ -8,7 +8,11 @@
- <% if user_signed_in? %> + <% if !@process.open_phase?(:allegations) %> +
+ <%= t("legislation.annotations.form.phase_not_open") %> +
+ <% elsif user_signed_in? %> <%= form_for Legislation::Annotation.new, url: legislation_process_draft_version_annotations_path(@process, @draft_version), remote: true do |f| %> <%= f.text_area :text %> @@ -22,10 +26,10 @@ <% end %> <% else %>
- <%= t("debates.show.login_to_comment", - signin: link_to(t("votes.signin"), new_user_session_path), - signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %> -
+ <%= t("legislation.annotations.form.login_to_comment", + signin: link_to(t("legislation.annotations.form.signin"), new_user_session_path), + signup: link_to(t("legislation.annotations.form.signup"), new_user_registration_path)).html_safe %> +
<% end %>
diff --git a/app/views/legislation/draft_versions/show.html.erb b/app/views/legislation/draft_versions/show.html.erb index d89926929..615a86fc8 100644 --- a/app/views/legislation/draft_versions/show.html.erb +++ b/app/views/legislation/draft_versions/show.html.erb @@ -54,6 +54,7 @@
<% end %> <%= @draft_version.body_html.html_safe %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 09eac149c..d65c3b007 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -237,6 +237,11 @@ en: other: "%{count} replies" cancel: Cancel publish_comment: Publish Comment + form: + phase_not_open: This phase is not open + login_to_comment: You must %{signin} or %{signup} to leave a comment. + signin: Sign in + signup: Sign up index: title: Comments comments_about: Comments about diff --git a/config/locales/es.yml b/config/locales/es.yml index 1a9626cc6..708a74ed2 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -237,6 +237,11 @@ es: other: "%{count} respuestas" cancel: Cancelar publish_comment: Publicar Comentario + form: + phase_not_open: Esta fase del proceso no está abierta + login_to_comment: Necesitas %{signin} o %{signup} para comentar. + signin: iniciar sesión + signup: registrarte index: title: Comentarios see_in_context: Ver en contexto