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:
@@ -3,16 +3,13 @@
|
||||
<h2><%= t("admin.questions.show.answers.documents_list") %></h2>
|
||||
|
||||
<ul class="breadcrumbs">
|
||||
<li><%= @answer.question.title %></li>
|
||||
<li><%= @answer.title %></li>
|
||||
<li><%= answer.question.title %></li>
|
||||
<li><%= answer.title %></li>
|
||||
</ul>
|
||||
|
||||
<div class="poll-question-form">
|
||||
<%= 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 %>
|
||||
|
||||
<div class="documents">
|
||||
<%= render "documents/nested_documents", f: f %>
|
||||
@@ -23,14 +20,14 @@
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% if @answer.documents.present? %>
|
||||
<% if answer.documents.present? %>
|
||||
<table>
|
||||
<tr>
|
||||
<th scope="col"><%= t("admin.questions.show.answers.document_title") %></th>
|
||||
<th scope="col"><%= t("admin.questions.show.answers.document_actions") %></th>
|
||||
</tr>
|
||||
|
||||
<% @answer.documents.each do |document| %>
|
||||
<% answer.documents.each do |document| %>
|
||||
<tr>
|
||||
<td>
|
||||
<%= link_to document.title, document.attachment %>
|
||||
@@ -0,0 +1,7 @@
|
||||
class Admin::Poll::Questions::Answers::Documents::IndexComponent < ApplicationComponent
|
||||
attr_reader :answer
|
||||
|
||||
def initialize(answer)
|
||||
@answer = answer
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<%= render Admin::Poll::Questions::Answers::Documents::IndexComponent.new(@answer) %>
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user