Use a switch to toggle visibility to valuators

Using a checkbox wasn't very intuitive because checkboxes are
checked/unchecked when clicked on even if there's an error in the
request. Usually, when checkboxes appear on a form, they don't send any
information to the server unless we click a button to send the form.

So we're using a switch instead of a checkbox, like we did to
enable/disable phases in commit 46d8bc4f0.

Note that, since we've got two switches that match the default
`dom_id(record) .toggle-switch` selector, we need to find a way to
differentiate them. We're adding the `form_class` option for that.

Also note that we're now using a separate action and removing the
JavaScript in the `update` action which assumed that AJAX requests to
this action were always related to updating the `visible_to_valuators`
attribute.
This commit is contained in:
Javi Martín
2024-10-08 16:04:18 +02:00
parent 76b0971b4a
commit fc5103881d
15 changed files with 191 additions and 63 deletions

View File

@@ -39,24 +39,36 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def update
authorize! :admin_update, @investment
respond_to do |format|
format.html do
if @investment.update(budget_investment_params)
redirect_to admin_budget_budget_investment_path(@budget,
@investment,
Budget::Investment.filter_params(params).to_h),
notice: t("flash.actions.update.budget_investment")
else
load_staff
load_valuator_groups
load_tags
render :edit
end
end
if @investment.update(budget_investment_params)
redirect_to admin_budget_budget_investment_path(@budget,
@investment,
Budget::Investment.filter_params(params).to_h),
notice: t("flash.actions.update.budget_investment")
else
load_staff
load_valuator_groups
load_tags
render :edit
end
end
format.json do
@investment.update!(budget_investment_params)
end
def show_to_valuators
authorize! :admin_update, @investment
@investment.update!(visible_to_valuators: true)
respond_to do |format|
format.html { redirect_to request.referer, notice: t("flash.actions.update.budget_investment") }
format.js { render :toggle_visible_to_valuators }
end
end
def hide_from_valuators
authorize! :admin_update, @investment
@investment.update!(visible_to_valuators: false)
respond_to do |format|
format.html { redirect_to request.referer, notice: t("flash.actions.update.budget_investment") }
format.js { render :toggle_visible_to_valuators }
end
end
@@ -108,7 +120,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def allowed_params
attributes = [:external_url, :heading_id, :administrator_id, :tag_list,
:valuation_tag_list, :incompatible, :visible_to_valuators, :selected,
:valuation_tag_list, :incompatible, :selected,
:milestone_tag_list, valuator_ids: [], valuator_group_ids: []]
[*attributes, translation_params(Budget::Investment)]
end