Use index_with to simplify hash generation

This method was introduced in Rails 6.0. It can be used to take an array
and create a hash where the elements of the array are the indexes of the
hash.
This commit is contained in:
Javi Martín
2022-07-26 02:34:17 +02:00
parent eca1714a26
commit 5b844bf231
4 changed files with 32 additions and 32 deletions

View File

@@ -17,7 +17,7 @@ module Globalize
end
def Globalize.set_fallbacks_to_all_available_locales
Globalize.fallbacks = I18n.available_locales.each_with_object({}) do |locale, fallbacks|
fallbacks[locale] = (I18n.fallbacks[locale] + I18n.available_locales).uniq
Globalize.fallbacks = I18n.available_locales.index_with do |locale|
(I18n.fallbacks[locale] + I18n.available_locales).uniq
end
end

View File

@@ -1,33 +1,33 @@
section "Creating Admin Notifications & Templates" do
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") }
end
).merge(link: "#{Setting["url"]}/proposals", segment_recipient: "administrators")
).deliver
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.help.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.help.#{attribute}") }
end
).merge(link: "https://crwd.in/consul", segment_recipient: "administrators")
).deliver
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.map.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.map.#{attribute}") }
end
).merge(segment_recipient: "administrators")
).deliver
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.budget.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.budget.#{attribute}") }
end
).merge(segment_recipient: "administrators", sent_at: nil)
)
end

View File

@@ -9,9 +9,9 @@ section "Creating header and cards for the homepage" do
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
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.header.#{attribute}") }
end
).merge(
link_url: "http://consulproject.org/",
header: true,
@@ -21,9 +21,9 @@ section "Creating header and cards for the homepage" do
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
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.debate.#{attribute}") }
end
).merge(
link_url: "https://youtu.be/zU_0UN4VajY",
header: false,
@@ -33,9 +33,9 @@ section "Creating header and cards for the homepage" do
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
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.proposal.#{attribute}") }
end
).merge(
link_url: "https://youtu.be/ZHqBpT4uCoM",
header: false,
@@ -45,9 +45,9 @@ section "Creating header and cards for the homepage" do
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
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.budget.#{attribute}") }
end
).merge(
link_url: "https://youtu.be/igQ8KGZdk9c",
header: false,

View File

@@ -16,7 +16,7 @@ shared_examples_for "globalizable" do |factory_name|
record.update!(attribute => "In English")
I18n.with_locale(:es) do
record.update!(required_fields.map { |field| [field, "En español"] }.to_h)
record.update!(required_fields.index_with("En español"))
record.update!(attribute => "En español")
end
@@ -26,7 +26,7 @@ shared_examples_for "globalizable" do |factory_name|
describe "Add a translation" do
it "Maintains existing translations" do
record.update!(translations_attributes: [
{ locale: :fr }.merge(fields.map { |field| [field, "En Français"] }.to_h)
{ locale: :fr }.merge(fields.index_with("En Français"))
])
record.reload
@@ -37,7 +37,7 @@ shared_examples_for "globalizable" do |factory_name|
it "Works with non-underscored locale name" do
record.update!(translations_attributes: [
{ locale: :"pt-BR" }.merge(fields.map { |field| [field, "Português"] }.to_h)
{ locale: :"pt-BR" }.merge(fields.index_with("Português"))
])
record.reload
@@ -63,7 +63,7 @@ shared_examples_for "globalizable" do |factory_name|
record.reload
record.update!(translations_attributes: [
{ locale: :de }.merge(fields.map { |field| [field, "Deutsche Sprache"] }.to_h)
{ locale: :de }.merge(fields.index_with("Deutsche Sprache"))
])
record.reload
@@ -102,7 +102,7 @@ shared_examples_for "globalizable" do |factory_name|
record.reload
record.update!(translations_attributes: [
{ id: record.translations.first.id }.merge(fields.map { |field| [field, "Actualizado"] }.to_h)
{ id: record.translations.first.id }.merge(fields.index_with("Actualizado"))
])
record.reload
@@ -155,7 +155,7 @@ shared_examples_for "globalizable" do |factory_name|
describe "Fallbacks" do
before do
I18n.with_locale(:de) do
record.update!(required_fields.map { |field| [field, "Deutsche Sprache"] }.to_h)
record.update!(required_fields.index_with("Deutsche Sprache"))
record.update!(attribute => "Deutsche Sprache")
end
end