Skip to content

Commit

Permalink
Extract methods from accountabilty seeds (decidim#13032)
Browse files Browse the repository at this point in the history
* Inherit from Decidim::Seeds in Accountability seeds

* Extract create_component! to its own method

* Extract create_statuses! to its own method

* Extract create_categories! to its own method

* Extract create_result! to its own method

* Fix rubocop offenses
  • Loading branch information
andreslucena authored Jul 9, 2024
1 parent 629fb4a commit a118398
Showing 1 changed file with 76 additions and 63 deletions.
139 changes: 76 additions & 63 deletions decidim-accountability/lib/decidim/accountability/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@

module Decidim
module Accountability
class Seeds
class Seeds < Decidim::Seeds
attr_reader :participatory_space

def initialize(participatory_space:)
@participatory_space = participatory_space
end

def call
admin_user = Decidim::User.find_by(
organization: participatory_space.organization,
email: "[email protected]"
)
component = create_component!

create_statuses!(component:)

3.times do
categories = create_categories!

categories.each do |category|
create_result!(component:, category:)
end
end
end

def create_component!
params = {
name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :accountability).i18n_name,
manifest_name: :accountability,
Expand All @@ -29,81 +38,85 @@ def call
}
}

component = Decidim.traceability.perform_action!("publish", Decidim::Component, admin_user, visibility: "all") do
Decidim.traceability.perform_action!("publish", Decidim::Component, admin_user, visibility: "all") do
Decidim::Component.create!(params)
end
end

def create_statuses!(component:)
5.times do |i|
Decidim::Accountability::Status.create!(
component:,
name: Decidim::Faker::Localized.word,
key: "status_#{i}"
)
end
end

3.times do
parent_category = participatory_space.categories.sample
categories = [parent_category]
def create_categories!
parent_category = participatory_space.categories.sample
categories = [parent_category]

2.times do
categories << Decidim::Category.create!(
name: Decidim::Faker::Localized.sentence(word_count: 5),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end,
parent: parent_category,
participatory_space:
)
end
2.times do
categories << Decidim::Category.create!(
name: Decidim::Faker::Localized.sentence(word_count: 5),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end,
parent: parent_category,
participatory_space:
)
end

categories.each do |category|
result = Decidim.traceability.create!(
Decidim::Accountability::Result,
admin_user,
{
component:,
scope: participatory_space.organization.scopes.sample,
category:,
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end
},
visibility: "all"
)
categories
end

def create_result!(component:, category:)
result = Decidim.traceability.create!(
Decidim::Accountability::Result,
admin_user,
{
component:,
scope: participatory_space.organization.scopes.sample,
category:,
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end
},
visibility: "all"
)

Decidim::Comments::Seed.comments_for(result)

3.times do
child_result = Decidim.traceability.create!(
Decidim::Accountability::Result,
admin_user,
{
component:,
parent: result,
start_date: Time.zone.today,
end_date: Time.zone.today + 10,
status: Decidim::Accountability::Status.all.sample,
progress: rand(1..100),
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end
},
visibility: "all"
)

rand(0..5).times do |i|
child_result.timeline_entries.create!(
entry_date: child_result.start_date + i.days,
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.paragraph(sentence_count: 1)
)
Decidim::Comments::Seed.comments_for(result)

3.times do
child_result = Decidim.traceability.create!(
Decidim::Accountability::Result,
admin_user,
{
component:,
parent: result,
start_date: Time.zone.today,
end_date: Time.zone.today + 10,
status: Decidim::Accountability::Status.all.sample,
progress: rand(1..100),
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end
},
visibility: "all"
)

Decidim::Comments::Seed.comments_for(child_result)
end
rand(0..5).times do |i|
child_result.timeline_entries.create!(
entry_date: child_result.start_date + i.days,
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.paragraph(sentence_count: 1)
)
end

Decidim::Comments::Seed.comments_for(child_result)
end
end
end
Expand Down

0 comments on commit a118398

Please sign in to comment.