Allow editing the name of budget phases

Co-authored-by: decabeza <alberto@decabeza.es>
This commit is contained in:
Julian Herrero
2020-03-16 12:54:00 +01:00
committed by Javi Martín
parent d20e521ee9
commit 909071c48b
17 changed files with 146 additions and 11 deletions

View File

@@ -21,7 +21,7 @@ class Budgets::PhasesComponent < ApplicationComponent
end
def phase_dom_id(phase)
phases.index(phase) + 1
"#{phases.index(phase) + 1}-#{phase.name.parameterize}"
end
def prev_phase_dom_id(phase)

View File

@@ -210,6 +210,7 @@ class Budget < ApplicationRecord
Budget::Phase.create(
budget: self,
kind: phase,
name: I18n.t("budgets.phase.#{phase}"),
prev_phase: phases&.last,
starts_at: phases&.last&.ends_at || Date.current,
ends_at: (phases&.last&.ends_at || Date.current) + 1.month

View File

@@ -5,6 +5,7 @@ class Budget
PUBLISHED_PRICES_PHASES = %w[publishing_prices balloting reviewing_ballots finished].freeze
DESCRIPTION_MAX_LENGTH = 2000
translates :name, touch: true
translates :summary, touch: true
translates :description, touch: true
include Globalizable
@@ -14,6 +15,7 @@ class Budget
belongs_to :next_phase, class_name: self.name, inverse_of: :prev_phase
has_one :prev_phase, class_name: self.name, foreign_key: :next_phase_id, inverse_of: :next_phase
validates_translation :name, presence: true
validates_translation :description, length: { maximum: DESCRIPTION_MAX_LENGTH }
validates :budget, presence: true
validates :kind, presence: true, uniqueness: { scope: :budget }, inclusion: { in: ->(*) { PHASE_KINDS }}
@@ -34,10 +36,6 @@ class Budget
PHASE_KINDS[PHASE_KINDS.index(phase)..-1]
end
def name
Class.new.extend(ActionView::Helpers::TranslationHelper).t("budgets.phase.#{kind}")
end
def next_enabled_phase
next_phase&.enabled? ? next_phase : next_phase&.next_enabled_phase
end

View File

@@ -31,6 +31,10 @@
</div>
<%= f.translatable_fields do |translations_form| %>
<div class="small-12 column">
<%= translations_form.text_field :name, hint: t("admin.budget_phases.edit.name_help_text") %>
</div>
<div class="small-12 column">
<%= translations_form.text_area :description,
maxlength: Budget::Phase::DESCRIPTION_MAX_LENGTH,

View File

@@ -230,6 +230,7 @@ en:
ends_at: "End date"
starts_at: "Start date"
budget/phase/translation:
name: "Name"
description: "Description"
summary: "Summary"
comment:

View File

@@ -178,6 +178,7 @@ en:
duration: "Phase's duration"
duration_description: "The period of time this phase will be active."
enabled_help_text: This phase will be public in the budget's phases timeline, as well as active for any other purpose
name_help_text: "This is the title of the phase users will read on the header whenever this phase is active."
save_changes: Save changes
index:
help: "Participatory budgets have different phases. Here you can enable or disable phases and also customize each individual phase."

View File

@@ -230,6 +230,7 @@ es:
ends_at: "Fecha de fin"
starts_at: "Fecha de inicio"
budget/phase/translation:
name: "Nombre"
description: "Descripción"
summary: "Resumen"
comment:

View File

@@ -178,6 +178,7 @@ es:
duration: "Duración de la fase"
duration_description: "El período de tiempo que esta fase estará activa."
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."
save_changes: Guardar cambios
index:
help: "Los presupuestos participativos tienen distintas fases. Aquí puedes habilitar o deshabilitar fases y también personalizar cada una de las fases."

View File

@@ -43,6 +43,7 @@ section "Creating Budgets" do
budget.phases.each do |phase|
random_locales.map do |locale|
Globalize.with_locale(locale) do
phase.name = "Name for locale #{locale}"
phase.description = "Description for locale #{locale}"
phase.summary = "Summary for locale #{locale}"
phase.save!

View File

@@ -0,0 +1,7 @@
class AddNameToBudgetPhases < ActiveRecord::Migration[5.2]
def change
change_table :budget_phase_translations do |t|
t.string :name
end
end
end

View File

@@ -286,6 +286,7 @@ ActiveRecord::Schema.define(version: 2021_01_23_100638) do
t.datetime "updated_at", null: false
t.text "description"
t.text "summary"
t.string "name"
t.index ["budget_phase_id"], name: "index_budget_phase_translations_on_budget_phase_id"
t.index ["locale"], name: "index_budget_phase_translations_on_locale"
end

View File

@@ -42,4 +42,27 @@ namespace :budgets do
end
end
end
desc "Add name to existing budget phases"
task add_name_to_existing_phases: :environment do
ApplicationLogger.new.info "Adding names to budgets phases"
Budget::Phase.find_each do |phase|
if phase.translations.present?
phase.translations.each do |translation|
unless translation.name.present?
if I18n.available_locales.include? translation.locale
locale = translation.locale
else
locale = I18n.default_locale
end
translation.update!(name: I18n.t("budgets.phase.#{phase.kind}", locale: locale))
end
end
else
phase.translations.create!(name: I18n.t("budgets.phase.#{phase.kind}"), locale: I18n.default_locale)
end
end
end
end

View File

@@ -9,6 +9,7 @@ namespace :consul do
"db:load_sdg",
"db:calculate_tsv",
"budgets:set_published",
"budgets:phases_summary_to_description"
"budgets:phases_summary_to_description",
"budgets:add_name_to_existing_phases"
]
end

