From ec861ca8e67e3a257635a0280acea86e9f043994 Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 14 Sep 2022 09:25:17 +0200 Subject: [PATCH] Extract documents action from Answer controller This way we have a controller just to manage Poll::Question::Answer related documents in the same way we have for videos and images. --- .../documents/index_component.html.erb} | 15 ++++------ .../answers/documents/index_component.rb | 7 +++++ .../questions/answers/documents_controller.rb | 29 +++++++++++++++++++ .../poll/questions/answers_controller.rb | 13 ++------- .../answers/documents/index.html.erb | 1 + config/routes/admin.rb | 2 +- .../answers/documents/documents_spec.rb | 12 ++++++++ 7 files changed, 58 insertions(+), 21 deletions(-) rename app/{views/admin/poll/questions/answers/documents.html.erb => components/admin/poll/questions/answers/documents/index_component.html.erb} (77%) create mode 100644 app/components/admin/poll/questions/answers/documents/index_component.rb create mode 100644 app/controllers/admin/poll/questions/answers/documents_controller.rb create mode 100644 app/views/admin/poll/questions/answers/documents/index.html.erb diff --git a/app/views/admin/poll/questions/answers/documents.html.erb b/app/components/admin/poll/questions/answers/documents/index_component.html.erb similarity index 77% rename from app/views/admin/poll/questions/answers/documents.html.erb rename to app/components/admin/poll/questions/answers/documents/index_component.html.erb index 7a94fd8e6..729d718e3 100644 --- a/app/views/admin/poll/questions/answers/documents.html.erb +++ b/app/components/admin/poll/questions/answers/documents/index_component.html.erb @@ -3,16 +3,13 @@

<%= t("admin.questions.show.answers.documents_list") %>

- <%= form_for(Poll::Question::Answer.new, - url: admin_question_answer_path(@answer.question, @answer), - method: :put) do |f| %> - - <%= render "shared/errors", resource: @answer %> + <%= form_for(Poll::Question::Answer.new, url: admin_answer_documents_path(answer)) do |f| %> + <%= render "shared/errors", resource: answer %>
<%= render "documents/nested_documents", f: f %> @@ -23,14 +20,14 @@
<% end %> - <% if @answer.documents.present? %> + <% if answer.documents.present? %> - <% @answer.documents.each do |document| %> + <% answer.documents.each do |document| %>
<%= t("admin.questions.show.answers.document_title") %> <%= t("admin.questions.show.answers.document_actions") %>
<%= link_to document.title, document.attachment %> diff --git a/app/components/admin/poll/questions/answers/documents/index_component.rb b/app/components/admin/poll/questions/answers/documents/index_component.rb new file mode 100644 index 000000000..ca72d4f98 --- /dev/null +++ b/app/components/admin/poll/questions/answers/documents/index_component.rb @@ -0,0 +1,7 @@ +class Admin::Poll::Questions::Answers::Documents::IndexComponent < ApplicationComponent + attr_reader :answer + + def initialize(answer) + @answer = answer + end +end diff --git a/app/controllers/admin/poll/questions/answers/documents_controller.rb b/app/controllers/admin/poll/questions/answers/documents_controller.rb new file mode 100644 index 000000000..3210896d6 --- /dev/null +++ b/app/controllers/admin/poll/questions/answers/documents_controller.rb @@ -0,0 +1,29 @@ +class Admin::Poll::Questions::Answers::DocumentsController < Admin::Poll::BaseController + include DocumentAttributes + + load_and_authorize_resource :answer, class: "::Poll::Question::Answer" + + def index + end + + def create + @answer.attributes = documents_params + + if @answer.save + redirect_to admin_answer_documents_path(@answer), + notice: t("admin.documents.create.success_notice") + else + render :new + end + end + + private + + def documents_params + params.require(:poll_question_answer).permit(allowed_params) + end + + def allowed_params + [documents_attributes: document_attributes] + end +end diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 77e48e46e..9dbeeab1a 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -1,12 +1,10 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController include Translatable - include DocumentAttributes load_and_authorize_resource :question, class: "::Poll::Question" load_and_authorize_resource class: "::Poll::Question::Answer", through: :question, - through_association: :question_answers, - except: :documents + through_association: :question_answers def new end @@ -35,13 +33,6 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController end end - def documents - @answer = ::Poll::Question::Answer.find(params[:answer_id]) - @documents = @answer.documents - - render "admin/poll/questions/answers/documents" - end - def order_answers ::Poll::Question::Answer.order_answers(params[:ordered_list]) head :ok @@ -54,7 +45,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController end def allowed_params - attributes = [:title, :description, :given_order, documents_attributes: document_attributes] + attributes = [:title, :description, :given_order] [*attributes, translation_params(Poll::Question::Answer)] end diff --git a/app/views/admin/poll/questions/answers/documents/index.html.erb b/app/views/admin/poll/questions/answers/documents/index.html.erb new file mode 100644 index 000000000..037ab68f2 --- /dev/null +++ b/app/views/admin/poll/questions/answers/documents/index.html.erb @@ -0,0 +1 @@ +<%= render Admin::Poll::Questions::Answers::Documents::IndexComponent.new(@answer) %> diff --git a/config/routes/admin.rb b/config/routes/admin.rb index dde11dbc9..f61ab41a8 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -173,7 +173,7 @@ namespace :admin do resources :answers, only: [], controller: "questions/answers" do resources :images, controller: "questions/answers/images" resources :videos, controller: "questions/answers/videos" - get :documents, to: "questions/answers#documents" + resources :documents, only: [:index, :create], controller: "questions/answers/documents" end post "/answers/order_answers", to: "questions/answers#order_answers" end diff --git a/spec/system/admin/poll/questions/answers/documents/documents_spec.rb b/spec/system/admin/poll/questions/answers/documents/documents_spec.rb index c64b52bb4..5ab6f11c0 100644 --- a/spec/system/admin/poll/questions/answers/documents/documents_spec.rb +++ b/spec/system/admin/poll/questions/answers/documents/documents_spec.rb @@ -21,6 +21,18 @@ describe "Documents", :admin do end end + scenario "Create document for answer" do + answer = create(:poll_question_answer) + + visit admin_answer_documents_path(answer) + + documentable_attach_new_file(Rails.root.join("spec/fixtures/files/clippy.pdf")) + click_button "Save" + + expect(page).to have_content "Document uploaded succesfully" + expect(page).to have_link "clippy.pdf" + end + scenario "Remove document from answer" do answer = create(:poll_question_answer) document = create(:document, documentable: answer)