diff --git a/app/components/admin/poll/questions/answers/documents/index_component.html.erb b/app/components/admin/poll/questions/answers/documents/index_component.html.erb index 9f34e68e9..6db626293 100644 --- a/app/components/admin/poll/questions/answers/documents/index_component.html.erb +++ b/app/components/admin/poll/questions/answers/documents/index_component.html.erb @@ -26,14 +26,14 @@ <% end %> - <% if answer.documents.present? %> + <% if documents.present? %> - <% answer.documents.each do |document| %> + <% 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 index 5a46f05da..b45c9856e 100644 --- a/app/components/admin/poll/questions/answers/documents/index_component.rb +++ b/app/components/admin/poll/questions/answers/documents/index_component.rb @@ -5,4 +5,10 @@ class Admin::Poll::Questions::Answers::Documents::IndexComponent < ApplicationCo def initialize(answer) @answer = answer end + + private + + def documents + @documents ||= @answer.class.find(@answer.id).documents + end end diff --git a/app/controllers/admin/poll/questions/answers/documents_controller.rb b/app/controllers/admin/poll/questions/answers/documents_controller.rb index eeea65aa6..4fce04d05 100644 --- a/app/controllers/admin/poll/questions/answers/documents_controller.rb +++ b/app/controllers/admin/poll/questions/answers/documents_controller.rb @@ -14,7 +14,7 @@ class Admin::Poll::Questions::Answers::DocumentsController < Admin::Poll::BaseCo redirect_to admin_answer_documents_path(@answer), notice: t("admin.documents.create.success_notice") else - render :new + render :index end 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 d4439e5bb..cc6f55bc9 100644 --- a/spec/system/admin/poll/questions/answers/documents/documents_spec.rb +++ b/spec/system/admin/poll/questions/answers/documents/documents_spec.rb @@ -24,16 +24,31 @@ describe "Documents", :admin do end end - scenario "Create document for answer" do - answer = create(:poll_question_answer, poll: future_poll) + describe "Create document for answer" do + scenario "with valid data" do + answer = create(:poll_question_answer, poll: future_poll) - visit admin_answer_documents_path(answer) + visit admin_answer_documents_path(answer) - documentable_attach_new_file(Rails.root.join("spec/fixtures/files/clippy.pdf")) - click_button "Save" + documentable_attach_new_file(Rails.root.join("spec/fixtures/files/clippy.pdf")) + click_button "Save" - expect(page).to have_content "Document uploaded successfully" - expect(page).to have_link "clippy.pdf" + expect(page).to have_content "Document uploaded successfully" + expect(page).to have_link "clippy.pdf" + end + + scenario "with invalid data" do + answer = create(:poll_question_answer, poll: future_poll) + + visit admin_answer_documents_path(answer) + + documentable_attach_new_file(Rails.root.join("spec/fixtures/files/clippy.pdf")) + fill_in "Title", with: "" + click_button "Save" + + expect(page).to have_content "1 error prevented this Answer from being saved" + expect(page).to have_content "Documents list" + end end scenario "Remove document from answer" do