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.
This commit is contained in:
@@ -1 +1 @@
|
|||||||
<%= render Admin::ActionComponent.new(:toggle_enabled, phase, options) %>
|
<%= render Admin::ActionComponent.new(action, phase, options) %>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
|
class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
|
||||||
attr_reader :phase
|
attr_reader :phase
|
||||||
|
delegate :enabled?, to: :phase
|
||||||
|
|
||||||
def initialize(phase)
|
def initialize(phase)
|
||||||
@phase = phase
|
@phase = phase
|
||||||
@@ -13,12 +14,21 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
|
|||||||
method: :patch,
|
method: :patch,
|
||||||
remote: true,
|
remote: true,
|
||||||
"aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name),
|
"aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name),
|
||||||
"aria-pressed": phase.enabled?
|
"aria-pressed": enabled?,
|
||||||
|
form_class: "toggle-switch"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def action
|
||||||
|
if enabled?
|
||||||
|
:disable
|
||||||
|
else
|
||||||
|
:enable
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def text
|
def text
|
||||||
if phase.enabled?
|
if enabled?
|
||||||
t("shared.yes")
|
t("shared.yes")
|
||||||
else
|
else
|
||||||
t("shared.no")
|
t("shared.no")
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ module Admin::BudgetPhasesActions
|
|||||||
include ImageAttributes
|
include ImageAttributes
|
||||||
|
|
||||||
before_action :load_budget
|
before_action :load_budget
|
||||||
before_action :load_phase, only: [:edit, :update, :toggle_enabled]
|
before_action :load_phase, only: [:edit, :update, :enable, :disable]
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
@@ -20,12 +20,21 @@ module Admin::BudgetPhasesActions
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle_enabled
|
def enable
|
||||||
@phase.update!(enabled: !@phase.enabled)
|
@phase.update!(enabled: true)
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_to phases_index, notice: t("flash.actions.save_changes.notice") }
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
<%= render template: "admin/budgets_wizard/phases/toggle_enabled" %>
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
var replacement = $("<%= j render Admin::BudgetPhases::ToggleEnabledComponent.new(@phase) %>");
|
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();
|
||||||
|
|||||||
@@ -70,7 +70,10 @@ namespace :admin do
|
|||||||
end
|
end
|
||||||
|
|
||||||
resources :budget_phases, only: [:edit, :update] do
|
resources :budget_phases, only: [:edit, :update] do
|
||||||
member { patch :toggle_enabled }
|
member do
|
||||||
|
patch :enable
|
||||||
|
patch :disable
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -81,7 +84,10 @@ namespace :admin do
|
|||||||
end
|
end
|
||||||
|
|
||||||
resources :phases, as: "budget_phases", only: [:index, :edit, :update] do
|
resources :phases, as: "budget_phases", only: [:index, :edit, :update] do
|
||||||
member { patch :toggle_enabled }
|
member do
|
||||||
|
patch :enable
|
||||||
|
patch :disable
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user