Class: Decidim::Votings::Seeds

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

Instance Method Summary collapse

Instance Method Details

#callObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/decidim/votings/seeds.rb', line 9

def call
  organization = Decidim::Organization.first

  3.times do |n|
    params = {
      organization:,
      title: Decidim::Faker::Localized.sentence(word_count: 5),
      slug: Decidim::Faker::Internet.unique.slug(words: nil, glue: "-"),
      description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
        Decidim::Faker::Localized.paragraph(sentence_count: 3)
      end,
      scope: n.positive? ? nil : Decidim::Scope.all.sample,
      banner_image: ::Faker::Boolean.boolean(true_ratio: 0.5) ? banner_image : nil, # Keep after organization
      published_at: 2.weeks.ago,
      start_time: n.weeks.from_now,
      end_time: (n + 1).weeks.from_now + 4.hours,
      voting_type: Decidim::Votings::Voting.voting_types.values.sample,
      promoted: n.odd?
    }

    voting = Decidim.traceability.perform_action!(
      "publish",
      Decidim::Votings::Voting,
      organization.users.first,
      visibility: "all"
    ) do
      Decidim::Votings::Voting.create!(params)
    end
    voting.add_to_index_as_search_resource

    unless voting.online_voting?
      3.times do
        params = {
          voting:,
          title: Decidim::Faker::Localized.sentence(word_count: 5),
          address: ::Faker::Address.full_address,
          latitude: ::Faker::Address.latitude,
          longitude: ::Faker::Address.longitude,
          location: Decidim::Faker::Localized.sentence,
          location_hints: Decidim::Faker::Localized.sentence
        }

        polling_station = Decidim.traceability.create!(
          Decidim::Votings::PollingStation,
          organization.users.first,
          params,
          visibility: "all"
        )

        email = "voting_#{voting.id}_president_#{polling_station.id}@example.org"

        user = Decidim::User.find_or_initialize_by(email:)
        user.update!(
          name: ::Faker::Name.name,
          nickname: ::Faker::Twitter.unique.screen_name,
          password: "decidim123456789",
          organization:,
          confirmed_at: Time.current,
          locale: I18n.default_locale,
          tos_agreement: true
        )

        Decidim.traceability.create!(
          Decidim::Votings::PollingOfficer,
          organization.users.first,
          {
            voting:,
            user:,
            presided_polling_station: polling_station
          },
          visibility: "all"
        )
      end
    end

    landing_page_content_blocks = [:hero, :title, :related_elections, :polling_stations, :related_documents, :related_images, :stats, :metrics]

    landing_page_content_blocks.each.with_index(1) do |manifest_name, index|
      Decidim::ContentBlock.create(
        organization:,
        scope_name: :voting_landing_page,
        manifest_name:,
        weight: index,
        scoped_resource_id: voting.id,
        published_at: Time.current
      )
    end

    2.times do
      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,
        participatory_space: voting
      )
    end

    Decidim.component_manifests.each do |manifest|
      manifest.seed!(voting.reload)
    end

    unless voting.online_voting?
      voting.reload.published_elections.finished.each do |election|
        polling_officer = voting.polling_officers.sample
        ps_closure = Decidim::Votings::PollingStationClosure.create!(
          election:,
          polling_officer:,
          polling_station: polling_officer.polling_station,
          signed_at: Time.current,
          phase: :complete
        )

        valid_ballots = ::Faker::Number.number(digits: 3)
        Decidim::Elections::Result.create!(
          value: valid_ballots,
          closurable: ps_closure,
          question: nil,
          answer: nil,
          result_type: :valid_ballots
        )

        null_ballots = ::Faker::Number.number(digits: 1)
        Decidim::Elections::Result.create!(
          value: null_ballots,
          closurable: ps_closure,
          question: nil,
          answer: nil,
          result_type: :null_ballots
        )

        blank_ballots = ::Faker::Number.number(digits: 2)
        Decidim::Elections::Result.create!(
          value: blank_ballots,
          closurable: ps_closure,
          question: nil,
          answer: nil,
          result_type: :blank_ballots
        )

        Decidim::Elections::Result.create!(
          value: valid_ballots + null_ballots + blank_ballots,
          closurable: ps_closure,
          question: nil,
          answer: nil,
          result_type: :total_ballots
        )

        election.questions.each do |question|
          question_pending = valid_ballots
          question.answers.shuffle.each do |answer|
            answer_value = ::Faker::Number.between(from: 0, to: question_pending)
            Decidim::Elections::Result.create!(
              value: answer_value,
              closurable: ps_closure,
              question:,
              answer:,
              result_type: :valid_answers
            )
            question_pending -= answer_value
          end

          next unless question.nota_option?

          Decidim::Elections::Result.create!(
            value: question_pending,
            closurable: ps_closure,
            question:,
            answer: nil,
            result_type: :blank_answers
          )
        end
      end
    end

    (1..2).each do |i|
      ballot_style = voting.ballot_styles.create!(code: "DISTRICT#{i}")
      voting.elections.each do |election|
        election.questions.sample(1 + rand(election.questions.count)).each do |question|
          ballot_style.ballot_style_questions.create!(question:)
        end
      end
    end
  end
end