Class: Decidim::Core::Seeds

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

Instance Method Summary collapse

Instance Method Details

#callObject



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

def call
  print "Creating seeds for decidim-core...\n" unless Rails.env.test? # rubocop:disable Rails/Output

  Rails.application.reloader.reload! if Rails.application.reloader.check!
  reset_column_information

  ActiveJob::Base.queue_adapter = :inline

  organization = create_organization!

  if organization.top_scopes.none?
    province = create_scope_type!(name: "province", plural: "provinces")
    municipality = create_scope_type!(name: "municipality", plural: "municipalities")

    3.times do
      parent = create_scope!(scope_type: province, parent: nil)

      5.times do
        create_scope!(scope_type: municipality, parent:)
      end
    end
  end

  territorial = create_area_type!(name: "territorial", plural: "territorials")
  sectorial = create_area_type!(name: "sectorials", plural: "sectorials")

  3.times do
    create_area!(area_type: territorial)
  end

  5.times do
    create_area!(area_type: sectorial)
  end

  admin = find_or_initialize_user_by(email: "admin@example.org")
  admin.update!(
    admin: true,
    admin_terms_accepted_at: Time.current
  )

  ["user@example.org", "user2@example.org"].each do |email|
    find_or_initialize_user_by(email:)
  end

  regular_user = Decidim::User.find_or_initialize_by(email: "user@example.org")

  locked_user = find_or_initialize_user_by(email: "locked_user@example.org")

  locked_user.lock_access!

  Decidim::Messaging::Conversation.start!(
    originator: admin,
    interlocutors: [regular_user],
    body: "Hey! I am glad you like Decidim"
  )

  Decidim::User.find_each do |user|
    [nil, Time.current].each do |verified_at|
      create_user_group!(user:, verified_at:)
    end
  end

  oauth_application = Decidim::OAuthApplication.create!(
    organization:,
    name: "Test OAuth application",
    organization_name: "Example organization",
    organization_url: "http://www.example.org",
    redirect_uri: "https://www.example.org/oauth/decidim",
    scopes: "public"
  )

  oauth_application..attach(io: File.open(File.join(seeds_root, "homepage_image.jpg")), filename: "organization_logo.jpg", content_type: "image/jpeg")

  Decidim::ContentBlocksCreator.new(organization).create_default!

  hero_content_block = Decidim::ContentBlock.find_by(organization:, manifest_name: :hero, scope_name: :homepage)
  hero_content_block.images_container.background_image = create_blob!(seeds_file: "homepage_image.jpg", filename: "homepage_image.jpg", content_type: "image/jpeg")
  settings = {}
  welcome_text = Decidim::Faker::Localized.sentence(word_count: 5)
  settings = welcome_text.inject(settings) { |acc, (k, v)| acc.update("welcome_text_#{k}" => v) }
  hero_content_block.settings = settings
  hero_content_block.save!

  create_user_report!(reportable: Decidim::User.take, current_user: Decidim::User.take)
end

#create_area!(area_type:) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/decidim/core/seeds.rb', line 194

def create_area!(area_type:)
  Decidim::Area.create!(
    name: Decidim::Faker::Localized.word,
    area_type:,
    organization:
  )
end

#create_area_type!(name:, plural:) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/decidim/core/seeds.rb', line 186

def create_area_type!(name:, plural:)
  Decidim::AreaType.create!(
    name: Decidim::Faker::Localized.literal(name),
    plural: Decidim::Faker::Localized.literal(plural),
    organization:
  )
end

#create_organization!Object



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

