Load videos through answer in all actions

This commit is contained in:
taitus
2022-09-09 14:27:29 +02:00
parent 919534fbc5
commit b17b03099c
8 changed files with 13 additions and 26 deletions

View File

@@ -1,17 +1,14 @@
class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseController class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseController
before_action :load_answer, only: [:index, :new, :create] load_resource :answer, class: "::Poll::Question::Answer"
before_action :load_video, only: [:edit, :update, :destroy] load_resource class: "::Poll::Question::Answer::Video", through: :answer
def index def index
end end
def new def new
@video = ::Poll::Question::Answer::Video.new
end end
def create def create
@video = ::Poll::Question::Answer::Video.new(video_params)
if @video.save if @video.save
redirect_to admin_answer_videos_path(@answer), redirect_to admin_answer_videos_path(@answer),
notice: t("flash.actions.create.poll_question_answer_video") notice: t("flash.actions.create.poll_question_answer_video")
@@ -25,7 +22,7 @@ class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseContr
def update def update
if @video.update(video_params) if @video.update(video_params)
redirect_to admin_answer_videos_path(@video.answer), notice: t("flash.actions.save_changes.notice") redirect_to admin_answer_videos_path(@answer), notice: t("flash.actions.save_changes.notice")
else else
render :edit render :edit
end end
@@ -34,7 +31,7 @@ class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseContr
def destroy def destroy
@video.destroy! @video.destroy!
notice = t("flash.actions.destroy.poll_question_answer_video") notice = t("flash.actions.destroy.poll_question_answer_video")
redirect_to admin_answer_videos_path(@video.answer), notice: notice redirect_to admin_answer_videos_path(@answer), notice: notice
end end
private private
@@ -44,14 +41,6 @@ class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseContr
end end
def allowed_params def allowed_params
[:title, :url, :answer_id] [:title, :url]
end
def load_answer
@answer = ::Poll::Question::Answer.find(params[:answer_id])
end
def load_video
@video = ::Poll::Question::Answer::Video.find(params[:id])
end end
end end

View File

@@ -2,8 +2,6 @@
<%= render "shared/errors", resource: @video %> <%= render "shared/errors", resource: @video %>
<%= f.hidden_field :answer_id, value: @video.answer_id || @answer.id %>
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">

View File

@@ -5,5 +5,5 @@
</h2> </h2>
<div class="poll-question-answer-video-form"> <div class="poll-question-answer-video-form">
<%= render "form", form_url: admin_video_path(@video) %> <%= render "form", form_url: admin_answer_video_path(@answer, @video) %>
</div> </div>

View File

@@ -7,7 +7,7 @@
</h2> </h2>
<%= link_to t("admin.answers.videos.index.add_video"), <%= link_to t("admin.answers.videos.index.add_video"),
new_admin_answer_video_path, new_admin_answer_video_path(@answer),
class: "button success float-right" %> class: "button success float-right" %>
<div> <div>
@@ -22,7 +22,7 @@
</thead> </thead>
<tbody> <tbody>
<% @answer.videos.each do |video| %> <% @videos.each do |video| %>
<tr id="<%= dom_id(video) %>" class="poll_question_answer_video"> <tr id="<%= dom_id(video) %>" class="poll_question_answer_video">
<td><%= video.title %></td> <td><%= video.title %></td>
<td><%= link_to video.url, video.url %></td> <td><%= link_to video.url, video.url %></td>

View File

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

View File

@@ -172,7 +172,7 @@ namespace :admin do
resources :answers, except: [:index, :show, :destroy], controller: "questions/answers", shallow: false resources :answers, except: [:index, :show, :destroy], controller: "questions/answers", shallow: false
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", shallow: false
resources :documents, only: [:index, :create], controller: "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"
@@ -328,5 +328,5 @@ resolve "Poll::Question::Answer" do |answer, options|
end end
resolve "Poll::Question::Answer::Video" do |video, options| resolve "Poll::Question::Answer::Video" do |video, options|
[:video, options.merge(id: video)] [:answer, :video, options.merge(answer_id: video.answer, id: video)]
end end

View File

@@ -97,7 +97,7 @@ describe "Polymorphic routes" do
it "routes poll answer videos" do it "routes poll answer videos" do
video = create(:poll_answer_video) video = create(:poll_answer_video)
expect(admin_polymorphic_path(video)).to eq admin_video_path(video) expect(admin_polymorphic_path(video)).to eq admin_answer_video_path(video.answer, video)
end end
it "routes milestones for resources with no hierarchy" do it "routes milestones for resources with no hierarchy" do

View File

@@ -26,7 +26,7 @@ describe "Videos", :admin do
scenario "Update" do scenario "Update" do
video = create(:poll_answer_video, answer: answer) video = create(:poll_answer_video, answer: answer)
visit edit_admin_video_path(video) visit edit_admin_answer_video_path(answer, video)
fill_in "Title", with: title fill_in "Title", with: title
fill_in "External video", with: url fill_in "External video", with: url