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

@@ -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,