This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/db/seeds.rb

150 lines
3.9 KiB
Ruby
Raw Normal View History

2012-11-15 17:53:17 +00:00
require 'factory_girl'
2012-11-26 16:05:04 +00:00
require 'faker'
2012-11-26 03:52:11 +00:00
require 'json'
2012-11-28 07:20:02 +00:00
CoAuthor.destroy_all
2012-12-03 09:16:28 +00:00
CoAuthorRelation.destroy_all
WritingJournal.destroy_all
2012-12-05 18:10:32 +00:00
WritingJournalFile.destroy_all
JournalLevelType.destroy_all
2012-11-28 07:20:02 +00:00
2012-11-26 03:52:11 +00:00
data = File.read("db/data")
data_json = JSON.parse(data)
2012-11-15 17:53:17 +00:00
2012-11-26 16:05:04 +00:00
name_tw = Array.new 51,""
name_tw = name_tw.map do |p| Faker::Name::name end
name_en = Array.new 51,""
name_en = name_en.map do |p| Faker::Name::name end
2012-11-26 03:52:11 +00:00
2012-11-26 16:05:04 +00:00
email = Array.new 51,""
email= email.map do |p| Faker::Internet.email end
2012-11-26 03:52:11 +00:00
2012-12-05 18:10:32 +00:00
type = ["friend", "teacher", "student", "schoolmate", "parent", "best friend", "instructor", "labmate", "TA", "mate"]
file_desc = Array.new 7, ""
file_desc = file_desc.map do |p| Faker::Lorem.word end
file = ["1.png", "5.pdf", "2.png", "6.pdf", "3.png", "7.pdf", "4.png"]
level = ["SCI", "SCIE", "SSCI", "AH & HCI", "EI", "CSCI", "CSSCI", "TSCI", "TSSCI", "THCI"]
2012-12-03 09:16:28 +00:00
2012-12-06 01:49:15 +00:00
rand = Random.new
2012-11-26 16:05:04 +00:00
FactoryGirl.define do
2012-12-05 18:10:32 +00:00
factory(:journal_level, class: "JournalLevelType") do |f|
f.sequence(:title_translations) do |n|
{ zh_tw: "#{level[n]}",
en: "#{level[n]}" }
end
end
factory(:upload_file, class: "WritingJournalFile") do |f|
f.sequence(:file) do |n|
2012-12-06 01:49:15 +00:00
File.new(File.join(Rails.root, "db" ,"files", file[rand.rand(0..6)]))
2012-12-05 18:10:32 +00:00
end
f.sequence(:title) do |n|
2012-12-06 01:49:15 +00:00
"#{file_desc[rand.rand(0..6)]}"
2012-12-05 18:10:32 +00:00
end
end
2012-12-03 09:16:28 +00:00
factory(:type, class: "CoAuthorRelation") do |f|
f.sequence(:relation_translations) do |n|
{ zh_tw: "#{type[n%type.size]}",
en: "#{type[n%type.size]}" }
end
end
2012-11-26 16:05:04 +00:00
factory(:co_author_candidate, class: "CoAuthor") do |f|
2012-11-26 03:52:11 +00:00
2012-11-26 16:05:04 +00:00
f.sequence(:co_author_translations) do |n|
{ zh_tw: "#{name_tw[n]}",
2012-12-03 09:16:28 +00:00
en: "#{name_en[n]}" }
2012-11-26 03:52:11 +00:00
end
f.sequence(:co_author_relations_id) do |n| "#{CoAuthorRelation.all[n%type.size].id}" end
2012-11-26 16:05:04 +00:00
f.sequence(:email) do |n| "#{email[n]}" end
f.name_id BSON::ObjectId('4f45f3b9e9d02c5db9000067') #user_id, this is Chris' account
end
2012-11-26 16:18:34 +00:00
factory :paper_record, class: "WritingJournal" do |f|
f.sequence(:paper_title_translations) do |n|
{ zh_tw: "tw_#{data_json[n]["paper_title"]}",
en: "en_#{data_json[n]["paper_title"]}" }
end
f.sequence(:journal_title_translations) do |n|
{zh_tw: "tw_#{data_json[n]["booktitle"]}",
en: "en_#{data_json[n]["booktitle"]}"}
end
f.sequence(:abstract) do |n|
"#{data_json[n]["abstract"]}"
end
f.sequence(:isbn) do |n|
"#{data_json[n]["isbn"]}"
end
f.sequence(:year) do |n|
"#{data_json[n]["year"]}"
end
f.sequence(:authors) do |n|
"#{data_json[n]["author"].map{|m| m.split(",").reverse.join(" ")}.join(",")}"
end
f.sequence(:form_to_start) do |n|
"#{data_json[n]["page_from"]}"
end
f.sequence(:form_to_end) do |n|
"#{data_json[n]["page_to"]}"
end
f.sequence(:total_pages) do |n|
"#{data_json[n]["total_page"]}"
end
f.sequence(:language) do |n|
"#{data_json[n]["language"]}"
end
f.sequence(:keywords) do |n|
"#{data_json[n]["abstract"].split[-3..-1].join(",")}"
end
2012-12-05 18:10:32 +00:00
f.sequence(:journal_level_types) do |n|
level = []
2012-12-06 01:49:15 +00:00
total_levels = rand.rand(1..JournalLevelType.count)
2012-12-05 18:10:32 +00:00
total_levels.times do
2012-12-06 01:49:15 +00:00
level << JournalLevelType.all[rand.rand(0..JournalLevelType.count-1)]
2012-12-05 18:10:32 +00:00
end
level.uniq
end
f.sequence(:writing_journal_files) do |n|
files = []
2012-12-06 01:49:15 +00:00
total_files = rand.rand(1..7)
2012-12-05 18:10:32 +00:00
total_files.times do files << FactoryGirl.create(:upload_file) end
files
end
2012-11-26 16:18:34 +00:00
f.create_user_id BSON::ObjectId('4f45f3b9e9d02c5db9000067') #user_id, this is Chris' account
f.update_user_id BSON::ObjectId('4f45f3b9e9d02c5db9000067') #user_id, this is Chris' account
end
end
2012-12-05 18:10:32 +00:00
(level.size - 1).times do
FactoryGirl.create(:journal_level)
end
10.times do
2012-12-03 09:16:28 +00:00
FactoryGirl.create(:type)
end
2012-11-26 16:18:34 +00:00
2012-12-05 18:10:32 +00:00
50.times do
2012-11-26 16:18:34 +00:00
FactoryGirl.create(:paper_record)
2012-11-26 16:05:04 +00:00
end
2012-11-15 17:53:17 +00:00
2012-12-05 18:10:32 +00:00
50.times do
2012-11-26 16:05:04 +00:00
FactoryGirl.create(:co_author_candidate)
2012-11-15 17:53:17 +00:00
end