Allow enabling/disabling phases in budgets wizard

So now there's no need to edit each phase individually to enable/disable
them.

We aren't doing the same thing in the form to edit a budget because we
aren't sure about possible usability issues. On one hand, in some tables
we automatically update records when we mark a checkbox, so users might
expect that. On the other hand, having a checkbox in the middle of a
form which updates the database automatically is counter-intuitive,
particularly when right below that table there are other checkboxes
which don't update the database until the form is submitted.

So, either way, chances are users would think they've updated the phases
(or kept them intact) while the opposite would be true.

In the form within the wizard to create a budget that problem isn't that
important because there aren't any other fields in the form and it's
pretty intuitive that what users do will have no effect until they press
the "Finish" button.

Co-Authored-By: Julian Nicolas Herrero <microweb10@gmail.com>
This commit is contained in:
Javi Martín
2021-06-06 15:57:22 +02:00
parent eb77d09425
commit f110e65f80
10 changed files with 80 additions and 9 deletions

View File

@@ -258,6 +258,11 @@ $table-header: #ecf1f6;
[type="submit"] ~ a {
margin-left: $line-height / 2;
}
[type="checkbox"] {
margin-bottom: 0;
vertical-align: middle;
}
}
hr {

View File

@@ -26,7 +26,7 @@
<% end %>
</td>
<td class="text-center">
<%= enabled_text(phase) %>
<%= enabled_cell(phase) %>
</td>
<td>
<%= render Admin::TableActionsComponent.new(phase,

View File

@@ -1,8 +1,9 @@
class Admin::BudgetPhases::PhasesComponent < ApplicationComponent
attr_reader :budget
attr_reader :budget, :form
def initialize(budget)
def initialize(budget, form: nil)
@budget = budget
@form = form
end
private
@@ -15,6 +16,20 @@ class Admin::BudgetPhases::PhasesComponent < ApplicationComponent
Admin::Budgets::DurationComponent.new(phase).dates
end
def enabled_cell(phase)
if form
form.fields_for :phases, phase do |phase_fields|
phase_fields.check_box :enabled,
label: false,
aria: {
label: t("admin.budgets.edit.enable_phase", phase: phase.name)
}
end
else
enabled_text(phase)
end
end
def enabled_text(phase)
if phase.enabled?
tag.span t("shared.yes"), class: "budget-phase-enabled"

View File

@@ -5,8 +5,7 @@
<%= render Admin::Budgets::HelpComponent.new("budget_phases") %>
<%= render Admin::BudgetsWizard::CreationTimelineComponent.new("phases") %>
<%= render Admin::BudgetPhases::PhasesComponent.new(budget) %>
<%= link_to t("admin.budgets_wizard.phases.continue"),
admin_budget_path(budget),
class: "button success" %>
<%= form_for budget, url: update_all_admin_budgets_wizard_budget_budget_phases_path(budget) do |f| %>
<%= render Admin::BudgetPhases::PhasesComponent.new(budget, form: f) %>
<%= f.submit t("admin.budgets_wizard.phases.continue"), class: "button success" %>
<% end %>

View File

@@ -4,9 +4,23 @@ class Admin::BudgetsWizard::PhasesController < Admin::BaseController
def index
end
def update_all
@budget.update!(phases_params)
redirect_to admin_budgets_path, notice: t("admin.budgets_wizard.phases.update_all.notice")
end
private
def phases_index
admin_budgets_wizard_budget_budget_phases_path(@phase.budget)
end
def phases_params
params.require(:budget).permit(allowed_phases_params)
end
def allowed_phases_params
{ phases_attributes: [:id, :enabled] }
end
end

View File

@@ -42,6 +42,7 @@ class Budget < ApplicationRecord
has_one :poll
after_create :generate_phases
accepts_nested_attributes_for :phases
scope :published, -> { where(published: true) }
scope :drafting, -> { where.not(id: published) }

View File

@@ -106,6 +106,7 @@ en:
enabled: Enabled
actions: Actions
edit_phase: Edit phase
enable_phase: "Enable %{phase} phase"
active: Active
blank_dates: Dates are blank
administrators:
@@ -308,6 +309,8 @@ en:
phases:
back: "Go back to headings"
continue: "Finish"
update_all:
notice: "Phases configured successfully"
milestones:
index:
table_id: "ID"

View File

@@ -106,6 +106,7 @@ es:
enabled: Habilitada
actions: Acciones
edit_phase: Editar fase
enable_phase: "Habilitar fase de %{phase}"
active: Activa
blank_dates: Sin fechas
administrators:
@@ -308,6 +309,8 @@ es:
phases:
back: "Volver a partidas"
continue: "Finalizar"
update_all:
notice: "Fases configuradas con éxito"
milestones:
index:
table_id: "ID"

View File

@@ -78,7 +78,9 @@ namespace :admin do
resources :headings, only: [:index, :create, :edit, :update, :destroy]
end
resources :phases, as: "budget_phases", only: [:index, :edit, :update]
resources :phases, as: "budget_phases", only: [:index, :edit, :update] do
collection { patch :update_all }
end
end
end

View File

@@ -19,6 +19,35 @@ describe "Budgets wizard, phases step", :admin do
expect(page).to have_css "tr", text: "Main Park"
expect(page).to have_css ".creation-timeline"
end
scenario "Enable and disable phases" do
visit admin_budgets_wizard_budget_budget_phases_path(budget)
uncheck "Enable Information phase"
uncheck "Enable Reviewing voting phase"
click_button "Finish"
expect(page).to have_content "Phases configured successfully"
visit edit_admin_budget_path(budget)
within "tr", text: "Information" do
expect(page).to have_css ".budget-phase-disabled", visible: :all
end
within "tr", text: "Reviewing voting" do
expect(page).to have_css ".budget-phase-disabled", visible: :all
end
within "tr", text: "Accepting projects" do
expect(page).to have_css ".budget-phase-enabled", visible: :all
end
within "tr", text: "Voting projects" do
expect(page).to have_css ".budget-phase-enabled", visible: :all
end
end
end
describe "Edit" do