From 8f112cf37ed57a5accb8f60fdf2d5e6b0816c0fd Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Thu, 24 Jan 2019 10:58:24 +0100 Subject: [PATCH 1/3] Allow admins delete poll answer documents --- app/models/abilities/administrator.rb | 1 + .../poll/questions/answers/documents.html.erb | 26 +++++------ .../answers/documents/documents_spec.rb | 44 +++++++++++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 spec/features/admin/poll/questions/answers/documents/documents_spec.rb diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index bdc457bc0..847f1effc 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -93,6 +93,7 @@ module Abilities cannot :comment_as_moderator, [::Legislation::Question, Legislation::Annotation, ::Legislation::Proposal] can [:create], Document + can [:destroy], Document, documentable_type: "Poll::Question::Answer" can [:create, :destroy], DirectUpload can [:deliver], Newsletter, hidden_at: nil diff --git a/app/views/admin/poll/questions/answers/documents.html.erb b/app/views/admin/poll/questions/answers/documents.html.erb index 54dd25a18..933be2b94 100644 --- a/app/views/admin/poll/questions/answers/documents.html.erb +++ b/app/views/admin/poll/questions/answers/documents.html.erb @@ -14,18 +14,12 @@ <%= render 'shared/errors', resource: @answer %> -
-
-
- <%= render 'documents/nested_documents', documentable: @answer, f: f %> -
+
+ <%= render 'documents/nested_documents', documentable: @answer, f: f %> +
-
-
- <%= f.submit(class: "button expanded", value: t("shared.save")) %> -
-
-
+
+ <%= f.submit(class: "button expanded", value: t("shared.save")) %>
<% end %> @@ -42,11 +36,17 @@ <%= link_to document.title, document.attachment.url %> - <%= link_to t('documents.buttons.download_document'), + <%= link_to t("documents.buttons.download_document"), document.attachment.url, target: "_blank", rel: "nofollow", - class: 'button hollow' %> + class: "button hollow" %> + + <%= link_to t("admin.shared.delete"), + document_path(document), + method: :delete, + class: "button hollow alert", + data: { confirm: t("admin.actions.confirm") } %> <% end %> diff --git a/spec/features/admin/poll/questions/answers/documents/documents_spec.rb b/spec/features/admin/poll/questions/answers/documents/documents_spec.rb new file mode 100644 index 000000000..34d43a454 --- /dev/null +++ b/spec/features/admin/poll/questions/answers/documents/documents_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +feature "Documents" do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + context "Index" do + scenario "Answer with no documents" do + answer = create(:poll_question_answer, question: create(:poll_question)) + document = create(:document) + + visit admin_answer_documents_path(answer) + + expect(page).not_to have_content(document.title) + end + + scenario "Answer with documents" do + answer = create(:poll_question_answer, question: create(:poll_question)) + document = create(:document, documentable: answer) + + visit admin_answer_documents_path(answer) + + expect(page).to have_content(document.title) + end + end + + scenario "Remove document from answer", :js do + answer = create(:poll_question_answer, question: create(:poll_question)) + document = create(:document, documentable: answer) + + visit admin_answer_documents_path(answer) + expect(page).to have_content(document.title) + + accept_confirm "Are you sure?" do + click_link "Delete" + end + + expect(page).not_to have_content(document.title) + end + +end From b588cd3a0b81c1ea0a380d7b0ed51796e80438cf Mon Sep 17 00:00:00 2001 From: decabeza Date: Thu, 24 Jan 2019 21:39:08 +0100 Subject: [PATCH 2/3] Add missing i18n --- config/locales/en/admin.yml | 1 + config/locales/es/admin.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index a86f8a315..0b51d9556 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -1187,6 +1187,7 @@ en: author: Author content: Content created_at: Created at + delete: Delete spending_proposals: index: geozone_filter_all: All zones diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index d33d91211..dc8e66fb0 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -1187,6 +1187,7 @@ es: author: Autor content: Contenido created_at: Fecha de creación + delete: Eliminar spending_proposals: index: geozone_filter_all: Todos los ámbitos de actuación From dacc2d529dd96ffa0b55644eb4ad3b9cb01bda11 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 9 May 2018 18:59:35 +0200 Subject: [PATCH 3/3] Fix destroy document specs We were linking to the document url itself, which does not have a route associated and so the specs fails With this commit we are using the correct path to the destroy action of the DocumentsController. We are also using the referrer instead of a params[:from] attribute, as it avoids having to pass an extra parameter, making the code prettier and it works the same way --- app/controllers/documents_controller.rb | 2 +- app/views/documents/_document.html.erb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 001d23446..1d6df8d14 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -11,7 +11,7 @@ class DocumentsController < ApplicationController else flash[:alert] = t "documents.actions.destroy.alert" end - redirect_to params[:from] + redirect_to request.referer end format.js do if @document.destroy diff --git a/app/views/documents/_document.html.erb b/app/views/documents/_document.html.erb index bf549e92b..9075bdd7c 100644 --- a/app/views/documents/_document.html.erb +++ b/app/views/documents/_document.html.erb @@ -13,7 +13,8 @@ <% if can?(:destroy, document) %>
<%= link_to t("documents.buttons.destroy_document"), - document_path(document, from: request.url), method: :delete, + document, + method: :delete, data: { confirm: t("documents.actions.destroy.confirm") }, class: "delete" %> <% end %>