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

@@ -9,7 +9,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
has_filters %w[all], only: :index
before_action :load_budget
before_action :load_investment, only: [:show, :edit, :update, :toggle_selection]
before_action :load_investment, except: [:index]
before_action :load_ballot, only: [:show, :index]
before_action :parse_valuation_filters
before_action :load_investments, only: :index
@@ -60,10 +60,18 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
end
end
def toggle_selection
authorize! :toggle_selection, @investment
@investment.toggle :selected
@investment.save!
def select
authorize! :select, @investment
@investment.update!(selected: true)
render :toggle_selection
end
def deselect
authorize! :deselect, @investment
@investment.update!(selected: false)
render :toggle_selection
end
private