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

@@ -23,18 +23,61 @@ describe Admin::BudgetInvestmentsController, :admin do
end
end
describe "PATCH update" do
it "does not redirect on AJAX requests" do
investment = create(:budget_investment)
describe "PATCH show_to_valuators" do
let(:investment) { create(:budget_investment, :invisible_to_valuators) }
patch :update, params: {
id: investment,
budget_id: investment.budget,
format: :json,
budget_investment: { visible_to_valuators: true }
}
it "marks the investment as visible to valuators" do
expect do
patch :show_to_valuators, xhr: true, params: { id: investment, budget_id: investment.budget }
end.to change { investment.reload.visible_to_valuators? }.from(false).to(true)
expect(response).not_to be_redirect
expect(response).to be_successful
end
it "does not modify investments visible to valuators" do
investment.update!(visible_to_valuators: true)
expect do
patch :show_to_valuators, xhr: true, params: { id: investment, budget_id: investment.budget }
end.not_to change { investment.reload.visible_to_valuators? }
end
it "redirects admins without JavaScript to the same page" do
request.env["HTTP_REFERER"] = admin_budget_budget_investments_path(investment.budget)
patch :show_to_valuators, params: { id: investment, budget_id: investment.budget }
expect(response).to redirect_to admin_budget_budget_investments_path(investment.budget)
expect(flash[:notice]).to eq "Investment project updated successfully."
end
end
describe "PATCH hide_from_valuators" do
let(:investment) { create(:budget_investment, :visible_to_valuators) }
it "marks the investment as visible to valuators" do
expect do
patch :hide_from_valuators, xhr: true, params: { id: investment, budget_id: investment.budget }
end.to change { investment.reload.visible_to_valuators? }.from(true).to(false)
expect(response).to be_successful
end
it "does not modify investments visible to valuators" do
investment.update!(visible_to_valuators: false)
expect do
patch :hide_from_valuators, xhr: true, params: { id: investment, budget_id: investment.budget }
end.not_to change { investment.reload.visible_to_valuators? }
end
it "redirects admins without JavaScript to the same page" do
request.env["HTTP_REFERER"] = admin_budget_budget_investments_path(investment.budget)
patch :hide_from_valuators, params: { id: investment, budget_id: investment.budget }
expect(response).to redirect_to admin_budget_budget_investments_path(investment.budget)
expect(flash[:notice]).to eq "Investment project updated successfully."
end
end