Load answer through question in answers controller

We are simplifying the load answer and we can remove the ambiguous
hidden field from answer form.
This commit is contained in:
taitus
2022-09-09 15:56:36 +02:00
parent 01005b50cb
commit 405b37f605
9 changed files with 14 additions and 20 deletions

View File

@@ -2,17 +2,16 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
include Translatable include Translatable
include DocumentAttributes include DocumentAttributes
before_action :load_answer, only: [:show, :edit, :update, :documents]
load_and_authorize_resource :question, class: "::Poll::Question" load_and_authorize_resource :question, class: "::Poll::Question"
load_resource class: "::Poll::Question::Answer",
through: :question,
through_association: :question_answers,
except: :documents
def new def new
@answer = @question.answers.new
end end
def create def create
@answer = @question.answers.new(answer_params)
if @answer.save if @answer.save
redirect_to admin_question_path(@question), redirect_to admin_question_path(@question),
notice: t("flash.actions.create.poll_question_answer") notice: t("flash.actions.create.poll_question_answer")
@@ -37,6 +36,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end end
def documents def documents
@answer = ::Poll::Question::Answer.find(params[:answer_id])
@documents = @answer.documents @documents = @answer.documents
render "admin/poll/questions/answers/documents" render "admin/poll/questions/answers/documents"
@@ -54,16 +54,11 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end end
def allowed_params def allowed_params
attributes = [:title, :description, :given_order, :question_id, attributes = [:title, :description, :given_order, documents_attributes: document_attributes]
documents_attributes: document_attributes]
[*attributes, translation_params(Poll::Question::Answer)] [*attributes, translation_params(Poll::Question::Answer)]
end end
def load_answer
@answer = ::Poll::Question::Answer.find(params[:id] || params[:answer_id])
end
def resource def resource
load_answer unless @answer load_answer unless @answer
@answer @answer

View File

@@ -7,8 +7,6 @@
<%= f.hidden_field :given_order, <%= f.hidden_field :given_order,
value: @answer.persisted? ? @answer.given_order : @answer.class.last_position(@answer.question_id || @question.id) + 1 %> value: @answer.persisted? ? @answer.given_order : @answer.class.last_position(@answer.question_id || @question.id) + 1 %>
<%= f.hidden_field :question_id, value: @answer.question_id || @question.id %>
<div class="row"> <div class="row">
<%= f.translatable_fields do |translations_form| %> <%= f.translatable_fields do |translations_form| %>
<div class="column end"> <div class="column end">

View File

@@ -9,7 +9,7 @@
<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_path(@answer), url: admin_question_answer_path(@answer.question, @answer),
method: :put) do |f| %> method: :put) do |f| %>
<%= render "shared/errors", resource: @answer %> <%= render "shared/errors", resource: @answer %>

View File

@@ -10,5 +10,5 @@
</h2> </h2>
<div class="poll-question-answer-form"> <div class="poll-question-answer-form">
<%= render "form", form_url: admin_answer_path(@answer) %> <%= render "form", form_url: admin_question_answer_path(@question, @answer) %>
</div> </div>

View File

@@ -1,6 +1,6 @@
<%= back_link_to %> <%= back_link_to %>
<%= link_to t("admin.answers.show.edit"), edit_admin_answer_path(@answer), <%= link_to t("admin.answers.show.edit"), edit_admin_question_answer_path(@answer.question, @answer),
class: "button hollow float-right" %> class: "button hollow float-right" %>
<ul class="breadcrumbs margin-top"> <ul class="breadcrumbs margin-top">

View File

@@ -52,7 +52,7 @@
<tbody class="sortable" data-js-url="<%= admin_question_answers_order_answers_path(@question.id) %>"> <tbody class="sortable" data-js-url="<%= admin_question_answers_order_answers_path(@question.id) %>">
<% @question.question_answers.each do |answer| %> <% @question.question_answers.each do |answer| %>
<tr id="<%= dom_id(answer) %>" class="poll_question_answer" data-answer-id="<%= answer.id %>"> <tr id="<%= dom_id(answer) %>" class="poll_question_answer" data-answer-id="<%= answer.id %>">
<td class="align-top"><%= link_to answer.title, admin_answer_path(answer) %></td> <td class="align-top"><%= link_to answer.title, admin_question_answer_path(@question, answer) %></td>
<td class="align-top break"><%= wysiwyg(answer.description) %></td> <td class="align-top break"><%= wysiwyg(answer.description) %></td>
<td class="align-top text-center"> <td class="align-top text-center">
(<%= answer.images.count %>) (<%= answer.images.count %>)

View File

@@ -169,7 +169,8 @@ namespace :admin do
end end
resources :questions, shallow: true do resources :questions, shallow: true do
resources :answers, except: [:index, :destroy], controller: "questions/answers" do resources :answers, except: [:index, :destroy], controller: "questions/answers", shallow: false
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" get :documents, to: "questions/answers#documents"

View File

@@ -36,7 +36,7 @@ describe "Answers", :admin do
answer = create(:poll_question_answer, question: question, title: "Answer title", given_order: 2) answer = create(:poll_question_answer, question: question, title: "Answer title", given_order: 2)
create(:poll_question_answer, question: question, title: "Another title", given_order: 1) create(:poll_question_answer, question: question, title: "Another title", given_order: 1)
visit admin_answer_path(answer) visit admin_question_answer_path(question, answer)
click_link "Edit answer" click_link "Edit answer"

View File

@@ -216,7 +216,7 @@ describe "Admin edit translatable records", :admin do
context "CKEditor fields" do context "CKEditor fields" do
let(:translatable) { create(:poll_question_answer) } let(:translatable) { create(:poll_question_answer) }
let(:path) { edit_admin_answer_path(translatable) } let(:path) { edit_admin_question_answer_path(translatable.question, translatable) }
scenario "Changes the existing translation" do scenario "Changes the existing translation" do
visit path visit path