-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1781 from tactilenews/add_data_generation_task
Add rake tasks to create mock data in database.
- Loading branch information
Showing
4 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters