Skip to content

Commit 804c7d8

Browse files
Merge pull request #205 from TreinaDev/feat/super-seed
Seed com 40 usuários e diversos objetos para teste amplo do Portfoliorrr Co-authored-by: Eliseu Ramos <[email protected]>
2 parents f3c0c80 + bc2e3de commit 804c7d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+8506
-0
lines changed

app/assets/images/avatars/avatar1.png

44.4 KB
34.3 KB
44 KB
34.2 KB
30.9 KB
24.5 KB
30.3 KB
31 KB
28.8 KB
34.6 KB
31.1 KB

app/assets/images/avatars/avatar2.png

39.3 KB
33 KB
26.2 KB
26.7 KB
37 KB
29.1 KB
30.6 KB
24.9 KB
36 KB
31.9 KB
21.4 KB

app/assets/images/avatars/avatar3.png

38.7 KB
29.7 KB
23 KB
36.8 KB
35.5 KB
43.8 KB
46.6 KB
37.4 KB
28.5 KB
26.8 KB
26 KB

app/assets/images/avatars/avatar4.png

33.4 KB
30.4 KB
37.2 KB
33.9 KB
38.1 KB
43.8 KB
32.3 KB
23.7 KB
33.3 KB
38.7 KB
24.7 KB

app/assets/images/avatars/avatar5.png

35.3 KB
38.6 KB
29.3 KB
20.7 KB
43.5 KB
28 KB
36 KB
40.4 KB
47.1 KB
34.1 KB
32.8 KB

app/assets/images/avatars/avatar6.png

39.7 KB
26.3 KB
42.5 KB
37 KB
35 KB
36.2 KB
33.1 KB
38.5 KB
31.1 KB
35.3 KB
33.5 KB

app/assets/images/avatars/avatar7.png

26.5 KB
33.3 KB

app/assets/images/avatars/avatar8.png

35.4 KB

app/assets/images/avatars/avatar9.png

31.6 KB

config/locales/pt-BR.faker.yml

+8,306
Large diffs are not rendered by default.

db/seeds/superseed.rb

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
require 'factory_bot_rails'
2+
require 'faker'
3+
Faker::Config.locale = :'pt-BR'
4+
Faker::UniqueGenerator.clear
5+
6+
# Cria admin
7+
admin = FactoryBot.create(:user, full_name: 'Boninho da Globo', email: '[email protected]', password: '654321', role: 'admin')
8+
9+
# Cria 40 usuários e pra cada um cria:
10+
# rand(2..7) experiências de trabalho
11+
# rand(2..7) experiências acadêmicas
12+
# rand(2..7) categorias de trabalho com descrição
13+
# 1 post com imagem
14+
# rand(1..3) posts somente texto
15+
# rand(2..5) seguidores
16+
# rand(0..10) comentários por posts
17+
# rand(0..10) likes em posts
18+
# rand(0..10) likes em comentários
19+
20+
21+
# Setup
22+
@number_of_users = 40
23+
24+
25+
# 30 categorias de Trabalho
26+
[
27+
'Trabalho em equipe', 'Comunicação', 'Resolução de problemas', 'Liderança', 'Organização',
28+
'Trabalhar sobre pressão', 'Confiança', 'Auto motivado', 'Habilidades de networking', 'Proatividade',
29+
'Aprendizado rápido', 'Experiência com tecnologia', 'Desenvolvimento Web', 'Programação em Ruby',
30+
'HTML', 'CSS', 'JavaScript', 'Resolução de Problemas', 'Comunicação Efetiva', 'Colaboração em Equipe',
31+
'Resiliência', 'Gestão do Tempo', 'Gestão de Projetos', 'Experiência do Usuário (UX)', 'SEO',
32+
'Análise de Dados', 'Redes Sociais', 'Marketing Digital', 'Liderança', 'Trabalho Remoto', 'Ruby on Rails'
33+
].each { |name| JobCategory.create(name:) }
34+
35+
# 12 combinações de tags para posts
36+
tags = [
37+
['tdd', 'rubocop'], ['seeds', 'desafios'], ['boaspraticas', 'solid'], ['vue', 'zoom'], ["vue", "desafios"],
38+
['codesaga', 'desafios', 'tdd'], ['rubocop', 'vue', 'seeds'], ['zoom', 'boaspraticas', 'solid'],
39+
["tdd", "codesaga"], ["rubocop", "vue", "desafios"], ["seeds", "boaspraticas", "zoom"], ["solid", "codesaga"]
40+
]
41+
42+
# 3 imagens para posts WIP
43+
images_for_posts = [
44+
ActiveStorage::Blob.create_and_upload!(
45+
io: File.open(Rails.root.join('app', 'assets', 'images', 'seeds',
46+
'turma_11.jpeg')),
47+
filename: 'turma11.jpeg'
48+
),
49+
ActiveStorage::Blob.create_and_upload!(
50+
io: File.open(Rails.root.join('app', 'assets', 'images', 'seeds',
51+
'git_github.jpg')),
52+
filename: 'git_github.jpg'
53+
),ActiveStorage::Blob.create_and_upload!(
54+
io: File.open(Rails.root.join('app', 'assets', 'images', 'seeds',
55+
'vue_js.jpg')),
56+
filename: 'vue_js.jpg'
57+
)
58+
]
59+
60+
# Adiciona usuários, perfis, informações pessoais
61+
@number_of_users.times do
62+
user = FactoryBot.create(:user, :seed)
63+
profile = FactoryBot.create(:profile, :seed, user:)
64+
profile.photo.attach(Rails.root.join('app', 'assets', 'images', 'avatars', "avatar#{user.id}.png"))
65+
personal_info = FactoryBot.create(:personal_info, :seed, profile:)
66+
67+
# Adiciona experiências profissionais
68+
FactoryBot.create(:professional_info, :first_seed, profile:)
69+
rand(2..7).times do
70+
FactoryBot.create(:professional_info, :seed, profile:)
71+
end
72+
73+
# Adiciona experiências acadêmicas
74+
FactoryBot.create(:education_info, :first_seed, profile:)
75+
rand(2..7).times do
76+
FactoryBot.create(:education_info, :seed, profile:)
77+
end
78+
79+
# Cadastra e escreve descrição de categorias de trabalho
80+
rand(2..7).times do
81+
repeated_job_category = profile.profile_job_categories.pluck(:job_category_id)
82+
job_categories = JobCategory.all.reject { |job_category| repeated_job_category.include?(job_category.id) }
83+
FactoryBot.create(:profile_job_category,
84+
profile:,
85+
job_category: job_categories.sample,
86+
description: Faker::Lorem.paragraph)
87+
end
88+
89+
# Cria um post com imagem e outros posts somente texto (WIP)
90+
html_post = %(<action-text-attachment sgid="#{images_for_posts.sample.attachable_sgid}"></action-text-attachment>)
91+
user.posts.create(title: Faker::Lorem.sentence, content: "#{Faker::Lorem.paragraph} #{html_post}", tag_list: [tags].sample)
92+
rand(1..3).times do
93+
user.posts.create(title: Faker::Lorem.sentence, content: "#{Faker::Lorem.paragraph}", tag_list: [tags].sample)
94+
end
95+
end
96+
97+
# Adiciona followers aos perfis
98+
User.all.each do |user|
99+
rand(2..5).times do
100+
not_followed_profiles = Profile.all.reject { |profile| profile.following?(user.profile) }
101+
followed_profile = not_followed_profiles.sample if not_followed_profiles.any?
102+
Connection.create!(follower: user.profile, followed_profile:) unless followed_profile == user.profile
103+
end
104+
end
105+
106+
# Adiciona comentários e likes
107+
Post.all.each do |post|
108+
rand(0..10).times do
109+
FactoryBot.create(:like, likeable: post, user: User.all.reject { |user| post.likes.pluck(:user_id).include?(user.id) }.sample)
110+
end
111+
rand(0..10).times do
112+
comment = FactoryBot.create(:comment, :seed, post:, user: User.all.sample)
113+
rand(0..10).times do
114+
FactoryBot.create(:like, likeable: comment, user: User.all.reject { |user| comment.likes.pluck(:user_id).include?(user.id) }.sample)
115+
end
116+
end
117+
end
118+
119+
puts "Pronto! #{@number_of_users} usuários criados."
120+
puts "Admin: #{admin.email}, senha: #{admin.password}"

lib/tasks/custom_seed.rake

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace :db do
2+
namespace :seed do
3+
Dir[Rails.root.join('db/seeds/*.rb')].each do |filename|
4+
task_name = File.basename(filename, '.rb')
5+
desc "Seed #{task_name}, based on the file with the same name in `db/seeds/*.rb`"
6+
task task_name.to_sym => :environment do
7+
load(filename) if File.exist?(filename)
8+
end
9+
end
10+
end
11+
end

spec/factories/comments.rb

+4
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
message { 'Um comentário legal' }
44
post
55
user
6+
7+
trait :seed do
8+
message { Faker::Lorem.paragraph }
9+
end
610
end
711
end

spec/factories/education_infos.rb

+17
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,22 @@
1111
institution { "Universidade #{Faker::Space.planet}" }
1212
course { "Tecnólogo em #{Faker::Sport.sport}" }
1313
end
14+
15+
trait :first_seed do
16+
institution { "Universidade #{Faker::Address.city} #{Faker::Space.planet}" }
17+
course { "#{Faker::Job.education_level} em #{Faker::Job.course}" }
18+
start_date { Faker::Date.backward(days: Time.zone.today - profile.personal_info.birth_date + 7000) }
19+
end_date { Faker::Date.between(from: start_date, to: start_date.advance(months: rand(2..120))) }
20+
end
21+
22+
trait :seed do
23+
institution { "Universidade #{Faker::Address.city} #{Faker::Space.planet}" }
24+
course { "#{Faker::Job.education_level} em #{Faker::Job.course}" }
25+
start_date do
26+
Faker::Date.between(from: profile.education_infos.last.end_date,
27+
to: profile.education_infos.last.end_date.advance(months: rand(2..12)))
28+
end
29+
end_date { Faker::Date.between(from: start_date, to: start_date.advance(months: rand(2..120))) }
30+
end
1431
end
1532
end

spec/factories/job_categories.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
FactoryBot.define do
22
factory :job_category do
33
sequence(:name) { |n| "Web Design#{n}" }
4+
5+
trait :seed do
6+
name { Faker::Job.unique.key_skills }
7+
end
48
end
59
end

spec/factories/personal_infos.rb

+13
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,18 @@
1010
birth_date { '1980-12-25' }
1111
visibility { true }
1212
profile
13+
14+
trait :seed do
15+
street { Faker::Address.street_name }
16+
street_number { Faker::Address.building_number }
17+
area { "#{%w[Vila Jardim].sample} #{Faker::Address.city}" }
18+
city { Faker::Address.city }
19+
zip_code { Faker::Address.zip_code }
20+
state { Faker::Address.state_abbr }
21+
phone { "#{Faker::PhoneNumber.subscriber_number}#{Faker::PhoneNumber.subscriber_number}" }
22+
birth_date { Faker::Date.birthday(min_age: 18, max_age: 88) }
23+
visibility { true }
24+
profile
25+
end
1326
end
1427
end

spec/factories/professional_infos.rb

+18
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,23 @@
1111
company { Faker::TvShows::RuPaul.queen }
1212
position { %w[programador estagiário gerente].sample }
1313
end
14+
15+
trait :first_seed do
16+
company { Faker::Company.name }
17+
position { Faker::Job.position }
18+
profile
19+
start_date { Faker::Date.backward(days: Time.zone.today - profile.personal_info.birth_date + 5840) }
20+
end_date { Faker::Date.between(from: start_date, to: start_date.advance(months: rand(2..120))) }
21+
end
22+
23+
trait :seed do
24+
company { Faker::Company.name }
25+
position { Faker::Job.position }
26+
start_date do
27+
Faker::Date.between(from: profile.professional_infos.last.end_date,
28+
to: profile.professional_infos.last.end_date.advance(months: rand(2..12)))
29+
end
30+
end_date { Faker::Date.between(from: start_date, to: start_date.advance(months: rand(2..120))) }
31+
end
1432
end
1533
end

spec/factories/profiles.rb

+7
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@
33
user
44
cover_letter { 'Sou profissional organizado, esforçado e apaixonado pelo que faço' }
55
end
6+
7+
trait :seed do
8+
cover_letter { Faker::Marketing.buzzwords }
9+
after(:build) do |profile|
10+
profile.class.skip_callback(:create, :after, :create_personal_info!, raise: false)
11+
end
12+
end
613
end

spec/factories/users.rb

+6
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
citizen_id_number { Faker::IDNumber.brazilian_citizen_number }
55
email { Faker::Internet.email }
66
password { '123456' }
7+
8+
trait :seed do
9+
after(:build) do |user|
10+
user.class.skip_callback(:create, :after, :create_profile!, raise: false)
11+
end
12+
end
713
end
814
end

0 commit comments

Comments
 (0)