Skip to content

Instantly share code, notes, and snippets.

@andresgutgon
Created September 26, 2010 19:56
Show Gist options
  • Save andresgutgon/598270 to your computer and use it in GitHub Desktop.
Save andresgutgon/598270 to your computer and use it in GitHub Desktop.
# Esto es un pequeño script que tengo para rellena mi BBDD de desarrollo:
# -----------------------------------------------------------------------
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
jobs_categories = %w[undergraduate predoctoral postdoctoral associate_prof assistent_prof research_prof technician]
address = Address.create!(
:city => "Barcelona",
:street => "Gayarre, 88",
:postcode => "08014",
:university => "Universidad de Barcelona",
:faculty => "Facultad de Medicina",
:department => "Central",
:campus => "Nord",
:name => "Medicina UB",
:telephone => "93 333 33 33",
:fax => "93 333 33 33",
:region => "Catalunya"
)
if Address.find(:first)
puts "--- Address created ---"
end
avatar_samples = %w[yo.jpg 1.png 2.png 3.png 4.png 5.png]
av_path = "#{RAILS_ROOT}/public/system/fake_avatars/yo.jpg"
avatar = File.open(av_path, 'r')
user = User.new(:email => "andres@gmail.com",
:password => "password",
:password_confirmation => "password"
)
# Con esto le digo al modelo user que NO tiene que buscar una Invitation asociada.
user.autopopulate = true
user.roles = User::ROLES #Le hago admin por ser el primero que creo
user.build_profile(
:name => "Andrés",
:family_name => "Gutiérrez",
:second_family_name => "González",
:job_category => jobs_categories.rand,
:address => address,
:contact_email => user.email,
:phone => Faker.numerify('93 ### ## ##'),
:fax => Faker.numerify('93 ### ## ##'),
:floor => rand(5) + 1,
:room => rand(60) + 1,
:research_interest => Faker::Lorem.sentence(255),
:avatar => avatar
)
user.save
if User.find(:first)
puts "--- User/Profile created ---"
end
systemSettings = SystemSetting.create!(
:user_id => user.id,
:address_id => address.id
)
if SystemSetting.find(:first)
puts "--- SystemSetting created ---"
end
num_users_created = 40
num_users_created.times do |n|
av_path = "#{RAILS_ROOT}/public/system/fake_avatars/#{avatar_samples.rand}"
avatar = File.open(av_path, 'r')
user = User.new(:email => "example-#{n+1}@domain.cat",
:password => "password",
:password_confirmation => "password"
)
# Con esto le digo al modelo user que no tiene que buscar una Invitation asociada.
user.autopopulate = true
user.roles = User::ROLES[1].to_a
user.build_profile(
:name => Faker::Name.first_name,
:family_name => Faker::Name.last_name,
:second_family_name => Faker::Name.last_name,
:job_category => jobs_categories.rand,
:address => address,
:contact_email => user.email,
:phone => Faker.numerify('93 ### ## ##'),
:fax => Faker.numerify('93 ### ## ##'),
:floor => rand(5) + 1,
:room => rand(60) + 1,
:research_interest => Faker::Lorem.sentence(255),
:avatar => avatar
)
user.save
end
if User.find(:all).count > 39
puts "--- #{num_users_created} users created ---"
end
num_publications = 50
User.all(:limit => 10).each do |user|
num_publications.times do
pub_types = %w[journal book article_or_chapter_book]
pub_type = pub_types.rand
status = %w[forthcoming submitted under_review]
attributes ={
:published => [true,false].rand,
:pub_type => pub_type,
:title => Faker::Lorem.sentence((4..14).to_a.rand),
:abstract => Faker::Lorem.sentence(255)
}
pub = Publication.new(attributes)
published_attributes = {}
if pub.published
if pub.have_pages?
published_attributes.merge!(
:pub_pages => [Faker.numerify('##-##'),Faker.numerify('##')].rand,
:pub_volume => [Faker.numerify('###'),""].rand,
:pub_issue => [Faker.numerify('###'),""].rand
)
end
if pub.have_media_title?
published_attributes.merge!(:media_title => Faker::Lorem.sentence((4..14).to_a.rand))
end
if pub.have_publisher?
published_attributes.merge!(:publisher => Faker::Lorem.sentence((4..6).to_a.rand))
end
if pub.have_editors?
editors = []
(0..4).to_a.rand.times do
editors<<"#{Faker::Name.last_name}, #{Faker::Name.first_name[0..0]}."
end
editors_txt = editors.join("&")
published_attributes.merge!(:editors => editors_txt)
end
else # pub.published
published_attributes = {
:status => status.rand
}
end
attributes.merge!(published_attributes)
pub = user.publications.create!(attributes)
# Asocio entre 1 y 6 perfiles a cada publicación
# associated_profiles = []
pub.profile_positions.build(:profile_id => user.profile.id, :position => 1)
(2..6).to_a.rand.times { |position|
profile_id = (1..num_users_created-10).to_a.rand
profile_id = profile_id == user.profile.id ? user.profile.id + 1 : profile_id
pub.profile_positions.build(:profile_id => profile_id, :position => position + 2)
# associated_profiles<<Profile.find(profile_id)
# pub.profiles<<Profile.find(profile_id)
}
pub.save
end
end
if Publication.find(:all).count > num_publications-1
puts "--- #{num_publications} publications created ---"
end
end # task :populate
end # namespace :db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment