Files
nairobi/db/dev_seeds/legislation_processes.rb
Javi Martín 490fe5fd11 Make dev seeds independent on available locales
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.

We're using a proc so we don't have code like this all over the place:

random_locales.map do |locale|
  I18n.with_locale(locale) do
    phase.name = I18n.t("budgets.phase.#{phase.kind}")
    phase.save!
  end
end

This would make the code harder to read and would execute a `save!` once
per locale, which would make the task much slower.

We could also avoid the procs writing something like:

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, (i18n_key, i18n_args)|
        value = I18n.t(i18n_key, (i18n_args || {}).merge(language: I18n.t("i18n.language.name")))
        attributes["#{attribute_name}_#{locale.to_s.underscore}"] = value
      end
    end
  end
end

And calling the method with with:

random_locales_attributes(name: ["seeds.budgets.name", year: Date.current.year - 1])

However, this code would also be different that what we usually do, we'd
have to apply some magic to pass the `language:` parameter, and the
strings wouldn't be recognized by i18n-tasks, so we aren't sure we're
really gaining anything.
2021-12-14 20:05:07 +01:00

40 lines
2.1 KiB
Ruby

section "Creating collaborative legislation" do
9.times do |i|
Legislation::Process.create!(title: Faker::Lorem.sentence(word_count: 3).truncate(60),
description: Faker::Lorem.paragraphs.join("\n\n"),
summary: Faker::Lorem.paragraph,
additional_info: Faker::Lorem.paragraphs.join("\n\n"),
proposals_description: Faker::Lorem.paragraph,
start_date: Date.current + (i - 7).days,
end_date: Date.current + (i - 1).days,
debate_start_date: Date.current + (i - 7).days,
debate_end_date: Date.current + (i - 5).days,
proposals_phase_start_date: Date.current + (i - 7).days,
proposals_phase_end_date: Date.current + (i - 5).days,
draft_publication_date: Date.current + (i - 3).days,
allegations_start_date: Date.current + (i - 2).days,
allegations_end_date: Date.current + (i - 1).days,
result_publication_date: Date.current + i.days,
debate_phase_enabled: true,
allegations_phase_enabled: true,
draft_publication_enabled: true,
result_publication_enabled: true,
proposals_phase_enabled: true,
published: true)
end
Legislation::Process.find_each do |process|
(1..3).each do |i|
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