Files
nairobi/db/dev_seeds/sdg.rb
Javi Martín 7aee4f6241 Add SDG phases cards management
These cards will be displayed in the SDG homepage.

Note there seems to be a strange behavior in cancancan. If we define
these rules:

can :manage, Widget::Card, page_type: "SDG::Phase"
can :manage, Widget::Card

The expected behavior is the first rule will always be ignored because
the second one overwrites it. However, when creating a new card with
`load_and_authorize_resource` will automatically add `page_type:
"SDG::Phase"`.

Similarly, if we do something like:

can :manage, Widget::Card, id: 3
can :manage, Widget::Card

Then the new card will have `3` as an ID.

Maybe upgrading cancancan solves the issue; we haven't tried it. For now
we're defining a different rule when creating widget cards.
2021-01-14 17:40:02 +01:00

39 lines
1.4 KiB
Ruby

section "Creating Sustainable Development Goals" do
load Rails.root.join("db", "sdg.rb")
SDG::Target.sample(30).each do |target|
title = "Title for default locale"
description = "Description for default locale"
rand(2..3).times do |n|
local_target = SDG::LocalTarget.create!(code: "#{target.code}.#{n + 1}",
title: title,
description: description,
target: target)
random_locales.map do |locale|
Globalize.with_locale(locale) do
local_target.title = "Title for locale #{locale}"
local_target.description = "Description for locale #{locale}"
local_target.save!
end
end
end
end
relatables = [Debate, Proposal, Poll, Legislation::Process, Budget::Investment]
relatables.map { |relatable| relatable.sample(5) }.flatten.each do |relatable|
Array(SDG::Goal.sample(rand(1..3))).each do |goal|
target = goal.targets.sample
local_target = target.local_targets.sample
relatable.sdg_goals << goal
relatable.sdg_targets << target
relatable.sdg_local_targets << local_target if local_target.present?
end
end
end
section "Creating SDG homepage cards" do
SDG::Phase.all.each do |phase|
Widget::Card.create!(cardable: phase, title: "#{phase.title} card")
end
end