-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeds.rb
50 lines (41 loc) · 1.14 KB
/
seeds.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
require 'faker'
user1 = User.create(email: '[email protected]' , password: 'password')
books_total = 50
book_titles_user_1 = books_total.times.map{|i| {
'title' => Faker::Book.title,
'author' => Faker::Book.author,
}}
def add_books_to_user(user, books, books_total)
books.each_with_index do |book, i|
end_date = Time.now - (Random.new.rand * 150).days
author = Author.find_or_create_by(
name: book['author']
)
new_book = user.books.create(
title: book['title'],
author_id: author['id'],
start_date: end_date - (Random.new.rand * 20 + 5).days
)
if i < books_total * 0.1
# currently reading
elsif i < books_total * 0.3
new_book.on_hold = end_date
elsif i < books_total * 0.6
new_book.start_date = nil
else
new_book.end_date = end_date
todo_action = 'review'
if i % 2 == 0
todo_action = 'prepare_notes'
end
todo = Todo.create(
user: user,
book: new_book,
action: todo_action,
due_date: end_date + 5.days
)
end
new_book.save
end
end
add_books_to_user(user1, book_titles_user_1, books_total)