Add phases step to budget creation

This commit is contained in:
Julian Herrero
2020-03-23 19:04:38 +07:00
committed by Javi Martín
parent 9421f1673a
commit eb77d09425
20 changed files with 189 additions and 31 deletions

View File

@@ -1,6 +1,6 @@
<%= render "shared/globalize_locales", resource: phase %> <%= render "shared/globalize_locales", resource: phase %>
<%= translatable_form_for [:admin, phase.budget, phase], html: { class: "budget-phases-form" } do |f| %> <%= translatable_form_for [namespace, phase.budget, phase], html: { class: "budget-phases-form" } do |f| %>
<%= render "shared/errors", resource: phase %> <%= render "shared/errors", resource: phase %>

View File

@@ -1,6 +1,7 @@
class Admin::BudgetPhases::FormComponent < ApplicationComponent class Admin::BudgetPhases::FormComponent < ApplicationComponent
include TranslatableFormHelper include TranslatableFormHelper
include GlobalizeHelper include GlobalizeHelper
include Admin::Namespace
attr_reader :phase attr_reader :phase

View File

@@ -8,6 +8,6 @@ class Admin::BudgetsWizard::CreationTimelineComponent < ApplicationComponent
private private
def steps def steps
%w[budget groups headings] %w[budget groups headings phases]
end end
end end

View File

@@ -16,7 +16,7 @@ class Admin::BudgetsWizard::Headings::CreationStepComponent < ApplicationCompone
end end
def next_step_path def next_step_path
admin_budget_path(budget) if next_step_enabled? admin_budgets_wizard_budget_budget_phases_path(budget) if next_step_enabled?
end end
def next_step_enabled? def next_step_enabled?

View File

@@ -0,0 +1,6 @@
<%= back_link_to admin_budgets_wizard_budget_budget_phases_path(budget) %>
<%= header %>
<%= render Admin::BudgetsWizard::CreationTimelineComponent.new("phases") %>
<%= render "/admin/budget_phases/form" %>

View File

@@ -0,0 +1,22 @@
class Admin::BudgetsWizard::Phases::EditComponent < ApplicationComponent
include Header
attr_reader :phase
def initialize(phase)
@phase = phase
end
def budget
phase.budget
end
def title
"#{t("admin.budget_phases.edit.title")} - #{phase.name}"
end
private
def form_path
admin_budgets_wizard_budget_budget_phases_path(budget)
end
end

View File

@@ -0,0 +1,12 @@
<%= back_link_to back_link_path, t("admin.budgets_wizard.phases.back") %>
<%= header %>
<%= 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" %>

View File

@@ -0,0 +1,18 @@
class Admin::BudgetsWizard::Phases::IndexComponent < ApplicationComponent
include Header
attr_reader :budget
def initialize(budget)
@budget = budget
end
def title
t("admin.budget_phases.index.title", budget: budget.name)
end
private
def back_link_path
admin_budgets_wizard_budget_group_headings_path(budget, budget.groups.first)
end
end

View File

@@ -1,28 +1,9 @@
class Admin::BudgetPhasesController < Admin::BaseController class Admin::BudgetPhasesController < Admin::BaseController
include Translatable include Admin::BudgetPhasesActions
before_action :load_phase, only: [:edit, :update]
def edit
end
def update
if @phase.update(budget_phase_params)
notice = t("flash.actions.save_changes.notice")
redirect_to edit_admin_budget_path(@phase.budget), notice: notice
else
render :edit
end
end
private private
def load_phase def phases_index
@phase = Budget::Phase.find(params[:id]) edit_admin_budget_path(@phase.budget)
end
def budget_phase_params
valid_attributes = [:starts_at, :ends_at, :enabled]
params.require(:budget_phase).permit(*valid_attributes, translation_params(Budget::Phase))
end end
end end

View File

@@ -0,0 +1,12 @@
class Admin::BudgetsWizard::PhasesController < Admin::BaseController
include Admin::BudgetPhasesActions
def index
end
private
def phases_index
admin_budgets_wizard_budget_budget_phases_path(@phase.budget)
end
end

