Skip to content

Commit

Permalink
Make dev seeds independent on available locales
Browse files Browse the repository at this point in the history
Some developers work on CONSUL installations where Spanish and/or
English aren't part of the available locales. In those cases, the
`dev_seed` task was crashing because we were using attributes like
`name_en` and `name_es`.

So we're using attributes for random locales instead.
  • Loading branch information
javierm committed Nov 26, 2021
1 parent f3f1712 commit 975dde9
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 161 deletions.
41 changes: 41 additions & 0 deletions config/locales/en/seeds.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
en:
seeds:
admin_notifications:
budget:
title: "We are closing the Participatory Budget!!"
body: "Hurry up and create a last proposal before it ends next in few days!"
help:
title: "Help us translate consul"
body: "If you are proficient in a language, please help us translate consul!."
map:
title: "You can now geolocate proposals & investments"
body: "When you create a proposal or investment you now can specify a point on a map."
proposal:
title: "Do you have a proposal?"
body: "Remember you can create a proposal with your ideas and people will discuss & support it."
settings:
official_level_1_name: Official position 1
official_level_2_name: Official position 2
Expand All @@ -14,6 +27,27 @@ en:
organizations:
human_rights: Human Rights
neighborhood_association: Neighborhood Association
cards:
budget:
title: "How do participatory budgets work?"
description: "Participatory budgets allow citizens to propose and decide directly how to spend part of the budget, with monitoring and rigorous evaluation of proposals by the institution. Maximum effectiveness and control with satisfaction for everyone."
link_text: "More about Participatory budgets"
label: "Participatory budgets"
debate:
title: "How do debates work?"
description: "Anyone can open threads on any subject, creating separate spaces where people can discuss the proposed topic. Debates are valued by everybody, to highlight the most important issues."
link_text: "More about debates"
label: "Debates"
header:
title: "CONSUL"
description: "Free software for citizen participation."
link_text: "More information"
label: "Welcome to"
proposal:
title: "How do citizen proposals work?"
description: "A space for everyone to create a citizen's proposal and seek supports. Proposals which reach to enough supports will be voted and so, together we can decide the issues that matter to us."
link_text: "More about proposals"
label: "Citizen proposals"
categories:
associations: Associations
culture: Culture
Expand All @@ -36,6 +70,9 @@ en:
groups:
all_city: All City
districts: Districts
phases:
description: "Description in %{language}"
summary: "Summary in %{language}"
valuator_groups:
culture_and_sports: Culture & Sports
gender_and_diversity: Gender & Diversity Policies
Expand All @@ -46,6 +83,10 @@ en:
bidding: Bidding
executing_project: Executing the project
executed: Executed
legislation:
draft_versions:
title: "Version %{number}"
body: "Draft version in %{language}"
polls:
current_poll: "Current Poll"
current_poll_geozone_restricted: "Current Poll Geozone Restricted"
Expand Down
41 changes: 41 additions & 0 deletions config/locales/es/seeds.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
es:
seeds:
admin_notification:
budget:
title: "Últimos días para crear proyectos de Presupuestos Participativos"
body: "Quedan pocos dias para que se cierre el plazo de presentación de proyectos de inversión para los presupuestos participativos!"
help:
title: "Ayúdanos a traducir CONSUL"
body: "Si dominas un idioma, ayúdanos a completar su traducción en CONSUL."
map:
title: "Ahora puedes geolocalizar propuestas y proyectos de inversión"
body: "Cuando crees una propuesta o proyecto de inversión podrás especificar su localización en el mapa"
proposal:
title: "Tienes una propuesta?"
body: "Recuerda que puedes crear propuestas y los ciudadanos las debatirán y apoyarán."
settings:
official_level_1_name: Cargo oficial 1
official_level_2_name: Cargo oficial 2
Expand All @@ -14,6 +27,27 @@ es:
organizations:
human_rights: Derechos Humanos
neighborhood_association: Asociación Vecinal
cards:
budget:
title: "¿Cómo funcionan los propuestos participativos?"
description: "Los presupuestos participativos permiten que los ciudadanos propongan y decidan directamente cómo gastar parte del presupuesto, con la supervisión y valoración de la institución. Máxima eficacia y control para la satisfacción de todos"
link_text: "Más sobre presupuestos participativos"
label: "Presupuestos participativos"
debate:
title: "¿Cómo funcionan los debates?"
description: "Cualquiera puede iniciar un debate sobre cualquier tema, creando un espacio separado donde compartir puntos de vista con otras personas. Los debates son valorados por todos para destacar los temas más importantes."
link_text: "Más sobre debates"
label: "Debates"
header:
title: "CONSUL"
description: "Software libre para la participación ciudadana."
link_text: "Más información"
label: "Bienvenido a"
proposal:
title: "¿Cómo funcionan las propuestas ciudadanas?"
description: "Un espacio para que el ciudadano cree una propuesta y busque apoyo. Las propuestas que obtengan el apoyo necesario serán votadas. Así juntos podemos decidir sobre los temas que nos importan."
link_text: "Más sobre propuestas"
label: "Propuestas ciudadanas"
categories:
associations: Asociaciones
culture: Cultura
Expand All @@ -36,6 +70,9 @@ es:
groups:
all_city: Toda la Ciudad
districts: Distritos
phases:
description: "Descripción en %{language}"
summary: "Resumen en %{language}"
valuator_groups:
culture_and_sports: Cultura y Deportes
gender_and_diversity: Políticas de Género y Diversidad
Expand All @@ -46,6 +83,10 @@ es:
bidding: Licitación
executing_project: Ejecutando el proyecto
executed: Ejecutado
legislation:
draft_versions:
title: "Versión %{number}"
body: "Borrador en %{language}"
polls:
current_poll: "Votación Abierta"
current_poll_geozone_restricted: "Votación Abierta restringida por geozona"
Expand Down
10 changes: 10 additions & 0 deletions db/dev_seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def random_locales
[I18n.default_locale, *(I18n.available_locales & %i[en es]), *I18n.available_locales.sample(4)].uniq.take(5)
end

