Use separate actions to select/deselect investments

This is consistent to what we usually do. Also, we're applying the same
criteria mentioned in commit 72704d776:

> 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 [selecting an investment] when trying
> to [deselect] it if someone else has [deselected it] it between the
> time the page loaded and the time the admin clicked on the
> "[Selected]" button.
This commit is contained in:
Javi Martín
2024-10-09 01:56:32 +02:00
parent 463112c2ea
commit 54a48d63e1
7 changed files with 87 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
<% if can?(:toggle_selection, investment) %>
<% if can?(action, investment) %>
<%= render Admin::ToggleSwitchComponent.new(action, investment, pressed: selected?, **options) %>
<% elsif selected? %>
<%= selected_text %>

View File

@@ -14,20 +14,26 @@ class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent
end
def action
:toggle_selection
if selected?
:deselect
else
:select
end
end
def path
toggle_selection_admin_budget_budget_investment_path(
investment.budget,
investment,
url_for({
controller: "admin/budget_investments",
action: action,
budget_id: investment.budget,
id: investment,
filter: params[:filter],
sort_by: params[:sort_by],
min_total_supports: params[:min_total_supports],
max_total_supports: params[:max_total_supports],
advanced_filters: params[:advanced_filters],
page: params[:page]
)
})
end
def options