Fix unselecting all staff for a budget

We need to add a hidden field for each group of check boxes, so if we
don't check anything, the hidden field is sent to the server, indicating
nothing was selected. Without the hidden field, the server will not know
anything has been done to the check boxes.

The easiest way to do it is using `collection_check_boxes`, which also
adds labels to every check box.
This commit is contained in:
Javi Martín
2019-10-31 16:11:27 +01:00
parent e47c55e87f
commit ac1dd79f95
7 changed files with 49 additions and 40 deletions

View File

@@ -2,13 +2,13 @@
"use strict"; "use strict";
App.BudgetEditAssociations = { App.BudgetEditAssociations = {
initialize: function() { initialize: function() {
$(".js-budget-list-checkbox-user").on({ $(".js-budget-users-list [type='checkbox']").on({
click: function() { change: function() {
var admin_count, tracker_count, valuator_count; var admin_count, tracker_count, valuator_count;
admin_count = $(".js-budget-list-checkbox-administrators:checkbox:checked").length; admin_count = $("#administrators_list :checked").length;
valuator_count = $(".js-budget-list-checkbox-valuators:checkbox:checked").length; valuator_count = $("#valuators_list :checked").length;
tracker_count = $(".js-budget-list-checkbox-trackers:checkbox:checked").length; tracker_count = $("#trackers_list :checked").length;
App.I18n.set_pluralize($(".js-budget-show-administrators-list"), admin_count); App.I18n.set_pluralize($(".js-budget-show-administrators-list"), admin_count);
App.I18n.set_pluralize($(".js-budget-show-valuators-list"), valuator_count); App.I18n.set_pluralize($(".js-budget-show-valuators-list"), valuator_count);

View File

@@ -3,23 +3,9 @@
<p><%= t("admin.budgets.edit.empty_#{assignable_type}") %></p> <p><%= t("admin.budgets.edit.empty_#{assignable_type}") %></p>
<% else %> <% else %>
<h3><%= t("admin.budgets.edit.#{assignable_type}", count: 0) %></h3> <h3><%= t("admin.budgets.edit.#{assignable_type}", count: 0) %></h3>
<table class="table-for-mobile"> <% field = "#{assignable_type.chomp("s")}_ids" %>
<thead> <%= form.collection_check_boxes field, assignables, :id, :name do |box| %>
<tr> <%= box.label { box.check_box + box.text } %>
<th class="small-8"><%= t("admin.budgets.edit.name") %></th>
<th class="small-4"><%= t("admin.budgets.edit.selected") %></th>
</tr>
</thead>
<tbody>
<% assignables.each do |assignable| %>
<tr>
<td class="small-8"><%= assignable.name %></td>
<td class="small-4 text-center">
<input type="checkbox" id="<%= assignable_type.singularize %>_<%= assignable.user.id %>" name="budget[<%= assignable_type.singularize %>_ids][]" value="<%= assignable.id %>" <%= "checked" if @budget.send(assignable_type).include? assignable %> class="js-budget-list-checkbox-<%= assignable_type %> js-budget-list-checkbox-user">
</td>
</tr>
<% end %> <% end %>
</tbody>
</table>
<% end %> <% end %>
</div> </div>

View File

@@ -33,9 +33,9 @@
</div> </div>
<div class="margin-top"> <div class="margin-top">
<%= render "/admin/budgets/association", assignable_type: "administrators", assignables: @admins, budget: @budget %> <%= render "/admin/budgets/association", assignable_type: "administrators", assignables: @admins, form: f %>
<%= render "/admin/budgets/association", assignable_type: "valuators", assignables: @valuators, budget: @budget %> <%= render "/admin/budgets/association", assignable_type: "valuators", assignables: @valuators, form: f %>
<%= render "/admin/budgets/association", assignable_type: "trackers", assignables: @trackers, budget: @budget %> <%= render "/admin/budgets/association", assignable_type: "trackers", assignables: @trackers, form: f %>
</div> </div>
<div class="margin-top"> <div class="margin-top">

View File

@@ -109,8 +109,6 @@ en:
empty_administrators: "There are no administrators" empty_administrators: "There are no administrators"
empty_valuators: "There are no valuators" empty_valuators: "There are no valuators"
empty_trackers: "There are no trackers" empty_trackers: "There are no trackers"
name: "Name"
selected: "Selected"
destroy: destroy:
success_notice: Budget deleted successfully success_notice: Budget deleted successfully
unable_notice: You cannot delete a budget that has associated investments unable_notice: You cannot delete a budget that has associated investments

View File

@@ -109,8 +109,6 @@ es:
empty_administrators: "No hay administradores" empty_administrators: "No hay administradores"
empty_valuators: "No hay evaluadores" empty_valuators: "No hay evaluadores"
empty_trackers: "No hay gestores de seguimiento" empty_trackers: "No hay gestores de seguimiento"
name: "Nombre"
selected: "Seleccionado"
destroy: destroy:
success_notice: Presupuesto eliminado correctamente success_notice: Presupuesto eliminado correctamente
unable_notice: No se puede eliminar un presupuesto con proyectos asociados unable_notice: No se puede eliminar un presupuesto con proyectos asociados

View File

@@ -1086,14 +1086,15 @@ describe "Admin budget investments" do
expect(page).not_to have_content "Mark as incompatible" expect(page).not_to have_content "Mark as incompatible"
end end
scenario "Add administrator" do scenario "Add administrator", :js do
budget_investment = create(:budget_investment) budget_investment = create(:budget_investment)
user = create(:user, username: "Marta", email: "marta@admins.org") user = create(:user, username: "Marta", email: "marta@admins.org")
create(:administrator, user: user, description: "Marta desc") create(:administrator, user: user, description: "Marta desc")
visit edit_admin_budget_path(budget_investment.budget) visit edit_admin_budget_path(budget_investment.budget)
check "administrator_#{user.id}" click_link "Select administrators"
check "Marta"
click_button "Update Budget" click_button "Update Budget"
visit admin_budget_budget_investment_path(budget_investment.budget, budget_investment) visit admin_budget_budget_investment_path(budget_investment.budget, budget_investment)
@@ -1119,8 +1120,8 @@ describe "Admin budget investments" do
visit edit_admin_budget_path(budget_investment.budget) visit edit_admin_budget_path(budget_investment.budget)
check "valuator_#{user1.id}" check "Valentina"
check "valuator_#{user3.id}" check "Val"
click_button "Update Budget" click_button "Update Budget"
visit admin_budget_budget_investment_path(budget_investment.budget, budget_investment) visit admin_budget_budget_investment_path(budget_investment.budget, budget_investment)

View File

@@ -232,13 +232,8 @@ describe "Admin budgets" do
end end
context "Update" do context "Update" do
before do
create(:budget)
end
scenario "Update budget" do scenario "Update budget" do
visit admin_budgets_path visit edit_admin_budget_path(create(:budget))
click_link "Edit budget"
fill_in "Name", with: "More trees on the streets" fill_in "Name", with: "More trees on the streets"
click_button "Update Budget" click_button "Update Budget"
@@ -246,6 +241,37 @@ describe "Admin budgets" do
expect(page).to have_content("More trees on the streets") expect(page).to have_content("More trees on the streets")
expect(page).to have_current_path(admin_budgets_path) expect(page).to have_current_path(admin_budgets_path)
end end
scenario "Deselect all selected staff", :js do
admin = Administrator.first
valuator = create(:valuator)
tracker = create(:tracker)
budget = create(:budget, administrators: [admin], valuators: [valuator], trackers: [tracker])
visit edit_admin_budget_path(budget)
click_link "1 administrator selected"
uncheck admin.name
expect(page).to have_link "Select administrators"
click_link "1 valuator selected"
uncheck valuator.name
expect(page).to have_link "Select valuators"
click_link "1 tracker selected"
uncheck tracker.name
expect(page).to have_link "Select trackers"
click_button "Update Budget"
visit edit_admin_budget_path(budget)
expect(page).to have_link "Select administrators"
expect(page).to have_link "Select valuators"
expect(page).to have_link "Select trackers"
end
end end
context "Calculate Budget's Winner Investments" do context "Calculate Budget's Winner Investments" do