def create_organization!
  smtp_label = ENV.fetch("SMTP_FROM_LABEL", ::Faker::Twitter.unique.screen_name)
  smtp_email = ENV.fetch("SMTP_FROM_EMAIL", ::Faker::Internet.email)

  primary_color, secondary_color, tertiary_color = [
    ["#bf4044", "#09780e", "#3584e4"],
    ["#09780e", "#4448bb", "#da565b"],
    ["#bf4086", "#487511", "#5384ac"],
    ["#086263", "#811380", "#abac53"],
    ["#5340bf", "#a82c2c", "#40bf53"]
  ].sample

  colors = {
    primary: primary_color,
    secondary: secondary_color,
    tertiary: tertiary_color
  }

  Decidim::Organization.first || Decidim::Organization.create!(
    name: Decidim::Faker::Localized.company,
    twitter_handler: ::Faker::Hipster.word,
    facebook_handler: ::Faker::Hipster.word,
    instagram_handler: ::Faker::Hipster.word,
    youtube_handler: ::Faker::Hipster.word,
    github_handler: ::Faker::Hipster.word,
    smtp_settings: {
      from: "#{smtp_label} <#{smtp_email}>",
      from_email: smtp_email,
      from_label: smtp_label,
      user_name: ENV.fetch("SMTP_USERNAME", ::Faker::Twitter.unique.screen_name),
      encrypted_password: Decidim::AttributeEncryptor.encrypt(ENV.fetch("SMTP_PASSWORD", ::Faker::Internet.password(min_length: 8))),
      address: ENV.fetch("SMTP_ADDRESS", nil) || ENV.fetch("DECIDIM_HOST", "localhost"),
      port: ENV.fetch("SMTP_PORT", nil) || ENV.fetch("DECIDIM_SMTP_PORT", "25")
    },
    host: ENV.fetch("DECIDIM_HOST", "localhost"),
    secondary_hosts: ENV.fetch("DECIDIM_HOST", "localhost") == "localhost" ? ["0.0.0.0", "127.0.0.1"] : nil,
    external_domain_allowlist: ["decidim.org", "github.com"],
    description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.sentence(word_count: 15)
    end,
    default_locale: Decidim.default_locale,
    available_locales: Decidim.available_locales,
    reference_prefix: ::Faker::Name.suffix,
    available_authorizations: Decidim.authorization_workflows.map(&:name),
    users_registration_mode: :enabled,
    tos_version: Time.current,
    badges_enabled: true,
    user_groups_enabled: true,
    send_welcome_notification: true,
    file_upload_settings: Decidim::OrganizationSettings.default(:upload),
    colors:
  )
end

#create_scope!(scope_type:, parent:) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/decidim/core/seeds.rb', line 170

def create_scope!(scope_type:, parent:)
  n = rand(99_999)
  code = parent.nil? ? "#{::Faker::Address.country_code}_#{n}" : "#{parent.code}-#{::Faker::Address.postcode}_#{n}"
  name = [::Faker::Address.state, ::Faker::Address.city].sample

  Decidim::Scope.create!(
    name: Decidim::Faker::Localized.literal(name),
    code:,
    scope_type:,
    organization:,
    parent:
  )
rescue ActiveRecord::RecordInvalid
  retry
end

#create_scope_type!(name:, plural:) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/decidim/core/seeds.rb', line 162

def create_scope_type!(name:, plural:)
  Decidim::ScopeType.create!(
    name: Decidim::Faker::Localized.literal(name),
    plural: Decidim::Faker::Localized.literal(plural),
    organization:
  )
end

#create_user_group!(user:, verified_at:) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/decidim/core/seeds.rb', line 202

def create_user_group!(user:, verified_at:)
  user_group = Decidim::UserGroup.create!(
    name: ::Faker::Company.unique.name,
    nickname: ::Faker::Twitter.unique.screen_name,
    email: ::Faker::Internet.email,
    confirmed_at: Time.current,
    extended_data: {
      document_number: ::Faker::Number.number(digits: 10).to_s,
      phone: ::Faker::PhoneNumber.phone_number,
      verified_at:
    },
    decidim_organization_id: user.organization.id
  )

  Decidim::UserGroupMembership.create!(
    user:,
    role: "creator",
    user_group:
  )
end

#reset_column_informationObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/decidim/core/seeds.rb', line 97

def reset_column_information
  # Since we usually migrate and seed in the same process, make sure
  # that we do not have invalid or cached information after a migration.
  decidim_tables = ActiveRecord::Base.connection.tables.select do |table|
    table.starts_with?("decidim_")
  end
  decidim_tables.map do |table|
    table.tr("_", "/").classify.safe_constantize
  end.compact.each(&:reset_column_information)
end