Class: Decidim::Elections::Seeds
- Inherits:
-
Seeds
- Object
- Seeds
- Decidim::Elections::Seeds
- Defined in:
- lib/decidim/elections/seeds.rb
Instance Attribute Summary collapse
-
#participatory_space ⇒ Object
readonly
Returns the value of attribute participatory_space.
Instance Method Summary collapse
- #admin_user ⇒ Object
- #call ⇒ Object
- #create_component! ⇒ Object
- #create_election!(component:) ⇒ Object
- #create_questions_for!(election) ⇒ Object
- #create_voters_for!(election) ⇒ Object
-
#initialize(participatory_space:) ⇒ Seeds
constructor
A new instance of Seeds.
- #organization ⇒ Object
Constructor Details
#initialize(participatory_space:) ⇒ Seeds
Returns a new instance of Seeds.
10 11 12 |
# File 'lib/decidim/elections/seeds.rb', line 10 def initialize(participatory_space:) @participatory_space = participatory_space end |
Instance Attribute Details
#participatory_space ⇒ Object (readonly)
Returns the value of attribute participatory_space.
8 9 10 |
# File 'lib/decidim/elections/seeds.rb', line 8 def participatory_space @participatory_space end |
Instance Method Details
#admin_user ⇒ Object
105 106 107 |
# File 'lib/decidim/elections/seeds.rb', line 105 def admin_user @admin_user ||= Decidim::User.find_by(organization:, email: "admin@example.org") end |
#call ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/decidim/elections/seeds.rb', line 14 def call component = create_component! number_of_records.times do election = create_election!(component:) create_questions_for!(election) if ::Faker::Boolean.boolean(true_ratio: 0.3) create_voters_for!(election) if election.census_manifest == "token_csv" end end |
#create_component! ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/decidim/elections/seeds.rb', line 24 def create_component! params = { name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :elections).i18n_name, published_at: Time.current, manifest_name: :elections, participatory_space: } Decidim.traceability.perform_action!( "publish", Decidim::Component, admin_user, visibility: "all" ) do Decidim::Component.create!(params) end end |
#create_election!(component:) ⇒ Object
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 |
# File 'lib/decidim/elections/seeds.rb', line 42 def create_election!(component:) census_manifest = %w(internal_users token_csv).sample handlers = ["", "id_documents", "postal_letter", "csv_census", "dummy_authorization_handler", "ephemeral_dummy_authorization_handler", "another_dummy_authorization_handler", "sms"] census_settings = census_manifest == "internal_users" ? { "verification_handlers" => handlers.sample(rand(1..2)) } : {} params = { title: Decidim::Faker::Localized.sentence(word_count: 2), description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do Decidim::Faker::Localized.paragraph(sentence_count: 3) end, component:, start_at: Time.current, end_at: 1.day.from_now, census_manifest:, census_settings: } Decidim.traceability.perform_action!( "publish", Decidim::Elections::Election, admin_user, visibility: "all" ) do Decidim::Elections::Election.create!(params) end end |
#create_questions_for!(election) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/decidim/elections/seeds.rb', line 70 def create_questions_for!(election) number_of_records.times do |position| question = Decidim::Elections::Question.create!( election:, position:, question_type: %w(single_option multiple_option).sample, body: Decidim::Faker::Localized.sentence(word_count: 4), description: Decidim::Faker::Localized.paragraph ) rand(2..4).times do Decidim::Elections::ResponseOption.create!( question:, body: Decidim::Faker::Localized.sentence(word_count: 2) ) end end end |
#create_voters_for!(election) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/decidim/elections/seeds.rb', line 89 def create_voters_for!(election) number_of_records.times do |i| Decidim::Elections::Voter.create!( election: election, data: { "email" => "user#{i + 1}@example.org", "token" => SecureRandom.hex(6).upcase } ) end end |
#organization ⇒ Object
101 102 103 |
# File 'lib/decidim/elections/seeds.rb', line 101 def organization @organization ||= Decidim::Organization.first end |