Skip to content

Commit

Permalink
Merge pull request #239 from TreinaDev/refactor/super-seed
Browse files Browse the repository at this point in the history
Refactor do Superseed

Co-authored-by: Gabriel Manika <[email protected]>
  • Loading branch information
GyodaiDDA and gmkoeb authored Feb 16, 2024
2 parents 83e26c4 + e8a303d commit 0f589df
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ O Portfoliorrr é uma rede social com funcionalidades de portfólio para pessoas
- Rode o comando `bin/setup` e aguarde sua conclusão;
- Rode o comando `yarn install` (necessário ter `node` instalado em sua máquina);

## Populando o banco de dados

- O seed comum, para desenvolvimento, pode ser feito normalmente com `rails db:seed`
- Para apresentações e testes de front-end, é possível fazer um superseed, utilizando `rails db:seed:superseed`
- O superseed utiliza as gems Faker e FactoryBot, que são instaladas durante a configuração (ver item anterior)

## Como visualizar a aplicação no navegador

- Siga as instruções de configuração da aplicação
Expand Down
24 changes: 18 additions & 6 deletions db/seeds/superseed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@
]

# Adiciona usuários, perfis, informações pessoais
print "\n4. criando usuários "
@number_of_users.times do
user = FactoryBot.create(:user, :seed)
profile = FactoryBot.create(:profile, :seed, user:)
profile.photo.attach(Rails.root.join('app', 'assets', 'images', 'avatars', "avatar#{user.id}.png"))
personal_info = FactoryBot.create(:personal_info, :seed, profile:)

# Adiciona experiências profissionais
FactoryBot.create(:professional_info, :first_seed, profile:)
FactoryBot.create(:professional_info, :first_job, profile:)
rand(2..7).times do
FactoryBot.create(:professional_info, :seed, profile:)
FactoryBot.create(:professional_info, :seed_job, profile:)
end
FactoryBot.create(:professional_info, :current_job, profile:)

# Adiciona experiências acadêmicas
FactoryBot.create(:education_info, :first_seed, profile:)
Expand All @@ -85,25 +87,33 @@
job_category: job_categories.sample,
description: Faker::Lorem.paragraph)
end
print '.'
end

# Cria um post com imagem e outros posts somente texto (WIP)
# Para cada user cria um post com imagem e de um a três sem imagens
print "\n3. criando postagens "
User.all.each do |user|
html_post = %(<action-text-attachment sgid="#{images_for_posts.sample.attachable_sgid}"></action-text-attachment>)
user.posts.create(title: Faker::Lorem.sentence, content: "#{Faker::Lorem.paragraph} #{html_post}", tag_list: [tags].sample)
FactoryBot.create(:post, :seed, user:, content: "#{Faker::Lorem.paragraphs(number: 3).join(' ')} #{html_post}")
rand(1..3).times do
user.posts.create(title: Faker::Lorem.sentence, content: "#{Faker::Lorem.paragraph}", tag_list: [tags].sample)
FactoryBot.create(:post, :seed, user:)
end
print '.'
end

# Adiciona followers aos perfis
print "\n2. estabelecendo seguidores e seguidos "
User.all.each do |user|
rand(2..5).times do
not_followed_profiles = Profile.all.reject { |profile| profile.following?(user.profile) }
followed_profile = not_followed_profiles.sample if not_followed_profiles.any?
Connection.create!(follower: user.profile, followed_profile:) unless followed_profile == user.profile
end
print '.'
end

# Adiciona comentários e likes
print "\n1. criando comentários e dando likes "
Post.all.each do |post|
rand(0..10).times do
FactoryBot.create(:like, likeable: post, user: User.all.reject { |user| post.likes.pluck(:user_id).include?(user.id) }.sample)
Expand All @@ -114,7 +124,9 @@
FactoryBot.create(:like, likeable: comment, user: User.all.reject { |user| comment.likes.pluck(:user_id).include?(user.id) }.sample)
end
end
print '.'
end

puts "Pronto! #{@number_of_users} usuários criados."
puts "\nPronto! #{@number_of_users} usuários criados."
puts "Admin: #{admin.email}, senha: #{admin.password}"
puts "\n"
25 changes: 24 additions & 1 deletion spec/factories/posts.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
FactoryBot.define do
def long_post
'very big sentence'
end

factory :post do
user
title { Faker::Lorem.sentence }
content { Faker::Lorem.paragraph sentence_count: rand(35..50) }
content { Faker::Lorem.paragraph sentence_count: rand(2..12) }
edited_at { Time.zone.now }

trait :published do
published_at { Time.zone.now }
status { :published }
end

trait :seed do
content do
[
Faker::Lorem.paragraph(sentence_count: rand(4..18)),
Faker::Lorem.paragraph(sentence_count: rand(2..18)),
Faker::Lorem.paragraph(sentence_count: rand(4..18)),
Faker::Lorem.paragraph(sentence_count: rand(4..18)),
Faker::Lorem.paragraph(sentence_count: rand(4..18))
].join('<br><br>')
end
tag_list do
[
%w[tdd rubocop], %w[seeds desafios], %w[boaspraticas solid], %w[vue zoom], %w[vue desafios],
%w[codesaga desafios tdd], %w[rubocop vue seeds], %w[zoom boaspraticas solid],
%w[tdd codesaga], %w[rubocop vue desafios], %w[seeds boaspraticas zoom], %w[solid codesaga]
].sample
end
end
end
end
16 changes: 14 additions & 2 deletions spec/factories/professional_infos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,34 @@
position { %w[programador estagiário gerente].sample }
end

trait :first_seed do
trait :first_job do
company { Faker::Company.name }
position { Faker::Job.position }
description { Faker::Lorem.sentences(number: 6) }
profile
start_date { Faker::Date.backward(days: Time.zone.today - profile.personal_info.birth_date + 5840) }
end_date { Faker::Date.between(from: start_date, to: start_date.advance(months: rand(2..120))) }
end

trait :seed do
trait :seed_job do
company { Faker::Company.name }
position { Faker::Job.position }
description { Faker::Lorem.sentences(number: 6) }
start_date do
Faker::Date.between(from: profile.professional_infos.last.end_date,
to: profile.professional_infos.last.end_date.advance(months: rand(2..12)))
end
end_date { Faker::Date.between(from: start_date, to: start_date.advance(months: rand(2..120))) }
end

trait :current_job do
company { Faker::Company.name }
position { Faker::Job.position }
description { Faker::Lorem.sentences(number: 6) }
profile
start_date { Faker::Date.backward(days: Time.zone.today - profile.personal_info.birth_date + 5840) }
end_date {}
current_job { true }
end
end
end

0 comments on commit 0f589df

Please sign in to comment.