Guest User

Untitled

a guest
Jun 15th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. # Esto es un pequeño script que tengo para rellena mi BBDD de desarrollo:
  2. # -----------------------------------------------------------------------
  3. require 'faker'
  4.  
  5. namespace :db do
  6. desc "Fill database with sample data"
  7. task :populate => :environment do
  8. Rake::Task['db:reset'].invoke
  9. jobs_categories = %w[undergraduate predoctoral postdoctoral associate_prof assistent_prof research_prof technician]
  10. address = Address.create!(
  11. :city => "Barcelona",
  12. :street => "Gayarre, 88",
  13. :postcode => "08014",
  14. :university => "Universidad de Barcelona",
  15. :faculty => "Facultad de Medicina",
  16. :department => "Central",
  17. :campus => "Nord",
  18. :name => "Medicina UB",
  19. :telephone => "93 333 33 33",
  20. :fax => "93 333 33 33",
  21. :region => "Catalunya"
  22. )
  23. if Address.find(:first)
  24. puts "--- Address created ---"
  25. end
  26.  
  27. avatar_samples = %w[yo.jpg 1.png 2.png 3.png 4.png 5.png]
  28. av_path = "#{RAILS_ROOT}/public/system/fake_avatars/yo.jpg"
  29. avatar = File.open(av_path, 'r')
  30. user = User.new(:email => "andres@gmail.com",
  31. :password => "password",
  32. :password_confirmation => "password"
  33. )
  34. # Con esto le digo al modelo user que NO tiene que buscar una Invitation asociada.
  35. user.autopopulate = true
  36. user.roles = User::ROLES #Le hago admin por ser el primero que creo
  37.  
  38. user.build_profile(
  39. :name => "Andrés",
  40. :family_name => "Gutiérrez",
  41. :second_family_name => "González",
  42. :job_category => jobs_categories.rand,
  43. :address => address,
  44. :contact_email => user.email,
  45. :phone => Faker.numerify('93 ### ## ##'),
  46. :fax => Faker.numerify('93 ### ## ##'),
  47. :floor => rand(5) + 1,
  48. :room => rand(60) + 1,
  49. :research_interest => Faker::Lorem.sentence(255),
  50. :avatar => avatar
  51. )
  52. user.save
  53.  
  54. if User.find(:first)
  55. puts "--- User/Profile created ---"
  56. end
  57.  
  58. systemSettings = SystemSetting.create!(
  59. :user_id => user.id,
  60. :address_id => address.id
  61. )
  62. if SystemSetting.find(:first)
  63. puts "--- SystemSetting created ---"
  64. end
  65. num_users_created = 40
  66. num_users_created.times do |n|
  67. av_path = "#{RAILS_ROOT}/public/system/fake_avatars/#{avatar_samples.rand}"
  68. avatar = File.open(av_path, 'r')
  69. user = User.new(:email => "example-#{n+1}@domain.cat",
  70. :password => "password",
  71. :password_confirmation => "password"
  72. )
  73. # Con esto le digo al modelo user que no tiene que buscar una Invitation asociada.
  74. user.autopopulate = true
  75. user.roles = User::ROLES[1].to_a
  76.  
  77. user.build_profile(
  78. :name => Faker::Name.first_name,
  79. :family_name => Faker::Name.last_name,
  80. :second_family_name => Faker::Name.last_name,
  81. :job_category => jobs_categories.rand,
  82. :address => address,
  83. :contact_email => user.email,
  84. :phone => Faker.numerify('93 ### ## ##'),
  85. :fax => Faker.numerify('93 ### ## ##'),
  86. :floor => rand(5) + 1,
  87. :room => rand(60) + 1,
  88. :research_interest => Faker::Lorem.sentence(255),
  89. :avatar => avatar
  90. )
  91. user.save
  92. end
  93. if User.find(:all).count > 39
  94. puts "--- #{num_users_created} users created ---"
  95. end
  96.  
  97. num_publications = 50
  98. User.all(:limit => 10).each do |user|
  99. num_publications.times do
  100. pub_types = %w[journal book article_or_chapter_book]
  101. pub_type = pub_types.rand
  102. status = %w[forthcoming submitted under_review]
  103. attributes ={
  104. :published => [true,false].rand,
  105. :pub_type => pub_type,
  106. :title => Faker::Lorem.sentence((4..14).to_a.rand),
  107. :abstract => Faker::Lorem.sentence(255)
  108. }
  109. pub = Publication.new(attributes)
  110. published_attributes = {}
  111. if pub.published
  112. if pub.have_pages?
  113. published_attributes.merge!(
  114. :pub_pages => [Faker.numerify('##-##'),Faker.numerify('##')].rand,
  115. :pub_volume => [Faker.numerify('###'),""].rand,
  116. :pub_issue => [Faker.numerify('###'),""].rand
  117. )
  118. end
  119. if pub.have_media_title?
  120. published_attributes.merge!(:media_title => Faker::Lorem.sentence((4..14).to_a.rand))
  121. end
  122. if pub.have_publisher?
  123. published_attributes.merge!(:publisher => Faker::Lorem.sentence((4..6).to_a.rand))
  124. end
  125. if pub.have_editors?
  126. editors = []
  127. (0..4).to_a.rand.times do
  128. editors<<"#{Faker::Name.last_name}, #{Faker::Name.first_name[0..0]}."
  129. end
  130. editors_txt = editors.join("&")
  131. published_attributes.merge!(:editors => editors_txt)
  132. end
  133. else # pub.published
  134. published_attributes = {
  135. :status => status.rand
  136. }
  137. end
  138. attributes.merge!(published_attributes)
  139. pub = user.publications.create!(attributes)
  140.  
  141. # Asocio entre 1 y 6 perfiles a cada publicación
  142. # associated_profiles = []
  143. pub.profile_positions.build(:profile_id => user.profile.id, :position => 1)
  144. (2..6).to_a.rand.times { |position|
  145. profile_id = (1..num_users_created-10).to_a.rand
  146. profile_id = profile_id == user.profile.id ? user.profile.id + 1 : profile_id
  147. pub.profile_positions.build(:profile_id => profile_id, :position => position + 2)
  148. # associated_profiles<<Profile.find(profile_id)
  149. # pub.profiles<<Profile.find(profile_id)
  150. }
  151. pub.save
  152. end
  153. end
  154. if Publication.find(:all).count > num_publications-1
  155. puts "--- #{num_publications} publications created ---"
  156. end
  157.  
  158. end # task :populate
  159. end # namespace :db
Add Comment
Please, Sign In to add comment