Advertisement
Secretprtay

seed.rb

Jan 30th, 2022
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.52 KB | None | 0 0
  1. return unless Rails.env == "development"
  2.  
  3. system 'clear'
  4.  
  5. puts 'Destroy all records'
  6. puts '*' * 80
  7.  
  8. User.destroy_all
  9. Product.destroy_all
  10. Category.destroy_all
  11.  
  12. puts 'Create new records'
  13. puts '*' * 80
  14.  
  15. MAX_USERS_COUNT    = 3
  16. MAX_CATEGORY_COUNT = Category::types.count
  17. MAX_PRODUCT_COUNT  = 68
  18. USER_EMAIL         = %w(jane.doe@example.com john.doe@example.com admin@example.com)
  19. # service
  20. def create_user(email, password = '123456')
  21.   User.create!(email: email, password: password)
  22.   print '.'
  23. end
  24. #create Users
  25. USER_EMAIL.each { |email| create_user(email) }
  26.  
  27. #create Category
  28. Category::types.keys.each do |name|
  29.   Category.create!(name: name)
  30.   print '.'
  31. end
  32.  
  33. #create Products
  34. MAX_PRODUCT_COUNT.times do
  35.   # create
  36.   Product.create!(name: Faker::Commerce.product_name,
  37.                   description: Faker::TvShows::BigBangTheory.quote,
  38.                   price: Faker::Commerce.price(range: 0..1000.00),
  39.                   brand: Product::brands.keys.sample,
  40.                   product_type:  Product::types.keys.sample,
  41.                   color: Product::colors.values.sample,
  42.                   size:  Product::sizes.values.sample,
  43.                   sex:   Product::sexes.values.sample,
  44.                   category: Category.all.sample)
  45.   print '.'
  46. end
  47.  
  48. puts ' '
  49. puts ' '
  50. puts "That's all folks!"
  51. puts '*' * 80
  52. p "Created #{Category.count} #{'category'.pluralize(Category.count)}"
  53. p "Created #{Product.count} #{'product'.pluralize(Product.count)}"
  54. p "Created #{User.count} #{'user'.pluralize(User.count)}"
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement