Class: Decidim::Accountability::Seeds

Inherits:
Seeds
  • Object
show all
Defined in:
lib/decidim/accountability/seeds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(participatory_space:) ⇒ Seeds

Returns a new instance of Seeds.



10
11
12
# File 'lib/decidim/accountability/seeds.rb', line 10

def initialize(participatory_space:)
  @participatory_space = participatory_space
end

Instance Attribute Details

#participatory_spaceObject (readonly)

Returns the value of attribute participatory_space.



8
9
10
# File 'lib/decidim/accountability/seeds.rb', line 8

def participatory_space
  @participatory_space
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/decidim/accountability/seeds.rb', line 14

def call
  component = create_component!

  create_statuses!(component:)

  3.times do
    categories = create_categories!

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

#create_categories!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/decidim/accountability/seeds.rb', line 56

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

  categories
end

#create_component!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/decidim/accountability/seeds.rb', line 28

def create_component!
  params = {
    name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :accountability).i18n_name,
    manifest_name: :accountability,
    published_at: Time.current,
    participatory_space:,
    settings: {
      intro: Decidim::Faker::Localized.wrapped("<p>", "</p>") { Decidim::Faker::Localized.sentence(word_count: 4) },
      scopes_enabled: true,
      scope_id: participatory_space.scope&.id
    }
  }

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

#create_result!(component:, category:) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/decidim/accountability/seeds.rb', line 74

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

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

#create_statuses!(component:) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/decidim/accountability/seeds.rb', line 46

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