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.
58 lines
1.6 KiB
Ruby
58 lines
1.6 KiB
Ruby
section "Creating header and cards for the homepage" do
|
|
def create_image_attachment(type)
|
|
{
|
|
cached_attachment: Rails.root.join("db/dev_seeds/images/#{type}_background.jpg"),
|
|
title: "#{type}_background.jpg",
|
|
user: User.first
|
|
}
|
|
end
|
|
|
|
Widget::Card.create!(
|
|
random_locales_attributes(
|
|
%i[title description link_text label].map do |attribute|
|
|
[attribute, -> { I18n.t("seeds.cards.header.#{attribute}") }]
|
|
end.to_h
|
|
).merge(
|
|
link_url: "http://consulproject.org/",
|
|
header: true,
|
|
image_attributes: create_image_attachment("header")
|
|
)
|
|
)
|
|
|
|
Widget::Card.create!(
|
|
random_locales_attributes(
|
|
%i[title description link_text label].map do |attribute|
|
|
[attribute, -> { I18n.t("seeds.cards.debate.#{attribute}") }]
|
|
end.to_h
|
|
).merge(
|
|
link_url: "https://youtu.be/zU_0UN4VajY",
|
|
header: false,
|
|
image_attributes: create_image_attachment("debate")
|
|
)
|
|
)
|
|
|
|
Widget::Card.create!(
|
|
random_locales_attributes(
|
|
%i[title description link_text label].map do |attribute|
|
|
[attribute, -> { I18n.t("seeds.cards.proposal.#{attribute}") }]
|
|
end.to_h
|
|
).merge(
|
|
link_url: "https://youtu.be/ZHqBpT4uCoM",
|
|
header: false,
|
|
image_attributes: create_image_attachment("proposal")
|
|
)
|
|
)
|
|
|
|
Widget::Card.create!(
|
|
random_locales_attributes(
|
|
%i[title description link_text label].map do |attribute|
|
|
[attribute, -> { I18n.t("seeds.cards.budget.#{attribute}") }]
|
|
end.to_h
|
|
).merge(
|
|
link_url: "https://youtu.be/igQ8KGZdk9c",
|
|
header: false,
|
|
image_attributes: create_image_attachment("budget")
|
|
)
|
|
)
|
|
end
|