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.
This commit is contained in:
taitus
2022-09-14 09:25:17 +02:00
parent cb2958e1b0
commit ec861ca8e6
7 changed files with 58 additions and 21 deletions

View File

@@ -3,16 +3,13 @@
<h2><%= t("admin.questions.show.answers.documents_list") %></h2> <h2><%= t("admin.questions.show.answers.documents_list") %></h2>
<ul class="breadcrumbs"> <ul class="breadcrumbs">
<li><%= @answer.question.title %></li> <li><%= answer.question.title %></li>
<li><%= @answer.title %></li> <li><%= answer.title %></li>
</ul> </ul>
<div class="poll-question-form"> <div class="poll-question-form">
<%= form_for(Poll::Question::Answer.new, <%= form_for(Poll::Question::Answer.new, url: admin_answer_documents_path(answer)) do |f| %>
url: admin_question_answer_path(@answer.question, @answer), <%= render "shared/errors", resource: answer %>
method: :put) do |f| %>
<%= render "shared/errors", resource: @answer %>
<div class="documents"> <div class="documents">
<%= render "documents/nested_documents", f: f %> <%= render "documents/nested_documents", f: f %>
@@ -23,14 +20,14 @@
</div> </div>
<% end %> <% end %>
<% if @answer.documents.present? %> <% if answer.documents.present? %>
<table> <table>
<tr> <tr>
<th scope="col"><%= t("admin.questions.show.answers.document_title") %></th> <th scope="col"><%= t("admin.questions.show.answers.document_title") %></th>
<th scope="col"><%= t("admin.questions.show.answers.document_actions") %></th> <th scope="col"><%= t("admin.questions.show.answers.document_actions") %></th>
</tr> </tr>
<% @answer.documents.each do |document| %> <% answer.documents.each do |document| %>
<tr> <tr>
<td> <td>
<%= link_to document.title, document.attachment %> <%= link_to document.title, document.attachment %>

View File

@@ -0,0 +1,7 @@
class Admin::Poll::Questions::Answers::Documents::IndexComponent < ApplicationComponent
attr_reader :answer
def initialize(answer)
@answer = answer
end
end

View File

@@ -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

View File

@@ -1,12 +1,10 @@
class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
include Translatable include Translatable
include DocumentAttributes
load_and_authorize_resource :question, class: "::Poll::Question" load_and_authorize_resource :question, class: "::Poll::Question"
load_and_authorize_resource class: "::Poll::Question::Answer", load_and_authorize_resource class: "::Poll::Question::Answer",
through: :question, through: :question,
through_association: :question_answers, through_association: :question_answers
except: :documents
def new def new
end end
@@ -35,13 +33,6 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end end
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 def order_answers
::Poll::Question::Answer.order_answers(params[:ordered_list]) ::Poll::Question::Answer.order_answers(params[:ordered_list])
head :ok head :ok
@@ -54,7 +45,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end end
def allowed_params def allowed_params
attributes = [:title, :description, :given_order, documents_attributes: document_attributes] attributes = [:title, :description, :given_order]
[*attributes, translation_params(Poll::Question::Answer)] [*attributes, translation_params(Poll::Question::Answer)]
end end

View File

@@ -0,0 +1 @@
<%= render Admin::Poll::Questions::Answers::Documents::IndexComponent.new(@answer) %>

View File

@@ -173,7 +173,7 @@ namespace :admin do
resources :answers, only: [], controller: "questions/answers" do resources :answers, only: [], controller: "questions/answers" do
resources :images, controller: "questions/answers/images" resources :images, controller: "questions/answers/images"
resources :videos, controller: "questions/answers/videos" resources :videos, controller: "questions/answers/videos"
get :documents, to: "questions/answers#documents" resources :documents, only: [:index, :create], controller: "questions/answers/documents"
end end
post "/answers/order_answers", to: "questions/answers#order_answers" post "/answers/order_answers", to: "questions/answers#order_answers"
end end

View File

@@ -21,6 +21,18 @@ describe "Documents", :admin do
end end
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 scenario "Remove document from answer" do
answer = create(:poll_question_answer) answer = create(:poll_question_answer)
document = create(:document, documentable: answer) document = create(:document, documentable: answer)