Guest User

Untitled

a guest
Mar 7th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. require 'highline/import'
  2.  
  3. namespace :db do
  4. desc 'Populates DB in interactive mode with sample data'
  5. task :populate => :environment do
  6.  
  7. puts
  8.  
  9. if agree("Do you want me to create admin user? ")
  10. Factory :admin_user,
  11. :login => 'admin',
  12. :name => 'Administrator',
  13. :password => 'adminus',
  14. :password_confirmation => 'adminus'
  15.  
  16. say "Administrative user crated! Login - <%= color('admin', BOLD) %>, password - <%= color('adminus', BOLD) %>"
  17.  
  18. end
  19.  
  20. puts
  21.  
  22. count = ActiveSupport::OrderedHash.new([[:user,50], [:artist,60], [:song,200], [:comment,100]])
  23. count.each do |model,default|
  24. count[model] = ask("How many <%= color('#{model.to_s.pluralize}', BOLD) %> do you want?\t", Integer) {|q| q.default = default }
  25. end
  26.  
  27. puts
  28.  
  29. count.each do |model,quantity|
  30. print 'Creating ' + model.to_s.pluralize
  31. quantity.times do
  32. print '.'
  33.  
  34. case model
  35. # simple factory
  36. when :user, :artist
  37. Factory model
  38.  
  39. # not so simple factory
  40. when :song
  41. Factory :song,
  42. :artist => Artist.find(:first, :offset => rand(Artist.count :all)),
  43. :user => (rand(2)) ? User.find(:first, :offset => rand(User.count :all)) : nil # I love ruby ninja style!!!
  44.  
  45. # ouch, that's one complicated!
  46. when :comment
  47. if rand(2)
  48. # user's comment
  49. Factory :comment,
  50. :song => Song.find(:first, :offset => rand(Song.count :all)),
  51. :user => User.find(:first, :offset => rand(User.count :all))
  52. else
  53. # anonymous comment
  54. Factory :anonymous_comment,
  55. :song => Song.find(:first, :offset => rand(Song.count :all))
  56. end
  57.  
  58. end # case
  59. end # count[model].times
  60. puts 'Done!'
  61.  
  62. end # count.each
  63.  
  64. end # End db:populate task
  65.  
  66. desc "Migrates to version 0 and than migrates back up to latest migration"
  67. task :reset_schema => :environment do
  68. ActiveRecord::Migrator.migrate("db/migrate/", 0)
  69. ActiveRecord::Migrator.migrate("db/migrate/")
  70. end
  71.  
  72. desc "Drops all table and recreates it using migration. Then fills it with sample data."
  73. task :bootstrap => :environment do
  74. Rake::Task["db:reset_schema"].invoke
  75. Rake::Task["db:populate"].invoke
  76. end
  77.  
  78.  
  79. end
Add Comment
Please, Sign In to add comment