From 173b1bb07ca34523f18c66a348d4ba40b0040404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 9 Oct 2024 13:03:27 +0200 Subject: [PATCH] Make it possible to select investments without JavaScript --- .../admin/budget_investments_controller.rb | 10 ++++++++-- .../budget_investments_controller_spec.rb | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index a8e2b140e..eec3583ac 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -64,14 +64,20 @@ class Admin::BudgetInvestmentsController < Admin::BaseController authorize! :select, @investment @investment.update!(selected: true) - render :toggle_selection + respond_to do |format| + format.html { redirect_to request.referer, notice: t("flash.actions.update.budget_investment") } + format.js { render :toggle_selection } + end end def deselect authorize! :deselect, @investment @investment.update!(selected: false) - render :toggle_selection + respond_to do |format| + format.html { redirect_to request.referer, notice: t("flash.actions.update.budget_investment") } + format.js { render :toggle_selection } + end end private diff --git a/spec/controllers/admin/budget_investments_controller_spec.rb b/spec/controllers/admin/budget_investments_controller_spec.rb index 8cee63ce0..54760c292 100644 --- a/spec/controllers/admin/budget_investments_controller_spec.rb +++ b/spec/controllers/admin/budget_investments_controller_spec.rb @@ -66,6 +66,15 @@ describe Admin::BudgetInvestmentsController, :admin do "'select' on Investment." expect(investment).not_to be_selected end + + it "redirects admins without JavaScript to the same page" do + request.env["HTTP_REFERER"] = admin_budget_budget_investments_path(investment.budget) + + patch :select, params: { id: investment, budget_id: investment.budget } + + expect(response).to redirect_to admin_budget_budget_investments_path(investment.budget) + expect(flash[:notice]).to eq "Investment project updated successfully." + end end describe "PATCH deselect" do @@ -96,5 +105,14 @@ describe Admin::BudgetInvestmentsController, :admin do "'deselect' on Investment." expect(investment).to be_selected end + + it "redirects admins without JavaScript to the same page" do + request.env["HTTP_REFERER"] = admin_budget_budget_investments_path(investment.budget) + + patch :deselect, params: { id: investment, budget_id: investment.budget } + + expect(response).to redirect_to admin_budget_budget_investments_path(investment.budget) + expect(flash[:notice]).to eq "Investment project updated successfully." + end end end