Skip to content

Commit

Permalink
Merge pull request #1781 from tactilenews/add_data_generation_task
Browse files Browse the repository at this point in the history
Add rake tasks to create mock data in database.
  • Loading branch information
mattwr18 authored Mar 28, 2024
2 parents 194359c + 838ebfd commit f1d5f79
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
6 changes: 5 additions & 1 deletion app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Request < ApplicationRecord

acts_as_taggable_on :tags

after_create { Request.broadcast!(self) }
after_create :broadcast_request

after_update_commit :broadcast_updated_request

Expand Down Expand Up @@ -95,6 +95,10 @@ def self.attach_files(files)

private

def broadcast_request
Request.broadcast!(self)
end

def broadcast_updated_request
return unless saved_change_to_schedule_send_for?

Expand Down
67 changes: 67 additions & 0 deletions db/seeds/fill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require 'factory_bot_rails'
require 'faker'

contributors_count = 100
request_count = 200
message_count = request_count * (contributors_count * 0.5).to_i
message_time = Faker::Time.backward(days: 14)

users = User.all

# images = 10.times.map { URI(Faker::Avatar.image(size: '50x50', format: 'png', set: 'set5')) }

FactoryBot.modify do
factory :contributor do
data_processing_consent { true }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
note { Faker::Movies::HitchhikersGuideToTheGalaxy.quote }

# after(:build) do |contributor|
# image = images.sample
# contributor.avatar.attach(
# io: image.open,
# filename: File.basename(image.path)
# )
# end
end
end

FactoryBot.modify do
factory :request do
title { Faker::Lorem.question }
text { Faker::Lorem.paragraph }
user { users.sample }
end
end

Rails.logger.debug 'Seeding requests..'
requests = FactoryBot.build_list(:request, request_count) do |request|
request.class.skip_callback(:create, :after, :broadcast_request, raise: false)
request.save!
end

Rails.logger.debug 'Seeding contributors..'
contributors = FactoryBot.create_list(:contributor, contributors_count)

FactoryBot.modify do
factory :message do
created_at { message_time }
updated_at { message_time }
sender_type { 'Contributor' }
text { Faker::Lorem.paragraph }
unknown_content { false }
broadcasted { false }
sender { contributors.sample }
recipient { nil }
request { requests.sample }
end
end

Rails.logger.debug 'Seeding messages..'
FactoryBot.build_list(:message, message_count) do |message|
message.class.skip_callback(:create, :after, :send_if_outbound, raise: false)
message.save!
end
13 changes: 13 additions & 0 deletions lib/tasks/custom_seeds.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

namespace :db do
namespace :seed do
Dir[Rails.root.join('db/seeds/*.rb')].each do |filename|
task_name = File.basename(filename, '.rb')
desc "Seed based on the file in `db/seeds/#{task_name}.rb`"
task task_name.to_sym => :environment do
load(filename) if File.exist?(filename)
end
end
end
end
6 changes: 1 addition & 5 deletions spec/factories/contributors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
first_name { 'John' }
last_name { 'Doe' }
data_processing_consent { true }
sequence :email do |n|
"contributor#{n}@example.org"
end
email { Faker::Internet.email }

trait :with_an_avatar do
after(:build) do |contributor|
Expand All @@ -17,7 +15,5 @@
)
end
end

organization
end
end

0 comments on commit f1d5f79

Please sign in to comment.