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)


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

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

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

  Decidim::EventsManager.publish(
    event: "decidim.events.meetings.meeting_created",
    event_class: Decidim::Meetings::CreateMeetingEvent,
    resource:,
    followers: resource.participatory_space.followers
  )

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

  Decidim::Comments::Seed.comments_for(resource)

  resource
end

#create_meeting_registration!(meeting:) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/decidim/meetings/seeds.rb', line 204

def create_meeting_registration!(meeting:)
  r = SecureRandom.hex(4)
  email = "meeting-registered-user-#{meeting.id}-#{r}@example.org"
  user = find_or_initialize_user_by(email:)

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

#create_questionnaire_for!(meeting:) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/decidim/meetings/seeds.rb', line 191

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



183
184
185
186
187
188
189
# File 'lib/decidim/meetings/seeds.rb', line 183

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
141
142
143
144
145
146
147
148
# 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
  registration_type = Decidim::Meetings::Meeting::REGISTRATION_TYPES.keys.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_type:,
    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(
               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(
               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(
               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

  params = case registration_type
           when :on_different_platform
             params.merge(registration_url: Faker::Internet.url)
           when :on_this_platform
             params.merge(registrations_enabled: true)
           else
             params # registration_disabled
           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 # official
  end
end