Class: Decidim::Meetings::Seeds

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(participatory_space:) ⇒ Seeds

Returns a new instance of Seeds.



11
12
13
# File 'lib/decidim/meetings/seeds.rb', line 11

def initialize(participatory_space:)
  @participatory_space = participatory_space
end

Instance Attribute Details

#participatory_spaceObject (readonly)

Returns the value of attribute participatory_space.



9
10
11
# File 'lib/decidim/meetings/seeds.rb', line 9

def participatory_space
  @participatory_space
end

Instance Method Details

#callObject



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
# File 'lib/decidim/meetings/seeds.rb', line 15

def call
  component = create_component!

  2.times do
    create_meeting!(component:, type: :online)
    create_meeting!(component:, type: :online_live_event)
    create_meeting!(component:, type: :hybrid)
    meeting = create_meeting!(component:, type: :in_person)

    2.times do
      create_service!(meeting:)
    end

    create_questionnaire_for!(meeting:)

    2.times do |_n|
      create_meeting_registration!(meeting:)
    end

    create_attachments!(attached_to: meeting)
  end

  create_meeting!(component:, type: [:in_person, :online, :hybrid].sample, author_type: :user)
  create_meeting!(component:, type: [:in_person, :online, :hybrid].sample, author_type: :user_group)
end

#create_component!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/decidim/meetings/seeds.rb', line 41

def create_component!
  params = {
    name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :meetings).i18n_name,
    published_at: Time.current,
    manifest_name: :meetings,
    participatory_space:
  }

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

#create_meeting!(component:, type: :in_person, author_type: :official) ⇒ Decidim::Meeting

Create a meeting

Parameters:

  • component (Decidim::Component)

    The component where this class will be created

  • type (:in_person, :hybrid, :online, :online_live_event) (defaults to: :in_person)

    The meeting type

  • author_type (:official, :user, :user_group) (defaults to: :official)

    Which type the author of the meeting will be

Returns:

  • (Decidim::Meeting)


149
150
151
152
153
154
155
156
157
158
# File 'lib/decidim/meetings/seeds.rb', line 149

def create_meeting!(component:, type: :in_person, author_type: :official)
  params = meeting_params(component:, type:, author_type:)

  Decidim.traceability.create!(
    Decidim::Meetings::Meeting,
    admin_user,
    params,
    visibility: "all"
  )
end

#create_meeting_registration!(meeting:) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/decidim/meetings/seeds.rb', line 181

def create_meeting_registration!(meeting:)
  r = SecureRandom.hex(4)
  email = "meeting-registered-user-#{meeting.id}-#{r}@example.org"
  name = "#{::Faker::Name.name} #{meeting.id} #{r}"
  user = Decidim::User.find_or_initialize_by(email:)

  user.update!(
    password: "decidim123456789",
    name:,
    nickname: ::Faker::Twitter.unique.screen_name,
    organization:,
    tos_agreement: "1",
    confirmed_at: Time.current,
    personal_url: ::Faker::Internet.url,
    about: ::Faker::Lorem.paragraph(sentence_count: 2)
  )

  Decidim::Meetings::Registration.create!(
    meeting:,
    user:
  )
end

#create_questionnaire_for!(meeting:) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/decidim/meetings/seeds.rb', line 168

def create_questionnaire_for!(meeting:)
  Decidim::Forms::Questionnaire.create!(
    title: Decidim::Faker::Localized.paragraph,
    description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.paragraph(sentence_count: 3)
    end,
    tos: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.paragraph(sentence_count: 2)
    end,
    questionnaire_for: meeting
  )
end

#create_service!(meeting:) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/decidim/meetings/seeds.rb', line 160

def create_service!(meeting:)
  Decidim::Meetings::Service.create!(
    meeting:,
    title: Decidim::Faker::Localized.sentence(word_count: 2),
    description: Decidim::Faker::Localized.sentence(word_count: 5)
  )
end

#meeting_params(component:, type:, author_type:) ⇒ Object



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
# File 'lib/decidim/meetings/seeds.rb', line 59

def meeting_params(component:, type:, author_type:)
  start_time = ::Faker::Date.between(from: 20.weeks.ago, to: 20.weeks.from_now)
  end_time = start_time + [rand(1..4).hours, rand(1..20).days].sample

  params = {
    component:,
    scope: random_scope(participatory_space:),
    category: participatory_space.categories.sample,
    title: Decidim::Faker::Localized.sentence(word_count: 2),
    description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.paragraph(sentence_count: 3)
    end,
    location: Decidim::Faker::Localized.sentence,
    location_hints: Decidim::Faker::Localized.sentence,
    start_time:,
    end_time:,
    address: "#{::Faker::Address.street_address} #{::Faker::Address.zip} #{::Faker::Address.city}",
    latitude: ::Faker::Address.latitude,
    longitude: ::Faker::Address.longitude,
    registrations_enabled: [true, false].sample,
    available_slots: (10..50).step(10).to_a.sample,
    author: participatory_space.organization,
    registration_terms: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.paragraph(sentence_count: 3)
    end,
    published_at: ::Faker::Boolean.boolean(true_ratio: 0.8) ? Time.current : nil
  }

  params = case type
           when :hybrid
             params.merge(
               end_time: Time.zone.now + [rand(1..4).hours, rand(1..20).days].sample,
               type_of_meeting: :hybrid,
               online_meeting_url: "https://www.youtube.com/watch?v=f6JMgJAQ2tc",
               iframe_access_level: :all,
               iframe_embed_type: [:embed_in_meeting_page, :open_in_live_event_page, :open_in_new_tab].sample
             )
           when :online
             params.merge(
               end_time: Time.zone.now + [rand(1..4).hours, rand(1..20).days].sample,
               location: nil,
               location_hints: nil,
               latitude: nil,
               longitude: nil,
               type_of_meeting: :online,
               online_meeting_url: "https://www.youtube.com/watch?v=f6JMgJAQ2tc",
               iframe_access_level: :all,
               iframe_embed_type: [:embed_in_meeting_page, :open_in_live_event_page, :open_in_new_tab].sample
             )
           when :online_live_event
             params.merge(
               end_time: Time.zone.now + [rand(1..4).hours, rand(1..20).days].sample,
               location: nil,
               location_hints: nil,
               latitude: nil,
               longitude: nil,
               type_of_meeting: :online,
               online_meeting_url: "https://www.youtube.com/watch?v=f6JMgJAQ2tc",
               iframe_access_level: :all,
               iframe_embed_type: :open_in_live_event_page
             )
           else
             params # :in_person
           end

  case author_type
  when :user
    params.merge(
      author: Decidim::User.where(decidim_organization_id: participatory_space.decidim_organization_id).all.sample
    )
  when :user_group
    user_group = Decidim::UserGroup.where(decidim_organization_id: participatory_space.decidim_organization_id).verified.sample
    author = user_group.users.sample

    params.merge(
      author:,
      user_group:
    )
  else
    params # oficial
  end
end