From a1183981e4041d7c6802982e937b7704c17f0d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Pereira=20de=20Lucena?= Date: Tue, 9 Jul 2024 14:57:47 +0200 Subject: [PATCH] Extract methods from accountabilty seeds (#13032) * 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 --- .../lib/decidim/accountability/seeds.rb | 139 ++++++++++-------- 1 file changed, 76 insertions(+), 63 deletions(-) diff --git a/decidim-accountability/lib/decidim/accountability/seeds.rb b/decidim-accountability/lib/decidim/accountability/seeds.rb index f5e364e265d5d..6d71cbd36d31a 100644 --- a/decidim-accountability/lib/decidim/accountability/seeds.rb +++ b/decidim-accountability/lib/decidim/accountability/seeds.rb @@ -4,7 +4,7 @@ module Decidim module Accountability - class Seeds + class Seeds < Decidim::Seeds attr_reader :participatory_space def initialize(participatory_space:) @@ -12,11 +12,20 @@ def initialize(participatory_space:) end def call - admin_user = Decidim::User.find_by( - organization: participatory_space.organization, - email: "admin@example.org" - ) + 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, @@ -29,10 +38,12 @@ 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:, @@ -40,70 +51,72 @@ def call 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("

", "

") 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("

", "

") 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("

", "

") 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("

", "

") 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("

", "

") 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("

", "

") 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