From 72704d7761c77d060f9c0b140b3d70bc454d8787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 15 Dec 2022 17:43:57 +0100 Subject: [PATCH] Use separate actions to enable/disable budget phases This is consistent with the way we use separate actions to hide and restore records, which is similar to enabling and disabling a record. We might do something similar with the `toggle_selection` actions in the future. For now, we're only doing it with budget phases because we're going to add a similar switch control to hide and restore tenants. We're also making these actions idempotent, so sending many requests to the same action will get the same result, which wasn't the case with the `toggle` action. Although it's a low probability case, the `toggle` action could result in disabling a phase when trying to enable it if someone else has enabled it between the time the page loaded and the time the admin clicked on the "enable" button. --- .../toggle_enabled_component.html.erb | 2 +- .../budget_phases/toggle_enabled_component.rb | 14 ++++++++++++-- .../concerns/admin/budget_phases_actions.rb | 17 +++++++++++++---- .../admin/budget_phases/toggle_enabled.js.erb | 1 - .../budgets_wizard/phases/toggle_enabled.js.erb | 5 +++-- config/routes/admin.rb | 10 ++++++++-- 6 files changed, 37 insertions(+), 12 deletions(-) delete mode 100644 app/views/admin/budget_phases/toggle_enabled.js.erb diff --git a/app/components/admin/budget_phases/toggle_enabled_component.html.erb b/app/components/admin/budget_phases/toggle_enabled_component.html.erb index d230e97e5..d84202d46 100644 --- a/app/components/admin/budget_phases/toggle_enabled_component.html.erb +++ b/app/components/admin/budget_phases/toggle_enabled_component.html.erb @@ -1 +1 @@ -<%= render Admin::ActionComponent.new(:toggle_enabled, phase, options) %> +<%= render Admin::ActionComponent.new(action, phase, options) %> diff --git a/app/components/admin/budget_phases/toggle_enabled_component.rb b/app/components/admin/budget_phases/toggle_enabled_component.rb index 0236cdae5..5c270050c 100644 --- a/app/components/admin/budget_phases/toggle_enabled_component.rb +++ b/app/components/admin/budget_phases/toggle_enabled_component.rb @@ -1,5 +1,6 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent attr_reader :phase + delegate :enabled?, to: :phase def initialize(phase) @phase = phase @@ -13,12 +14,21 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent method: :patch, remote: true, "aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name), - "aria-pressed": phase.enabled? + "aria-pressed": enabled?, + form_class: "toggle-switch" } end + def action + if enabled? + :disable + else + :enable + end + end + def text - if phase.enabled? + if enabled? t("shared.yes") else t("shared.no") diff --git a/app/controllers/concerns/admin/budget_phases_actions.rb b/app/controllers/concerns/admin/budget_phases_actions.rb index 8c267c364..ff38f196a 100644 --- a/app/controllers/concerns/admin/budget_phases_actions.rb +++ b/app/controllers/concerns/admin/budget_phases_actions.rb @@ -6,7 +6,7 @@ module Admin::BudgetPhasesActions include ImageAttributes before_action :load_budget - before_action :load_phase, only: [:edit, :update, :toggle_enabled] + before_action :load_phase, only: [:edit, :update, :enable, :disable] end def edit @@ -20,12 +20,21 @@ module Admin::BudgetPhasesActions end end - def toggle_enabled - @phase.update!(enabled: !@phase.enabled) + def enable + @phase.update!(enabled: true) respond_to do |format| format.html { redirect_to phases_index, notice: t("flash.actions.save_changes.notice") } - format.js + format.js { render template: "admin/budgets_wizard/phases/toggle_enabled" } + end + end + + def disable + @phase.update!(enabled: false) + + respond_to do |format| + format.html { redirect_to phases_index, notice: t("flash.actions.save_changes.notice") } + format.js { render template: "admin/budgets_wizard/phases/toggle_enabled" } end end diff --git a/app/views/admin/budget_phases/toggle_enabled.js.erb b/app/views/admin/budget_phases/toggle_enabled.js.erb deleted file mode 100644 index b90a979bd..000000000 --- a/app/views/admin/budget_phases/toggle_enabled.js.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render template: "admin/budgets_wizard/phases/toggle_enabled" %> diff --git a/app/views/admin/budgets_wizard/phases/toggle_enabled.js.erb b/app/views/admin/budgets_wizard/phases/toggle_enabled.js.erb index d4424bf4c..d0688ab25 100644 --- a/app/views/admin/budgets_wizard/phases/toggle_enabled.js.erb +++ b/app/views/admin/budgets_wizard/phases/toggle_enabled.js.erb @@ -1,4 +1,5 @@ var replacement = $("<%= j render Admin::BudgetPhases::ToggleEnabledComponent.new(@phase) %>"); -var form = $("#" + replacement.find("[type='submit']").attr("id")).closest("form"); +var form = $("#<%= dom_id(@phase) %> .toggle-switch"); -form.html(replacement.html()).find("[type='submit']").focus(); +form.replaceWith(replacement); +replacement.find("[type='submit']").focus(); diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 752942b82..af7d96cec 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -70,7 +70,10 @@ namespace :admin do end resources :budget_phases, only: [:edit, :update] do - member { patch :toggle_enabled } + member do + patch :enable + patch :disable + end end end @@ -81,7 +84,10 @@ namespace :admin do end resources :phases, as: "budget_phases", only: [:index, :edit, :update] do - member { patch :toggle_enabled } + member do + patch :enable + patch :disable + end end end end