View File

@@ -83,6 +83,8 @@ describe "budget tasks" do
description_en: "English description",
description_es: "Spanish description",
description_fr: "French description",
name_es: "Spanish name",
name_fr: "French name",
summary_en: "English summary",
summary_fr: "French summary"
)
@@ -96,4 +98,62 @@ describe "budget tasks" do
expect(budget_phase.summary).to be nil
end
end
describe "add_name_to_existing_phases" do
let(:run_rake_task) do
Rake::Task["budgets:add_name_to_existing_phases"].reenable
Rake.application.invoke_task("budgets:add_name_to_existing_phases")
end
it "adds the name to existing budget phases" do
budget = create(:budget)
informing_phase = budget.phases.informing
accepting_phase = budget.phases.accepting
accepting_phase.update!(name_en: "Custom accepting", name_es: "Aceptando personalizado")
informing_phase.translations.create!(locale: :es, name: "temp")
informing_phase.translations.update_all(name: "")
expect(informing_phase.name_en).to eq ""
expect(informing_phase.name_es).to eq ""
expect(informing_phase.name_fr).to be nil
expect(accepting_phase.name_en).to eq "Custom accepting"
expect(accepting_phase.name_es).to eq "Aceptando personalizado"
expect(accepting_phase.name_fr).to be nil
run_rake_task
expect(informing_phase.reload.name_en).to eq "Information"
expect(informing_phase.reload.name_es).to eq "Información"
expect(informing_phase.reload.name_fr).to be nil
expect(accepting_phase.reload.name_en).to eq "Custom accepting"
expect(accepting_phase.reload.name_es).to eq "Aceptando personalizado"
expect(accepting_phase.reload.name_fr).to be nil
end
it "adds the name in default locale to existing translations no longer available" do
budget = create(:budget)
informing_phase = budget.phases.informing
obsolete_translation = informing_phase.translations.build(locale: :fiction)
obsolete_translation.save!(validate: false)
expect(obsolete_translation.reload.name).to be nil
run_rake_task
expect(obsolete_translation.reload.name).to eq "Information"
end
it "adds a default translation to phases with no translations" do
budget = create(:budget)
informing_phase = budget.phases.informing
informing_phase.translations.destroy_all
expect(informing_phase.reload.name).to be nil
run_rake_task
expect(informing_phase.reload.name).to eq "Information"
end
end
end

View File

@@ -21,5 +21,24 @@ describe "Admin budget phases" do
expect(budget.current_phase.description).to include("New description of the phase.")
expect(budget.current_phase.enabled).to be(false)
end
scenario "Show default phase name or custom if present" do
visit edit_admin_budget_path(budget)
within_table "Phases" do
expect(page).to have_content "Accepting projects"
expect(page).not_to have_content "My phase custom name"
within("tr", text: "Accepting projects") { click_link "Edit phase" }
end
fill_in "Name", with: "My phase custom name"
click_button "Save changes"
within_table "Phases" do
expect(page).to have_content "My phase custom name"
expect(page).not_to have_content "Accepting projects"
end
end
end
end

View File

@@ -416,7 +416,7 @@ describe "Admin edit translatable records", :admin do
let(:translatable) { create(:budget).phases.last }
scenario "Shows first available fallback" do
translatable.update!({ description_fr: "Phase en Français", summary_fr: "Phase résumé" })
translatable.update!({ name_fr: "Name en Français", description_fr: "Phase en Français" })
visit edit_admin_budget_budget_phase_path(translatable.budget, translatable)
@@ -428,8 +428,9 @@ describe "Admin edit translatable records", :admin do
click_button "Save changes"
visit budgets_path
click_link "Name en Français"
expect(page).to have_content "Phase résumé"
expect(page).to have_content "Phase en Français"
end
end

View File

@@ -209,18 +209,18 @@ describe "Budgets" do
phases.informing.update!(starts_at: "30-12-2017", ends_at: "31-12-2017", enabled: true,
description: "Description of informing phase",
summary: "<p>This is the summary for informing phase</p>")
name: "Custom name for informing phase")
phases.accepting.update!(starts_at: "01-01-2018", ends_at: "10-01-2018", enabled: true,
description: "Description of accepting phase",
summary: "This is the summary for accepting phase")
name: "Custom name for accepting phase")
phases.reviewing.update!(starts_at: "11-01-2018", ends_at: "20-01-2018", enabled: false,
description: "Description of reviewing phase")
phases.selecting.update!(starts_at: "21-01-2018", ends_at: "01-02-2018", enabled: true,
description: "Description of selecting phase",
summary: "This is the summary for selecting phase")
name: "Custom name for selecting phase")
phases.valuating.update!(starts_at: "10-02-2018", ends_at: "20-02-2018", enabled: false,
description: "Description of valuating phase")
@@ -260,6 +260,21 @@ describe "Budgets" do
expect(page).to have_content "March 21, 2018 - March 29, 2018"
expect(page).to have_css(".tabs-panel.is-active", count: 1)
within("#budget_phases_tabs") do
expect(page).to have_link "Custom name for informing phase"
expect(page).to have_link "Custom name for accepting phase"
expect(page).to have_link "Custom name for selecting phase"
expect(page).to have_link phases.balloting.name
expect(page).to have_link "Current phase #{phases.finished.name}"
end
click_link "Custom name for accepting phase"
within("#2-custom-name-for-accepting-phase") do
expect(page).to have_link("Previous phase", href: "#1-custom-name-for-informing-phase")
expect(page).to have_link("Next phase", href: "#3-custom-name-for-selecting-phase")
end
end
context "Index map" do