Created
August 15, 2012 15:34
-
-
Save allspiritseve/3361056 to your computer and use it in GitHub Desktop.
Rails rake task to install sample data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/tasks/db/sample.rake | |
namespace :db do | |
desc "Clear out the database and replace it with sample data" | |
task :sample => ['db:ensure_development_or_staging','environment','db:drop','db:setup'] do | |
sample_file = 'db/samples.rb' | |
load(sample_file) if File.exists?(sample_file) | |
end | |
task :ensure_development_or_staging do | |
if !['development','staging'].include? Rails.env | |
puts "This task can only run in a development or staging environment" | |
exit | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/samples.rb | |
artist_names = ['Incubus','Evanescence','Third Eye Blind','Shakira','Maroon 5','Christina Perri','Brand New','A Perfect Circle','Tool','Muse','Dispatch','Finger Eleven','Flyleaf','Kings of Leon','Modest Mouse','Regina Spektor'] | |
venue_names = ['The Blind Pig','The Ark','The Fillmore','Magic Hat','Crazy Wisdom Team Room','Michigan Theater','Potbelly\'s on State St.'] | |
location_names = ['Ann Arbor, MI','Ypsilanti, MI','Brighton, MI','Dexter, MI','Milan, MI','Detroit, MI'] | |
# Generate sample artists | |
artists_count = 0 | |
artist_names.each do |artist_name| | |
Artist.create! do |a| | |
a.name = artist_name | |
a.location = location_names.sample | |
a.local = [true,false].sample | |
a.website = "http://#{artist_name.parameterize}.local" | |
a.twitter = artist_name.parameterize | |
a.youtube = artist_name.gsub(/\s/,'') | |
end | |
artists_count += 1 | |
end | |
puts "Generated #{artists_count} sample artists" | |
# Generate sample venues | |
venues_count = 0 | |
venue_names.each do |venue_name| | |
Venue.create! do |v| | |
v.name = venue_name | |
v.website = "http://#{venue_name.parameterize}.local" | |
v.address = "123 Main St, #{location_names.sample}" | |
end | |
venues_count += 1 | |
end | |
puts "Generated #{venues_count} sample venues" | |
# Generate sample events | |
events_count = 0 | |
venues = Venue.all | |
artists = Artist.all | |
(1..60).each do |n| | |
(1..3).to_a.sample.times do | |
starts_at_date = Date.today + n.days | |
starts_at_time = (5..11).to_a.sample | |
performers = artists.sample(1 + rand(3)) | |
Event.create! do |e| | |
e.venue = venues.sample | |
e.performances.new(:artist_id => performers.first.id) | |
(performers - [performers.first]).each do |performer| | |
e.performances.new(:artist_id => performer.id, :type => Performance::TYPES.sample) | |
end | |
e.starts_at_date = starts_at_date | |
e.doors_open_at_time = "#{starts_at_time - 1} pm" if [true,false].sample | |
e.starts_at_time = "#{starts_at_time} pm" | |
e.ends_at_time = "#{starts_at_time + (1..3).to_a.sample % 12} pm" if [true,false].sample | |
e.reviewed_at = Time.now | |
e.age_restriction = Event::AGE_RESTRICTIONS.sample | |
end | |
events_count += 1 | |
end | |
end | |
puts "Generated #{events_count} sample events" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment