-
Notifications
You must be signed in to change notification settings - Fork 2
/
seed_manager.rb
75 lines (62 loc) · 1.61 KB
/
seed_manager.rb
1
2
3
4
5
6
7
8
9
10
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
# frozen_string_literal: true
require 'factory_bot_rails'
require 'faker'
FactoryBot.definition_file_paths = ['spec/backend/factories']
FactoryBot.find_definitions
class SeedManager
def self.all
return if production?
log('Create About')
create(:about, 1)
log('Create Work Experiences')
create(:work_experience, 5)
log('Create Skills')
create(:skill, 20)
log('Create Educations')
create(:education, 2, :finished)
end
def self.clear_cache
log('Clearing cache')
Rails.cache.clear
end
def self.clean
return if production?
log('Cleaning tables')
tables = %i[about
education
skill
work_experience]
tables.each do |model|
ActiveRecord::Base.connection.execute(
"TRUNCATE #{model.to_s.tableize} RESTART IDENTITY CASCADE"
)
end
end
def self.create(model, quantity, trait = nil)
return if production?
I18n.available_locales.each do |locale|
FactoryBot.create_list(model, quantity, trait, locale: locale)
end
end
def self.user
return if production?
log('Create/Update default user')
User.where(
first_name: 'Gustavo',
last_name: 'Cunha',
email: 'email@example.com',
default_locale: :en
).first_or_create(password: 'password2018')
end
def self.log(message)
max_size = 79
messages = []
messages << ('=' * 2)
messages << message
messages << ('=' * (max_size - 2 - messages.map(&:length).inject(:+)))
Rails.logger.info messages.join(' ')
end
def self.production?
ENV['IS_PRODUCTION'].present?
end
end