From 633a68675a334ba4dcdb70116dfd5fb87c9d031e Mon Sep 17 00:00:00 2001 From: Rueshyna Date: Thu, 6 Dec 2012 22:05:22 +0800 Subject: [PATCH] make factory girl file clean --- Gemfile | 2 +- app/views/desktop/co_authors/index.html.erb | 4 +- db/seeds.rb | 135 +----------------- spec/factories/co_author_relations.rb | 12 ++ spec/factories/co_authors.rb | 22 +++ spec/factories/journal_level_types.rb | 12 ++ spec/factories/writing_journal_files.rb | 19 +++ spec/factories/writing_journals.rb | 76 ++++++++++ .../requests/desktop/co_authors_pages.spec.rb | 4 +- spec/spec_helper.rb | 2 +- 10 files changed, 150 insertions(+), 138 deletions(-) create mode 100644 spec/factories/co_author_relations.rb create mode 100644 spec/factories/co_authors.rb create mode 100644 spec/factories/journal_level_types.rb create mode 100644 spec/factories/writing_journal_files.rb create mode 100644 spec/factories/writing_journals.rb diff --git a/Gemfile b/Gemfile index ca24e34f..e9f5c697 100644 --- a/Gemfile +++ b/Gemfile @@ -88,7 +88,7 @@ group :test, :development do gem "delorean" gem "watchr" gem "spork" - # gem "capybara" + gem "capybara" # gem 'yard' # gem "bluecloth" end diff --git a/app/views/desktop/co_authors/index.html.erb b/app/views/desktop/co_authors/index.html.erb index ec9e156d..9881e2bd 100644 --- a/app/views/desktop/co_authors/index.html.erb +++ b/app/views/desktop/co_authors/index.html.erb @@ -1,6 +1,6 @@
- <%= link_to "New Co-Author", new_desktop_co_author_path, :class=>"bt-co-author fn_btn hp hh2 thmc2 thmtxt" %> + <%= link_to "New Co-Author", new_desktop_co_author_path, :class=>"bt-co-author fn_btn hp hh2 thmc2 thmtxt" %> <%= link_to "New Type", desktop_co_author_relations_path, :class=>"bt-new-type fn_btn hp hh2 thmc2 thmtxt" %>
@@ -31,4 +31,4 @@ <% end %> - \ No newline at end of file + diff --git a/db/seeds.rb b/db/seeds.rb index e73cd4ca..0f4f8b98 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,143 +1,14 @@ require 'factory_girl' require 'faker' -require 'json' -CoAuthor.destroy_all -CoAuthorRelation.destroy_all -WritingJournal.destroy_all -WritingJournalFile.destroy_all -JournalLevelType.destroy_all +#Dir[Rails.root.join("spec/factories/*.rb")].each {|f| require f} -data = File.read("db/data") -data_json = JSON.parse(data) - -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 - -email = Array.new 51,"" -email= email.map do |p| Faker::Internet.email end - -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"] - -rand = Random.new - -FactoryGirl.define do - 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| - File.new(File.join(Rails.root, "db" ,"files", file[rand.rand(0..6)])) - end - f.sequence(:title) do |n| - "#{file_desc[rand.rand(0..6)]}" - end - end - - 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 - - factory(:co_author_candidate, class: "CoAuthor") do |f| - - f.sequence(:co_author_translations) do |n| - { zh_tw: "#{name_tw[n]}", - en: "#{name_en[n]}" } - end - f.sequence(:co_author_relations_id) do |n| "#{CoAuthorRelation.all[n%type.size].id}" end - f.sequence(:email) do |n| "#{email[n]}" end - f.name_id BSON::ObjectId('4f45f3b9e9d02c5db9000067') #user_id, this is Chris' account - end - - 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 - - f.sequence(:journal_level_types) do |n| - level = [] - total_levels = rand.rand(1..JournalLevelType.count) - total_levels.times do - level << JournalLevelType.all[rand.rand(0..JournalLevelType.count-1)] - end - level.uniq - end - - f.sequence(:writing_journal_files) do |n| - files = [] - total_files = rand.rand(1..7) - total_files.times do files << FactoryGirl.create(:upload_file) end - files - end - 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 - -(level.size - 1).times do +10.times do FactoryGirl.create(:journal_level) end 10.times do - FactoryGirl.create(:type) + FactoryGirl.create(:relations) end 50.times do diff --git a/spec/factories/co_author_relations.rb b/spec/factories/co_author_relations.rb new file mode 100644 index 00000000..f157983e --- /dev/null +++ b/spec/factories/co_author_relations.rb @@ -0,0 +1,12 @@ +CoAuthorRelation.destroy_all + +types = ["friend", "teacher", "student", "schoolmate", "parent", "best friend", "instructor", "labmate", "TA", "mate"] + +FactoryGirl.define do + factory(:relations, class: "CoAuthorRelation") do |f| + f.sequence(:relation_translations) do |n| + { zh_tw: "#{types[n%types.size]}", + en: "#{types[n%types.size]}" } + end + end +end diff --git a/spec/factories/co_authors.rb b/spec/factories/co_authors.rb new file mode 100644 index 00000000..8afff831 --- /dev/null +++ b/spec/factories/co_authors.rb @@ -0,0 +1,22 @@ +CoAuthor.destroy_all + +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 + +email = Array.new 51,"" +email= email.map do |p| Faker::Internet.email end + +FactoryGirl.define do + factory(:co_author_candidate, class: "CoAuthor") do |f| + + f.sequence(:co_author_translations) do |n| + { zh_tw: "#{name_tw[n]}", + en: "#{name_en[n]}" } + end + f.sequence(:co_author_relations_id) do |n| "#{CoAuthorRelation.all[n%CoAuthorRelation.count].id}" end + f.sequence(:email) do |n| "#{email[n]}" end + f.name_id BSON::ObjectId('4f45f3b9e9d02c5db9000067') #user_id, this is Chris' account + end +end diff --git a/spec/factories/journal_level_types.rb b/spec/factories/journal_level_types.rb new file mode 100644 index 00000000..0dddf7bd --- /dev/null +++ b/spec/factories/journal_level_types.rb @@ -0,0 +1,12 @@ +JournalLevelType.destroy_all + +level = ["SCI", "SCIE", "SSCI", "AH & HCI", "EI", "CSCI", "CSSCI", "TSCI", "TSSCI", "THCI"] + +FactoryGirl.define do + factory(:journal_level, class: "JournalLevelType") do |f| + f.sequence(:title_translations) do |n| + { zh_tw: "#{level[n]}", + en: "#{level[n]}" } + end + end +end diff --git a/spec/factories/writing_journal_files.rb b/spec/factories/writing_journal_files.rb new file mode 100644 index 00000000..4fc43b74 --- /dev/null +++ b/spec/factories/writing_journal_files.rb @@ -0,0 +1,19 @@ +WritingJournalFile.destroy_all + +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"] + +rand = Random.new + +FactoryGirl.define do + factory(:upload_file, class: "WritingJournalFile") do |f| + f.sequence(:file) do |n| + File.new(File.join(Rails.root, "db" ,"files", file[rand.rand(0..6)])) + end + f.sequence(:title) do |n| + "#{file_desc[rand.rand(0..6)]}" + end + end +end diff --git a/spec/factories/writing_journals.rb b/spec/factories/writing_journals.rb new file mode 100644 index 00000000..e74ff32f --- /dev/null +++ b/spec/factories/writing_journals.rb @@ -0,0 +1,76 @@ +require 'json' + +WritingJournal.destroy_all + +data = File.read("#{Rails.root}/db/data") +data_json = JSON.parse(data) + +rand = Random.new + +FactoryGirl.define do + factory :paper_record, class: "WritingJournal" do |f| + f.sequence(:paper_title_translations) do |n| + { zh_tw: "#{data_json[n]["paper_title"]}_tw", + en: "#{data_json[n]["paper_title"]}_en" } + end + + f.sequence(:journal_title_translations) do |n| + {zh_tw: "#{data_json[n]["booktitle"]}_tw", + en: "#{data_json[n]["booktitle"]}_en"} + 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 + + f.sequence(:journal_level_types) do |n| + level = [] + total_levels = rand.rand(1..JournalLevelType.count) + total_levels.times do + level << JournalLevelType.all[rand.rand(0..JournalLevelType.count-1)] + end + level.uniq + end + + f.sequence(:writing_journal_files) do |n| + files = [] + total_files = rand.rand(1..7) + total_files.times do files << FactoryGirl.create(:upload_file) end + files + end + 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 diff --git a/spec/requests/desktop/co_authors_pages.spec.rb b/spec/requests/desktop/co_authors_pages.spec.rb index f68d40a9..d2957e10 100644 --- a/spec/requests/desktop/co_authors_pages.spec.rb +++ b/spec/requests/desktop/co_authors_pages.spec.rb @@ -4,8 +4,8 @@ describe "Co-Author pages" do subject {co-author} describe "Co-Author list" do - before { visit desktop_co_authors } - it { should have_selector('a', text: 'New Co-Author') } + before { visit desktop_co_authors_path } + it { should have_selector('a', text: "New Co-Author") } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cd0b93ad..9d2b3d0d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -56,4 +56,4 @@ end # - Any code that is left outside of the blocks will be ran during preforking # and during each_run! # - These instructions should self-destruct in 10 seconds. If they don't, -# feel free to delete them. \ No newline at end of file +# feel free to delete them.