View File

@@ -0,0 +1,36 @@
module Admin::BudgetPhasesActions
extend ActiveSupport::Concern
included do
include Translatable
before_action :load_budget
before_action :load_phase, only: [:edit, :update]
end
def edit
end
def update
if @phase.update(budget_phase_params)
redirect_to phases_index, notice: t("flash.actions.save_changes.notice")
else
render :edit
end
end
private
def load_budget
@budget = Budget.find_by_slug_or_id!(params[:budget_id])
end
def load_phase
@phase = @budget.phases.find(params[:id])
end
def budget_phase_params
valid_attributes = [:starts_at, :ends_at, :enabled]
params.require(:budget_phase).permit(*valid_attributes, translation_params(Budget::Phase))
end
end

View File

@@ -1,5 +1 @@
<%= back_link_to edit_admin_budget_path(@phase.budget) %> <%= render Admin::BudgetsWizard::Phases::EditComponent.new(@phase) %>
<h2><%= t("admin.budgets.edit.title") %> - <%= @phase.name %></h2>
<%= render "/admin/budget_phases/form" %>

View File

@@ -0,0 +1 @@
<%= render Admin::BudgetsWizard::Phases::EditComponent.new(@phase) %>

View File

@@ -0,0 +1 @@
<%= render Admin::BudgetsWizard::Phases::IndexComponent.new(@budget) %>

View File

@@ -149,6 +149,7 @@ ignore_unused:
- "admin.budgets.edit.(administrators|valuators).*" - "admin.budgets.edit.(administrators|valuators).*"
- "admin.budget_groups.index.*.help_block" - "admin.budget_groups.index.*.help_block"
- "admin.budget_headings.index.*.help_block" - "admin.budget_headings.index.*.help_block"
- "admin.budget_phases.index.help_block"
- "admin.budget_investments.index.filter*" - "admin.budget_investments.index.filter*"
- "admin.organizations.index.filter*" - "admin.organizations.index.filter*"
- "admin.hidden_users.index.filter*" - "admin.hidden_users.index.filter*"

View File

@@ -188,6 +188,7 @@ en:
title: "%{budget} / %{group} headings" title: "%{budget} / %{group} headings"
budget_phases: budget_phases:
edit: edit:
title: "Edit phase"
description_help_text: This text will appear in the header when the phase is active description_help_text: This text will appear in the header when the phase is active
duration: "Phase's duration" duration: "Phase's duration"
duration_description: "The period of time this phase will be active." duration_description: "The period of time this phase will be active."
@@ -196,6 +197,7 @@ en:
save_changes: Save changes save_changes: Save changes
index: index:
help: "Participatory budgets have different phases. Here you can enable or disable phases and also customize each individual phase." help: "Participatory budgets have different phases. Here you can enable or disable phases and also customize each individual phase."
title: "%{budget} phases"
budget_investments: budget_investments:
index: index:
heading_filter_all: All headings heading_filter_all: All headings
@@ -296,12 +298,16 @@ en:
budget: Budget budget: Budget
groups: Groups groups: Groups
headings: Headings headings: Headings
phases: Phases
budgets: budgets:
continue: "Continue to groups" continue: "Continue to groups"
groups: groups:
continue: "Continue to headings" continue: "Continue to headings"
headings: headings:
continue: "Continue to phases" continue: "Continue to phases"
phases:
back: "Go back to headings"
continue: "Finish"
milestones: milestones:
index: index:
table_id: "ID" table_id: "ID"

View File

