Guest User

Untitled

a guest
Jan 23rd, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. require 'ffaker'
  2. include ActionDispatch::TestProcess
  3. include FileUtils
  4.  
  5. class Seeder
  6.  
  7. def seed
  8. puts "===== Adding Seed Data ====="
  9. housekeeping
  10. rebuild_database
  11. countries
  12. admin_users
  13. users
  14. topics
  15. posts
  16. end
  17.  
  18. #
  19. # Housekeeping
  20. #
  21.  
  22. def housekeeping
  23. puts "Removing old system (Paperclip) images"
  24. FileUtils.remove_dir('public/system') if File.exists?('public/system')
  25. end
  26.  
  27. #
  28. # Countries
  29. #
  30.  
  31. def countries
  32. puts "===== COUNTRIES ====="
  33. puts "Adding Countries"
  34.  
  35. File.open("fixtures/countries.txt", "r") do |file|
  36. while name = file.gets
  37. country = Country.create(:name => name)
  38. report country
  39. end
  40. end
  41.  
  42. count_records Country
  43. end
  44.  
  45. #
  46. # USERS
  47. #
  48.  
  49. def admin_users
  50. puts "===== ADMIN USERS ====="
  51.  
  52. spacemonkey = User.create( :email => "spacemonkey@example.com",
  53. :password => "123456",
  54. :password_confirmation => "123456",
  55. :avatar => fixture_file_upload("fixtures/avatars/spacemonkey.jpg", 'image/jpeg'),
  56. :phone => Faker::PhoneNumber.short_phone_number,
  57. :country => Country.random.first )
  58. report(spacemonkey)
  59.  
  60. spartacus = User.create( :email => "spartacus@creativeallies.com",
  61. :password => "spartacus",
  62. :password_confirmation => "spartacus",
  63. :avatar => fixture_file_upload("fixtures/avatars/spartacus.jpg", 'image/jpeg'),
  64. :phone => Faker::PhoneNumber.short_phone_number,
  65. :country => Country.random.first )
  66. report(spartacus)
  67.  
  68. count_records User
  69. end
  70.  
  71. def users
  72. puts "==== RANDOM USERS ===="
  73.  
  74. 50.times do
  75. begin
  76. user = User.create( :email => Faker::Internet.email,
  77. :password => '123456',
  78. :password_confirmation => '123456',
  79. :avatar => fixture_file_upload("fixtures/headshots/#{rand(19)+1}.jpg", 'image/jpeg'),
  80. :phone => Faker::PhoneNumber.short_phone_number,
  81. :country => Country.random.first )
  82. report(user)
  83. rescue => e
  84. puts "Something went wrong => #{e}"
  85. end
  86. end
  87.  
  88. count_records User
  89. end
  90.  
  91. #
  92. # FORUM
  93. #
  94.  
  95. def topics
  96. puts "===== TOPICS ====="
  97. puts "Adding 100 Topics"
  98. 50.times do
  99. topic = Topic.create(:title => Faker::Company.bs, :user => User.random.first, :sticky => randbool)
  100. report topic
  101. end
  102.  
  103. count_records Topic
  104. end
  105.  
  106. def posts
  107. puts "===== POSTS ====="
  108. puts "Adding 1000 Posts"
  109. 3000.times do
  110. post = Post.create(:body => Faker::Lorem.paragraphs.join("\n\n"), :user => User.random.first, :topic => Topic.random.first)
  111. report post
  112. end
  113.  
  114. count_records Post
  115. end
  116.  
  117. #
  118. # Utilities
  119. #
  120.  
  121. def mime_type filepath
  122. `file -Ib #{filepath}`.gsub(/\n/,"").split(';').first
  123. end
  124.  
  125. def truncate_table(table_name)
  126. ActiveRecord::Base.connection.execute("DELETE FROM #{table_name}")
  127. ActiveRecord::Base.connection.execute("VACUUM")
  128. puts "XXX Truncated Table #{table_name}"
  129. end
  130.  
  131. def rebuild_database
  132. puts "rewinding database"
  133. `rake db:migrate VERSION=0`
  134. puts "rebuilding database"
  135. `rake db:migrate`
  136. end
  137.  
  138. def count_records(element, title = false)
  139. puts "### COUNT #{title || element.to_s} -> #{element.count}"
  140. end
  141.  
  142. def report(element)
  143. case element.class.to_s
  144. when "User"
  145. puts "Added User #{element.email} from #{element.country.name}"
  146. when "Country"
  147. puts "Added Country #{element.name}"
  148. when "Topic"
  149. puts "Added Topic #{element.title}"
  150. when "Post"
  151. puts "Added Post to Topic #{element.topic.title} by #{element.user.email}"
  152. end
  153. end
  154.  
  155. def randbool
  156. rand(2) == 1 ? true : false
  157. end
  158.  
  159. #
  160. # Method Missing
  161. #
  162.  
  163. def self.method_missing(name, *args, &block)
  164. Seeder.new.send(name, *args, &block)
  165. end
  166.  
  167. end
Add Comment
Please, Sign In to add comment