def random_locales_attributes(**attribute_names_with_values)
random_locales.each_with_object({}) do |locale, attributes|
I18n.with_locale(locale) do
attribute_names_with_values.each do |attribute_name, value_proc|
attributes["#{attribute_name}_#{locale.to_s.underscore}"] = value_proc.call
end
end
end
end

require_relative "dev_seeds/settings"
require_relative "dev_seeds/geozones"
require_relative "dev_seeds/users"
Expand Down
54 changes: 20 additions & 34 deletions db/dev_seeds/admin_notifications.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
section "Creating Admin Notifications & Templates" do
AdminNotification.create!(
title_en: "Do you have a proposal?",
title_es: "Tienes una propuesta?",

body_en: "Remember you can create a proposal with your ideas and "\
"people will discuss & support it.",
body_es: "Recuerda que puedes crear propuestas y los ciudadanos las debatirán y apoyarán.",

link: "#{Setting["url"]}/proposals",
segment_recipient: "administrators"
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") }]
end.to_h
).merge(link: "#{Setting["url"]}/proposals", segment_recipient: "administrators")
).deliver

AdminNotification.create!(
title_en: "Help us translate consul",
title_es: "Ayúdanos a traducir CONSUL",

body_en: "If you are proficient in a language, please help us translate consul!.",
body_es: "Si dominas un idioma, ayúdanos a completar su traducción en CONSUL.",

link: "https://crwd.in/consul",
segment_recipient: "administrators"
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.help.#{attribute}") }]
end.to_h
).merge(link: "https://crwd.in/consul", segment_recipient: "administrators")
).deliver

AdminNotification.create!(
title_en: "You can now geolocate proposals & investments",
title_es: "Ahora puedes geolocalizar propuestas y proyectos de inversión",

body_en: "When you create a proposal or investment you now can specify a point on a map",
body_es: "Cuando crees una propuesta o proyecto de inversión podrás especificar "\
"su localización en el mapa",

segment_recipient: "administrators"
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.map.#{attribute}") }]
end.to_h
).merge(segment_recipient: "administrators")
).deliver

AdminNotification.create!(
title_en: "We are closing the Participatory Budget!!",
title_es: "Últimos días para crear proyectos de Presupuestos Participativos",

body_en: "Hurry up and create a last proposal before it ends next in few days!",
body_es: "Quedan pocos dias para que se cierre el plazo de presentación de proyectos de "\
"inversión para los presupuestos participativos!",

segment_recipient: "administrators",
sent_at: nil
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.budget.#{attribute}") }]
end.to_h
).merge(segment_recipient: "administrators", sent_at: nil)
)
end
87 changes: 40 additions & 47 deletions db/dev_seeds/budgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,75 +28,68 @@ def add_image_to(imageable)

section "Creating Budgets" do
Budget.create!(
name_en: "#{I18n.t("seeds.budgets.budget", locale: :en)} #{Date.current.year - 1}",
name_es: "#{I18n.t("seeds.budgets.budget", locale: :es)} #{Date.current.year - 1}",
currency_symbol: I18n.t("seeds.budgets.currency"),
phase: "finished",
published: true
{
currency_symbol: I18n.t("seeds.budgets.currency"),
phase: "finished",
published: true
}.merge(
random_locales_attributes(name: -> { "#{I18n.t("seeds.budgets.budget")} #{Date.current.year - 1}" })
)
)

Budget.create!(
name_en: "#{I18n.t("seeds.budgets.budget", locale: :en)} #{Date.current.year}",
name_es: "#{I18n.t("seeds.budgets.budget", locale: :es)} #{Date.current.year}",
currency_symbol: I18n.t("seeds.budgets.currency"),
phase: "accepting",
published: true
{
currency_symbol: I18n.t("seeds.budgets.currency"),
phase: "accepting",
published: true
}.merge(
random_locales_attributes(name: -> { "#{I18n.t("seeds.budgets.budget")} #{Date.current.year}" })
)
)

Budget.find_each do |budget|
budget.phases.each do |phase|
random_locales.map do |locale|
Globalize.with_locale(locale) do
phase.name = I18n.t("budgets.phase.#{phase.kind}")
phase.description = "Description for locale #{locale}"
phase.summary = "Summary for locale #{locale}"
phase.save!
end
end
phase.update!(random_locales_attributes(
name: -> { I18n.t("budgets.phase.#{phase.kind}") },
summary: -> { I18n.t("seeds.budgets.phases.summary", language: I18n.t("i18n.language.name")) },
description: -> { I18n.t("seeds.budgets.phases.description", language: I18n.t("i18n.language.name")) }
))
end
end

Budget.all.each do |budget|
city_group = budget.groups.create!(
name_en: I18n.t("seeds.budgets.groups.all_city", locale: :en),
name_es: I18n.t("seeds.budgets.groups.all_city", locale: :es)
random_locales_attributes(name: -> { I18n.t("seeds.budgets.groups.all_city") })
)

city_group.headings.create!(
name_en: I18n.t("seeds.budgets.groups.all_city", locale: :en),
name_es: I18n.t("seeds.budgets.groups.all_city", locale: :es),
price: 1000000,
population: 1000000,
latitude: "40.416775",
longitude: "-3.703790"
{
price: 1000000,
population: 1000000,
latitude: "40.416775",
longitude: "-3.703790"
}.merge(
random_locales_attributes(name: -> { I18n.t("seeds.budgets.groups.all_city") })
)
)

districts_group = budget.groups.create!(
name_en: I18n.t("seeds.budgets.groups.districts", locale: :en),
name_es: I18n.t("seeds.budgets.groups.districts", locale: :es)
random_locales_attributes(name: -> { I18n.t("seeds.budgets.groups.districts") })
)

[
{
name_en: I18n.t("seeds.geozones.north_district", locale: :en),
name_es: I18n.t("seeds.geozones.north_district", locale: :es),
random_locales_attributes(name: -> { I18n.t("seeds.geozones.north_district") }).merge(
population: 350000
},
{
name_en: I18n.t("seeds.geozones.west_district", locale: :en),
name_es: I18n.t("seeds.geozones.west_district", locale: :es),
population: 300000,
},
{
name_en: I18n.t("seeds.geozones.east_district", locale: :en),
name_es: I18n.t("seeds.geozones.east_district", locale: :es),
population: 200000,
},
{
name_en: I18n.t("seeds.geozones.central_district", locale: :en),
name_es: I18n.t("seeds.geozones.central_district", locale: :es),
population: 150000,
}
),
random_locales_attributes(name: -> { I18n.t("seeds.geozones.west_district") }).merge(
population: 300000
),
random_locales_attributes(name: -> { I18n.t("seeds.geozones.east_district") }).merge(
population: 200000
),
random_locales_attributes(name: -> { I18n.t("seeds.geozones.central_district") }).merge(
population: 150000
)
].each do |heading_params|
districts_group.headings.create!(heading_params.merge(
price: rand(5..10) * 100000,
Expand Down
15 changes: 9 additions & 6 deletions db/dev_seeds/legislation_processes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@

Legislation::Process.find_each do |process|
(1..3).each do |i|
process.draft_versions.create!(title_en: "Version #{i}",
title_es: "Versión #{i}",
body_en: ["Draft version in English",
*Faker::Lorem.paragraphs].join("\n\n"),
body_es: ["Versión borrador en Español",
*Faker::Lorem.paragraphs].join("\n\n"))
process.draft_versions.create!(random_locales_attributes(
title: -> { I18n.t("seeds.legislation.draft_versions.title", number: i) },
body: -> do
[
I18n.t("seeds.legislation.draft_versions.body", language: I18n.t("i18n.language.name")),
*Faker::Lorem.paragraphs
].join("\n\n")
end
))
end
end
end
Loading

0 comments on commit 975dde9

Please sign in to comment.