@@ -194,8 +194,10 @@ es:
enabled_help_text: Esta fase será pública en el calendario de fases del presupuesto y estará activa para otros propósitos enabled_help_text: Esta fase será pública en el calendario de fases del presupuesto y estará activa para otros propósitos
name_help_text: "Este es el título de la fase que los usuarios leerán en el encabezado cuando la fase esté activa." name_help_text: "Este es el título de la fase que los usuarios leerán en el encabezado cuando la fase esté activa."
save_changes: Guardar cambios save_changes: Guardar cambios
title: "Editar fase"
index: index:
help: "Los presupuestos participativos tienen distintas fases. Aquí puedes habilitar o deshabilitar fases y también personalizar cada una de las fases." help: "Los presupuestos participativos tienen distintas fases. Aquí puedes habilitar o deshabilitar fases y también personalizar cada una de las fases."
title: "Fases de %{budget}"
budget_investments: budget_investments:
index: index:
heading_filter_all: Todas las partidas heading_filter_all: Todas las partidas
@@ -296,12 +298,16 @@ es:
budget: Presupuesto budget: Presupuesto
groups: Grupos groups: Grupos
headings: Partidas headings: Partidas
phases: Fases
budgets: budgets:
continue: "Continuar a grupos" continue: "Continuar a grupos"
groups: groups:
continue: "Continuar a partidas" continue: "Continuar a partidas"
headings: headings:
continue: "Continuar a fases" continue: "Continuar a fases"
phases:
back: "Volver a partidas"
continue: "Finalizar"
milestones: milestones:
index: index:
table_id: "ID" table_id: "ID"

View File

@@ -77,6 +77,8 @@ namespace :admin do
resources :groups, only: [:index, :create, :edit, :update, :destroy] do resources :groups, only: [:index, :create, :edit, :update, :destroy] do
resources :headings, only: [:index, :create, :edit, :update, :destroy] resources :headings, only: [:index, :create, :edit, :update, :destroy]
end end
resources :phases, as: "budget_phases", only: [:index, :edit, :update]
end end
end end

View File

@@ -32,7 +32,7 @@ describe "Admin budget phases" do
within("tr", text: "Accepting projects") { click_link "Edit phase" } within("tr", text: "Accepting projects") { click_link "Edit phase" }
end end
expect(page).to have_css "h2", exact_text: "Edit Participatory budget - Accepting projects" expect(page).to have_css "h2", exact_text: "Edit phase - Accepting projects"
fill_in "Name", with: "My phase custom name" fill_in "Name", with: "My phase custom name"
click_button "Save changes" click_button "Save changes"

View File

@@ -0,0 +1,57 @@
require "rails_helper"
describe "Budgets wizard, phases step", :admin do
let(:budget) { create(:budget, :drafting) }
let!(:heading) { create(:budget_heading, budget: budget) }
describe "Index" do
scenario "back to a previous step" do
heading.update!(name: "Main Park")
visit admin_budgets_wizard_budget_budget_phases_path(budget)
within "#side_menu" do
expect(page).to have_css ".is-active", exact_text: "Participatory budgets"
end
click_link "Go back to headings"
expect(page).to have_css "tr", text: "Main Park"
expect(page).to have_css ".creation-timeline"
end
end
describe "Edit" do
scenario "update phase" do
visit admin_budgets_wizard_budget_budget_phases_path(budget)
expect(page).to have_css ".creation-timeline"
within("tr", text: "Selecting projects") { click_link "Edit phase" }
fill_in "Name", with: "Choosing projects"
click_button "Save changes"
expect(page).to have_content "Changes saved"
expect(page).to have_css ".creation-timeline"
within_table("Phases") { expect(page).to have_content "Choosing projects" }
end
scenario "submit the form with errors and then without errors" do
phase = budget.phases.accepting
visit edit_admin_budgets_wizard_budget_budget_phase_path(budget, phase)
fill_in "Name", with: ""
click_button "Save changes"
expect(page).to have_css "#error_explanation"
expect(page).to have_css ".creation-timeline"
fill_in "Name", with: "Welcoming projects"
click_button "Save changes"
expect(page).to have_content "Changes saved"
expect(page).to have_css ".creation-timeline"
within_table("Phases") { expect(page).to have_content "Welcoming projects" }
end
end
end