Files
nairobi/app/controllers/concerns/admin/budget_phases_actions.rb
Javi Martín 46d8bc4f0e Use a switch to enable/disable budget phases
In the past it would have been confusing to add a way to directly
enable/disable a phase in the phases table because it was in the middle
of the form. So we would have had next to each other controls that don't
do anything until the form is sent and controls which modify the
database immediately. That's why we couldn't add the checkboxes we used
when using the wizard.

Now the phases aren't on the same page as the budget form, so we can
edit them independently. We're using a switch, so it's consistent with
the way we enable/disable features. We could have used checkboxes, but
with checkboxes, users expect they aren't changing anything until they
click on a button to send the form, so we'd have to add a button, and it
might be missed since we're going to add "buttons" for headings and
groups to this page which won't send a form but will be links.

Since we're changing the element with JavaScript after an AJAX call, we
need a way to find the button we're changing. The easiest way is adding
an ID attribute to all admin actions buttons/links.
2021-10-25 18:01:47 +02:00

48 lines
1.1 KiB
Ruby

module Admin::BudgetPhasesActions
extend ActiveSupport::Concern
included do
include Translatable
include ImageAttributes
before_action :load_budget
before_action :load_phase, only: [:edit, :update, :toggle_enabled]
end
def edit
end
def update
if @phase.update(budget_phase_params)
redirect_to phases_index, notice: t("flash.actions.save_changes.notice")
else
render :edit
end
end
def toggle_enabled
@phase.update!(enabled: !@phase.enabled)
respond_to do |format|
format.html { redirect_to phases_index, notice: t("flash.actions.save_changes.notice") }
format.js
end
end
private
def load_budget
@budget = Budget.find_by_slug_or_id!(params[:budget_id])
end
def load_phase
@phase = @budget.phases.find(params[:id])
end
def budget_phase_params
valid_attributes = [:starts_at, :ends_at, :enabled, :main_link_url,
image_attributes: image_attributes]
params.require(:budget_phase).permit(*valid_attributes, translation_params(Budget::Phase))
end
end