From 4c8be42ea1122edc674de9fd1b8fa9758fbb8d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 15 Sep 2022 16:39:29 +0200 Subject: [PATCH] Simplify new question form In this form, the only case where `poll` might be present without `question.poll` being present to is going to be the `new` action. We can assign the poll in the `new` action and get rid of the `poll` variable in the form. --- app/components/admin/poll/questions/form_component.html.erb | 4 +--- app/components/admin/poll/questions/form_component.rb | 5 ++--- app/controllers/admin/poll/questions_controller.rb | 1 + app/views/admin/poll/questions/_form.html.erb | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/components/admin/poll/questions/form_component.html.erb b/app/components/admin/poll/questions/form_component.html.erb index 09c9ba9ad..34193725f 100644 --- a/app/components/admin/poll/questions/form_component.html.erb +++ b/app/components/admin/poll/questions/form_component.html.erb @@ -8,9 +8,7 @@
- <% if poll.present? %> - <%= f.hidden_field :poll_id, value: poll.id %> - <% elsif question.poll.present? %> + <% if question.poll.present? %> <%= f.hidden_field :poll_id, value: question.poll.id %> <% else %>
diff --git a/app/components/admin/poll/questions/form_component.rb b/app/components/admin/poll/questions/form_component.rb index 25507c284..4930c984e 100644 --- a/app/components/admin/poll/questions/form_component.rb +++ b/app/components/admin/poll/questions/form_component.rb @@ -1,10 +1,9 @@ class Admin::Poll::Questions::FormComponent < ApplicationComponent include TranslatableFormHelper include GlobalizeHelper - attr_reader :poll, :question, :url + attr_reader :question, :url - def initialize(poll, question, url:) - @poll = poll + def initialize(question, url:) @question = question @url = url end diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index 598e664e7..ea133c65f 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -16,6 +16,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController @polls = Poll.all proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present? @question.copy_attributes_from_proposal(proposal) + @question.poll = @poll end def create diff --git a/app/views/admin/poll/questions/_form.html.erb b/app/views/admin/poll/questions/_form.html.erb index fb792c3de..c1254b5ca 100644 --- a/app/views/admin/poll/questions/_form.html.erb +++ b/app/views/admin/poll/questions/_form.html.erb @@ -1 +1 @@ -<%= render Admin::Poll::Questions::FormComponent.new(@poll, @question, url: form_url) %> +<%= render Admin::Poll::Questions::FormComponent.new(@question, url: form_url) %>