From 5011d4745bdc5dc2bc64a33400f89a2cfa7492b3 Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 7 Sep 2022 13:41:26 +0200 Subject: [PATCH 01/19] Remove related code for unused actions from admin polls controller Actions :search_booths and :search_officers in admin polls controller are moved to other controllers since commit 20e31133a for :search_booths and commit 19ec7f93b for :search_officers. This then allows us to remove the code that references these actions in the controller and in the administrator abilities. --- app/controllers/admin/poll/polls_controller.rb | 9 --------- app/models/abilities/administrator.rb | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index 8cfa00136..5e9c84c90 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -4,7 +4,6 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController include ReportAttributes load_and_authorize_resource - before_action :load_search, only: [:search_booths, :search_officers] before_action :load_geozones, only: [:new, :create, :edit, :update] def index @@ -86,14 +85,6 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController [*attributes, *report_attributes, translation_params(Poll)] end - def search_params - params.permit(:poll_id, :search) - end - - def load_search - @search = search_params[:search] - end - def resource @poll ||= Poll.find(params[:id]) end diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 0f492cd0b..44201bdf4 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -83,7 +83,7 @@ module Abilities can [:index, :create, :update, :destroy], Geozone - can [:read, :create, :update, :destroy, :add_question, :search_booths, :search_officers, :booth_assignments], Poll + can [:read, :create, :update, :destroy, :add_question, :booth_assignments], Poll can [:read, :create, :update, :destroy, :available], Poll::Booth can [:search, :create, :index, :destroy], ::Poll::Officer can [:create, :destroy, :manage], ::Poll::BoothAssignment From 38b6cf36a2ca476cf008289da82de694c6f17b3e Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 7 Sep 2022 14:25:15 +0200 Subject: [PATCH 02/19] Remove unused add_question action from admin polls controller Since commit adf18ee756 this action no longer makes sense. --- app/controllers/admin/poll/polls_controller.rb | 12 ------------ app/models/abilities/administrator.rb | 2 +- config/locales/en/admin.yml | 3 --- config/locales/es/admin.yml | 3 --- config/routes/admin.rb | 1 - 5 files changed, 1 insertion(+), 20 deletions(-) diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index 5e9c84c90..71156ecf5 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -42,18 +42,6 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController end end - def add_question - question = ::Poll::Question.find(params[:question_id]) - - if question.present? - @poll.questions << question - notice = t("admin.polls.flash.question_added") - else - notice = t("admin.polls.flash.error_on_question_added") - end - redirect_to admin_poll_path(@poll), notice: notice - end - def booth_assignments @polls = Poll.current.created_by_admin end diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 44201bdf4..9647f970f 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -83,7 +83,7 @@ module Abilities can [:index, :create, :update, :destroy], Geozone - can [:read, :create, :update, :destroy, :add_question, :booth_assignments], Poll + can [:read, :create, :update, :destroy, :booth_assignments], Poll can [:read, :create, :update, :destroy, :available], Poll::Booth can [:search, :create, :index, :destroy], ::Poll::Officer can [:create, :destroy, :manage], ::Poll::BoothAssignment diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index f615458c3..5e3e52896 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -1103,9 +1103,6 @@ en: table_title: "Title" edit_answers: Edit answers see_proposal: "(See proposal)" - flash: - question_added: "Question added to this poll" - error_on_question_added: "Question could not be assigned to this poll" destroy: alert: "This action will remove the poll and all its associated questions." success_notice: "Poll deleted successfully" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index aa861ab09..d50c807da 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -1102,9 +1102,6 @@ es: table_title: "Título" edit_answers: Editar respuestas see_proposal: "(Ver propuesta)" - flash: - question_added: "Pregunta añadida a esta votación" - error_on_question_added: "No se pudo asignar la pregunta" destroy: alert: "Esta acción eliminará la votación y todas sus preguntas asociadas." success_notice: "Votación eliminada correctamente" diff --git a/config/routes/admin.rb b/config/routes/admin.rb index bae512e69..c3ee8f142 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -141,7 +141,6 @@ namespace :admin do scope module: :poll do resources :polls do get :booth_assignments, on: :collection - patch :add_question, on: :member resources :booth_assignments, only: [:index, :show, :create, :destroy] do get :search_booths, on: :collection From cc4b22ee3713187fed799653d5e77f508da9f70e Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 7 Sep 2022 14:35:43 +0200 Subject: [PATCH 03/19] Use resource from load_and_authorize_resource in admin polls controller --- app/controllers/admin/poll/polls_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index 71156ecf5..e42db27db 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -7,18 +7,18 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController before_action :load_geozones, only: [:new, :create, :edit, :update] def index - @polls = Poll.not_budget.created_by_admin.order(starts_at: :desc) + @polls = @polls.not_budget.created_by_admin.order(starts_at: :desc) end def show - @poll = Poll.find(params[:id]) end def new end def create - @poll = Poll.new(poll_params.merge(author: current_user)) + @poll.author = current_user + if @poll.save notice = t("flash.actions.create.poll") if @poll.budget.present? From ad9362399eab35dcd34998038fc6fe8df5733334 Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 8 Sep 2022 16:40:08 +0200 Subject: [PATCH 04/19] After destroy question redirect to his poll show page After removing a question from a poll it makes more sense to redirect to your own poll show page in order to manage their questions. Currently it is redirecting to the questions index page where all the questions from all the polls are displayed and takes you completely out of the context of the poll you are in. In the future we will remove this index question page. --- app/controllers/admin/poll/questions_controller.rb | 2 +- spec/system/admin/poll/questions_spec.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index bb07a5ac2..85673b936 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -48,7 +48,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController else notice = t("flash.actions.destroy.error") end - redirect_to admin_questions_path, notice: notice + redirect_to admin_poll_path(@question.poll), notice: notice end private diff --git a/spec/system/admin/poll/questions_spec.rb b/spec/system/admin/poll/questions_spec.rb index f7062ea67..bbf0eb77e 100644 --- a/spec/system/admin/poll/questions_spec.rb +++ b/spec/system/admin/poll/questions_spec.rb @@ -147,8 +147,9 @@ describe "Admin poll questions", :admin do end end - expect(page).not_to have_content(question1.title) - expect(page).to have_content(question2.title) + expect(page).not_to have_content question1.title + expect(page).to have_content question2.title + expect(page).to have_current_path admin_poll_path(poll) end context "Poll select box" do From 8b4cd13675287855474f46981b234cf4db3ba103 Mon Sep 17 00:00:00 2001 From: taitus Date: Fri, 9 Sep 2022 11:52:50 +0200 Subject: [PATCH 05/19] Unify with the rest of application destroy method in questions controller We also add a missing translation. --- app/controllers/admin/poll/questions_controller.rb | 8 ++------ config/locales/en/admin.yml | 2 ++ config/locales/es/admin.yml | 2 ++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index 85673b936..0bef8fb0b 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -43,12 +43,8 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController end def destroy - if @question.destroy - notice = "Question destroyed succesfully" - else - notice = t("flash.actions.destroy.error") - end - redirect_to admin_poll_path(@question.poll), notice: notice + @question.destroy! + redirect_to admin_poll_path(@question.poll), notice: t("admin.questions.destroy.notice") end private diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 5e3e52896..d0fe1c394 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -1126,6 +1126,8 @@ en: new: title: "Create question to poll %{poll}" title_proposal: "Create question" + destroy: + notice: "Question deleted succesfully" answers: images: add_image: "Add image" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index d50c807da..47399d97d 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -1125,6 +1125,8 @@ es: new: title: "Crear pregunta ciudadana para la votación %{poll}" title_proposal: "Crear pregunta ciudadana" + destroy: + notice: "Pregunta eliminada correctamente" answers: images: add_image: "Añadir imagen" From 01005b50cbd644c1c5f8e43c0f3923210cf9db4f Mon Sep 17 00:00:00 2001 From: taitus Date: Fri, 9 Sep 2022 10:57:53 +0200 Subject: [PATCH 06/19] Load question from load_and_authorize_resource in answers controller --- app/controllers/admin/poll/questions/answers_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 5e224a514..5713ea99a 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -7,12 +7,11 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController load_and_authorize_resource :question, class: "::Poll::Question" def new - @answer = ::Poll::Question::Answer.new + @answer = @question.answers.new end def create - @answer = ::Poll::Question::Answer.new(answer_params) - @question = @answer.question + @answer = @question.answers.new(answer_params) if @answer.save redirect_to admin_question_path(@question), @@ -30,7 +29,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController def update if @answer.update(answer_params) - redirect_to admin_question_path(@answer.question), + redirect_to admin_question_path(@question), notice: t("flash.actions.save_changes.notice") else render :edit From 405b37f6054f6fefe086622717c9150b80da5934 Mon Sep 17 00:00:00 2001 From: taitus Date: Fri, 9 Sep 2022 15:56:36 +0200 Subject: [PATCH 07/19] Load answer through question in answers controller We are simplifying the load answer and we can remove the ambiguous hidden field from answer form. --- .../admin/poll/questions/answers_controller.rb | 17 ++++++----------- .../admin/poll/questions/answers/_form.html.erb | 2 -- .../poll/questions/answers/documents.html.erb | 2 +- .../admin/poll/questions/answers/edit.html.erb | 2 +- .../admin/poll/questions/answers/show.html.erb | 2 +- app/views/admin/poll/questions/show.html.erb | 2 +- config/routes/admin.rb | 3 ++- .../poll/questions/answers/answers_spec.rb | 2 +- spec/system/admin/translatable_spec.rb | 2 +- 9 files changed, 14 insertions(+), 20 deletions(-) diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 5713ea99a..1d4dd4982 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -2,17 +2,16 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController include Translatable include DocumentAttributes - before_action :load_answer, only: [:show, :edit, :update, :documents] - load_and_authorize_resource :question, class: "::Poll::Question" + load_resource class: "::Poll::Question::Answer", + through: :question, + through_association: :question_answers, + except: :documents def new - @answer = @question.answers.new end def create - @answer = @question.answers.new(answer_params) - if @answer.save redirect_to admin_question_path(@question), notice: t("flash.actions.create.poll_question_answer") @@ -37,6 +36,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController end def documents + @answer = ::Poll::Question::Answer.find(params[:answer_id]) @documents = @answer.documents render "admin/poll/questions/answers/documents" @@ -54,16 +54,11 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController end def allowed_params - attributes = [:title, :description, :given_order, :question_id, - documents_attributes: document_attributes] + attributes = [:title, :description, :given_order, documents_attributes: document_attributes] [*attributes, translation_params(Poll::Question::Answer)] end - def load_answer - @answer = ::Poll::Question::Answer.find(params[:id] || params[:answer_id]) - end - def resource load_answer unless @answer @answer diff --git a/app/views/admin/poll/questions/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb index 53eceae17..ef32b93e5 100644 --- a/app/views/admin/poll/questions/answers/_form.html.erb +++ b/app/views/admin/poll/questions/answers/_form.html.erb @@ -7,8 +7,6 @@ <%= f.hidden_field :given_order, 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 %> -
<%= f.translatable_fields do |translations_form| %>
diff --git a/app/views/admin/poll/questions/answers/documents.html.erb b/app/views/admin/poll/questions/answers/documents.html.erb index 4c0396849..7a94fd8e6 100644 --- a/app/views/admin/poll/questions/answers/documents.html.erb +++ b/app/views/admin/poll/questions/answers/documents.html.erb @@ -9,7 +9,7 @@
<%= form_for(Poll::Question::Answer.new, - url: admin_answer_path(@answer), + url: admin_question_answer_path(@answer.question, @answer), method: :put) do |f| %> <%= render "shared/errors", resource: @answer %> diff --git a/app/views/admin/poll/questions/answers/edit.html.erb b/app/views/admin/poll/questions/answers/edit.html.erb index 46a8784cc..def21279d 100644 --- a/app/views/admin/poll/questions/answers/edit.html.erb +++ b/app/views/admin/poll/questions/answers/edit.html.erb @@ -10,5 +10,5 @@
- <%= render "form", form_url: admin_answer_path(@answer) %> + <%= render "form", form_url: admin_question_answer_path(@question, @answer) %>
diff --git a/app/views/admin/poll/questions/answers/show.html.erb b/app/views/admin/poll/questions/answers/show.html.erb index 8244687b2..00bc72b02 100644 --- a/app/views/admin/poll/questions/answers/show.html.erb +++ b/app/views/admin/poll/questions/answers/show.html.erb @@ -1,6 +1,6 @@ <%= 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" %>