require 'factory_girl' namespace :db do desc "Add a simple one track recording to the database" task single_recording: :environment do User.where(:musician => true).order('RANDOM()').limit(10).each do |uu| @user = uu next if @user.connections.present? @connection = FactoryGirl.create(:connection, :user => @user) @track = FactoryGirl.create(:track, :connection => @connection, :instrument => Instrument.find('violin'), :client_track_id => "t1") @music_session = FactoryGirl.create(:music_session, :creator => @user, :musician_access => true) @music_session.connections << @connection @music_session.save @recording = FactoryGirl.create(:recording, :music_session => @music_session, :owner => @user, :id=>"R#{rand(10000)}") @recorded_track = RecordedTrack.create_from_track(@track, @recording) @recorded_track.save #@recording = Recording.start(@music_session, @user) @recording.stop @recording.reload @genre = Genre.find('ambient') @recording.claim(@user, "name", "description", @genre, true, true) @recording.reload @claimed_recording = @recording.claimed_recordings.first end end task clean: :environment do DatabaseCleaner.strategy = :truncation, {:except => %w[instruments genres users] } DatabaseCleaner.clean_with(:truncation, {:except => %w[instruments genres users] }) DatabaseCleaner.start DatabaseCleaner.clean end task populate_friends: :environment do make_friends end task populate_bands: :environment do make_bands end task populate_band_members: :environment do make_band_members end task populate_band_genres: :environment do make_band_genres end desc "Fill database with music session sample data" task populate_music_sessions: :environment do make_users(10) if 14 > User.count make_bands if 0==Band.count make_music_sessions_history make_music_sessions_user_history end end def make_music_sessions_history users = User.all.map(&:id) bands = Band.all.map(&:id) genres = Genre.all.map(&:description) 50.times do |nn| obj = MusicSessionHistory.new obj.music_session_id = rand(100000000).to_s obj.description = Faker::Lorem.paragraph obj.user_id = users[rand(users.count)] obj.band_id = bands[rand(bands.count)] obj.created_at = Time.now - rand(1.month.seconds) obj.session_removed_at = obj.created_at + (rand(3)+1).hour obj.genres = genres.shuffle[0..rand(4)].join(' | ') obj.save! end end def make_music_sessions_user_history users = User.all.map(&:id) hists = MusicSessionHistory.all hists.each do |msh| (rand(9)+1).times do |nn| obj = MusicSessionUserHistory.new obj.music_session_id = msh.music_session_id obj.user_id = users[rand(users.count)] obj.created_at = msh.created_at obj.session_removed_at = obj.created_at + (rand(3)+1).hour obj.client_id = rand(100000000).to_s obj.save! end end end def make_band_members Band.find_each do |bb| User.order('RANDOM()').limit(4).each do |uu| BandMusician.create!({:user_id => uu.id, :band_id => bb.id}) end end end def make_band_genres Band.find_each do |bb| next if bb.genres.present? Genre.order('RANDOM()').limit(rand(3)+1).each do |gg| bb.genres << gg end end end def make_bands 10.times do |nn| name = Faker::Name.name website = Faker::Internet.url biography = Faker::Lorem.sentence city = 'Austin' # Faker::Address.city state = 'TX' # Faker::Address.state_abbr country = 'US' bb = Band.new( name: name, website: website, biography: biography, city: city, state: state, country: country, ) begin bb.save! rescue puts $!.to_s + ' ' + bb.errors.inspect end end end def make_users(num=99) admin = User.create!( first_name: Faker::Name.name, last_name: Faker::Name.name, email: "example@railstutorial.org", password: "foobar", password_confirmation: "foobar", terms_of_service: true) admin.toggle!(:admin) num.times do |n| email = "example-#{n+1}@railstutorial.org" password = "password" User.create!(first_name: Faker::Name.name, last_name: Faker::Name.name, terms_of_service: true, email: email, password: password, password_confirmation: password) end end def make_microposts users = User.all(limit: 6) 50.times do content = Faker::Lorem.sentence(5) users.each { |user| user.microposts.create!(content: content) } end end def make_relationships users = User.all user = users.first followed_users = users[2..50] followers = users[3..40] followed_users.each { |followed| user.followings << followed } followers.each { |follower| follower.follow!(user) } end def make_followings users = User.all users.each do |uu| users[0..rand(users.count)].shuffle.each do |uuu| uuu.followings << uu unless 0 < UserFollowing.where(:user_id => uu.id, :follower_id => uuu.id).count uu.followings << uuu unless 0 < UserFollowing.where(:user_id => uuu.id, :follower_id => uu.id).count if rand(3)==0 end end end def make_friends users = User.all users[6..-1].each do |uu| users[0..5].shuffle.each do |uuu| Friendship.save(uu.id, uuu.id) end end end