diff options
author | 2010-06-24 20:05:17 +0200 | |
---|---|---|
committer | 2010-06-25 19:39:42 +0200 | |
commit | 6b1f8b6ca389a53f9ff7e641fe464ab22ed2c214 (patch) | |
tree | 8f5d3d5aa2721c7834fa6d301fe3bd912ebfbc4c /db | |
parent | Project acceptances (diff) | |
download | recruiting-webapp-6b1f8b6ca389a53f9ff7e641fe464ab22ed2c214.tar.gz recruiting-webapp-6b1f8b6ca389a53f9ff7e641fe464ab22ed2c214.tar.bz2 recruiting-webapp-6b1f8b6ca389a53f9ff7e641fe464ab22ed2c214.zip |
Rewrite seed to make it cleaner
Questions and users data moved to external YAML files.
Diffstat (limited to 'db')
-rw-r--r-- | db/fixtures/questions.yml | 60 | ||||
-rw-r--r-- | db/fixtures/users.yml | 48 | ||||
-rw-r--r-- | db/seeds.rb | 191 |
3 files changed, 180 insertions, 119 deletions
diff --git a/db/fixtures/questions.yml b/db/fixtures/questions.yml new file mode 100644 index 0000000..fb3e910 --- /dev/null +++ b/db/fixtures/questions.yml @@ -0,0 +1,60 @@ +ebuild_q1: + title: Big changes in Gentoo + documentation: GLEPs + question_category: ebuild + content: What is the proper method for suggesting a wide-ranging feature + or enhancement to Gentoo? Describe the process for getting this feature + approved and implemented. + +ebuild_q2: + title: Responsibilities + documentation: devrel policy + question_category: ebuild + content: Who should be contacted with complaints about specific developers + or projects? + +ebuild_q3: + title: Gentoo mailing lists + documentation: gentoo.org + question_category: ebuild + content: "When is it appropriate to post to the following mailing lists: + gentoo-core, gentoo-dev, gentoo-dev-announce, gentoo-project?" + +mentor_q1: + title: Scopes in ebuild + documentation: handbook + question_category: mentoring + content: "What's the difference between local and global scope in an ebuild?" + +mentor_q2: + title: Optional SSL support in ebuild + documentation: devmanual + question_category: mentoring + content: 'You have a patch for foomatic which enables SSL support that is + optional at build time. Assuming that foomatic uses an autotools based + build system provide most probable changes required in an EAPI="0" ebuild. + What should be done for the ebuild in case it uses EAPI="2"?' + +mentor_q3: + title: Improve maintainability of ebuild + documentation: devmanual + question_category: mentoring + content: You are writing an ebuild for the foomatic package. Upstream calls + the current version "1.3-7b" (but this is _not_ a beta release). How would the + ebuild be named? What's wrong with the ebuild snippet below and how should this + be written to aid maintainability?<br/><br/> + SRC_URI="http://foomatic.example.com/download/foomatic-1.3-7b.tar.bz2" + S=${WORKDIR}/foomatic-1.3-7b' + +non_q1: + title: Gentoo Foundation + documentation: gentoo.org + question_category: non_ebuild + content: What is the Gentoo Foundation? How does one apply for + membership and who are eligible? + +non_q2: + title: Gentoo Council + documentation: GLEPs + question_category: non_ebuild + content: What is the purpose of the Gentoo Council? diff --git a/db/fixtures/users.yml b/db/fixtures/users.yml new file mode 100644 index 0000000..c33ba0b --- /dev/null +++ b/db/fixtures/users.yml @@ -0,0 +1,48 @@ +admin: + email_address: admin@recruiters.org + name: Admin + nick: Admin + role: recruiter + password: secret + password_confirmation: secret + administrator: true + +recruiter: + email_address: recruiter@recruiters.org + name: Recruiter + nick: Recruiter + role: recruiter + password: secret + password_confirmation: secret + +mentor: + email_address: mentor@recruits.org + name: Mentor who was recruit + nick: Mentor who was recruit + role: mentor + password: secret + password_confirmation: secret + +recruit: + email_address: recruit@recruits.org + name: Recruit + role: recruit + password: secret + password_confirmation: secret + mentor: mentor + +advanced: + email_address: advanced@recruits.org + name: Advanced Recruit + role: recruit + password: secret + password_confirmation: secret + mentor: mentor + +middle: + email_address: middle@recruits.org + name: Middle Recruit + role: recruit + password: secret + password_confirmation: secret + mentor: mentor diff --git a/db/seeds.rb b/db/seeds.rb index fb4d7f3..08b3f1d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,11 +1,34 @@ -# This removes existing database entries, so ask for confirmation - -response = nil -while response != 'y' - puts "Warning, this will remove content of your database, are you sure you want it(y/n)?" - STDOUT.flush() - response = STDIN.gets.chomp - exit if 'n' == response +# This removes existing database entries +# but don't ask for confirmation - it's prohibited on Heroku +# In future (when we will have some important data) +# we should consider removing it (or at least disable it in production mode) +class SeedHelper + attr_accessor :objects + def initialize + @objects ={} + end + + # Read data from file + # in each item replace values of fields given in replace_with_objects with objects + # and create! + def read_yaml(file, klass, replace_with_objects) + for item_array in YAML::load_file(file) + name = item_array[0] + hash = item_array[1] + for field in replace_with_objects + hash[field] = @objects[hash[field]] + end + @objects[name] = klass.create! hash + end + end + + def answer_many(user, questions, answer_hash) + for question in questions + answer_hash[:question] = @objects[question] + answer_hash[:owner] = @objects[user] + Answer.create! answer_hash + end + end end # Remove existing database entries @@ -16,130 +39,60 @@ Question.destroy_all UserCategory.destroy_all User.destroy_all +seeder = SeedHelper.new + # Question categories -ebuild = QuestionCategory.create! :name => 'Ebuild quiz' -mentoring = QuestionCategory.create! :name => 'End of mentoring quiz' -non = QuestionCategory.create! :name => 'Non-ebuild staff quiz' - -# Ebuild quiz questions -ebuild_q1 = Question.create! :title => 'Big changes in Gentoo', - :content => 'What is the proper method for suggesting a wide-ranging feature ' + - 'or enhancement to Gentoo? Describe the process for getting this feature ' + - 'approved and implemented.', :documentation => 'GLEPs', :question_category => ebuild - -ebuild_q2 = Question.create! :title => 'Responsibilities', - :content => 'Who should be contacted with complaints about specific ' + - 'developers or projects?', :documentation => 'devrel policy', :question_category => ebuild - -ebuild_q3 = Question.create! :title => 'Gentoo mailing lists', - :content => 'When is it appropriate to post to the following mailing lists:' + - 'gentoo-core, gentoo-dev, gentoo-dev-announce, gentoo-project?', - :documentation => 'gentoo.org', :question_category => ebuild - -# End of mentoring quiz questions -mentor_q1 = Question.create! :title => 'Scopes in ebuild', - :content => "What's the difference between local and global scope in an ebuild?", - :documentation => 'handbook', :question_category => mentoring - -mentor_q2 = Question.create! :title => 'Optional SSL support in ebuild', - :content => 'You have a patch for foomatic which enables SSL support that is' + - ' optional at build time. Assuming that foomatic uses an autotools based ' + - 'build system provide most probable changes required in an EAPI="0" ebuild.' + - 'What should be done for the ebuild in case it uses EAPI="2"?', - :documentation => 'devmanual', :question_category => mentoring - -mentor_q3 = Question.create! :title => 'Improve maintainability of ebuild', - :content => 'You are writing an ebuild for the foomatic package. Upstream calls' + - 'the current version "1.3-7b" (but this is _not_ a beta release). How would the ' + - "ebuild be named? What's wrong with the ebuild snippet below and how should this " + - 'be written to aid maintainability?<br/><br/>' + - 'SRC_URI="http://foomatic.example.com/download/foomatic-1.3-7b.tar.bz2"'+ - 'S=${WORKDIR}/foomatic-1.3-7b ', - :documentation => 'devmanual', :question_category => mentoring - -# Nonebuild staff quiz questions -non_q1 = Question.create! :title => 'Gentoo Foundation', - :content => 'What is the Gentoo Foundation? How does one apply for membership and who are eligible?', - :documentation => 'gentoo.org', :question_category => non - -non_q2 = Question.create! :title => 'Gentoo Council', - :content => 'What is the purpose of the Gentoo Council?', - :documentation => 'GLEPs', :question_category => non - -non_q3 = Question.create! :title => 'Gentoo Council', - :content => 'What is the purpose of the Gentoo Council?', - :documentation => 'GLEPs', :question_category => non - -# Recruiters -admin = User.create! :email_address => 'admin@recruiters.org', :name => 'Admin', - :role => :recruiter, :password => 'secret', :password_confirmation => 'secret', - :nick => 'admin' - -recruiter = User.create! :email_address => 'recruiter@recruiters.org', - :name => 'Recruiter', :role => :recruiter, :password => 'secret', - :password_confirmation => 'secret', :nick => 'recruiter' - -# Mentor. Used to be recruit, has answered questions -# in ebuild and end of mentoring quizzes and question categories. -mentor = User.create! :email_address => 'mentor@recruits.org', - :name => 'Recruit who is Mentor', :role => :mentor, :password => 'secret', - :password_confirmation => 'secret', :mentor => recruiter, :nick => 'mentor' - -# Recruit - no questions answered -recruit = User.create! :email_address => 'recruit@recruits.org', - :name => 'Recruit', :role => :recruit, :password => 'secret', - :password_confirmation => 'secret', :mentor => mentor - -# Recruit - completed ebuild quiz -advanced = User.create! :email_address => 'advanced@recruits.org', - :name => 'Advanced Recruit', :role => :recruit, :password => 'secret', - :password_confirmation => 'secret', :mentor => mentor - -# Recruit - some questions answered -recruit_mid = User.create! :email_address => 'recruit-mid@recruits.org', - :name => 'Recruit in middle of ebuild quizz', :role => :recruit, :password => 'secret', - :password_confirmation => 'secret', :mentor => mentor +seeder.objects['ebuild'] = QuestionCategory.create! :name => 'Ebuild quiz' +seeder.objects['mentoring'] = QuestionCategory.create! :name => 'End of mentoring quiz' +seeder.objects['non'] = QuestionCategory.create! :name => 'Non-ebuild staff quiz' + +# Questions - load from YAML file +seeder.read_yaml 'db/fixtures/questions.yml', Question, 'question_category' + +# Users - load from YAML file +seeder.read_yaml 'db/fixtures/users.yml', User, 'mentor' # Categories for users -UserCategory.create! [{:question_category => ebuild, :user => mentor}, - {:question_category => mentoring, :user => mentor}, - {:question_category => ebuild, :user => recruit}, - {:question_category => ebuild, :user => recruit_mid}, - {:question_category => ebuild, :user => advanced}, - {:question_category => mentoring, :user => advanced}] - -def answer_many(users, questions, answer_hash) - for question in questions - answer_hash[:question] = question - for user in users - answer_hash[:owner] = user - Answer.create! answer_hash - end - end +user_cats = [ + ['ebuild', 'mentor'], + ['ebuild', 'recruit'], + ['ebuild', 'middle'], + ['ebuild', 'advanced'], + ['mentoring', 'advanced'], + ['mentoring', 'mentor']] + +for uc in user_cats + UserCategory.create! :question_category => seeder.objects[uc[0]], :user => seeder.objects[uc[1]] end + +ebuild_q = ['ebuild_q1', 'ebuild_q2', 'ebuild_q3'] +mentor_q = ['mentor_q1', 'mentor_q2', 'mentor_q3'] +non_q = ['non_q1', 'non_q', 'non_q3'] + # non-approved answers ans_hash = {:content => 'Some answer'} -answer_many [recruit], [ebuild_q1, ebuild_q2, ebuild_q3], ans_hash -answer_many [recruit_mid], [ebuild_q1, ebuild_q2], ans_hash -answer_many [advanced], [mentor_q1, mentor_q2, mentor_q3], ans_hash +seeder.answer_many 'recruit', ebuild_q, ans_hash +seeder.answer_many 'middle', ebuild_q - ['ebuild_q3'], ans_hash +seeder.answer_many 'advanced', mentor_q, ans_hash # approved answers ans_hash[:approved] = true -answer_many [mentor, advanced], [ebuild_q1, ebuild_q2, ebuild_q3], ans_hash -answer_many [mentor], [mentor_q1, mentor_q2, mentor_q3], ans_hash - -# reference answers -for q in [mentor_q1, mentor_q2, mentor_q3, ebuild_q1, ebuild_q2, ebuild_q3, - non_q1, non_q2, non_q3] - Answer.create! :owner => recruiter, :question => q, :content => "Some reference answer", - :reference => true -end +seeder.answer_many 'mentor', ebuild_q, ans_hash +seeder.answer_many 'advanced', ebuild_q, ans_hash +seeder.answer_many 'mentor', mentor_q, ans_hash + +# reference answers for most questions +seeder.answer_many 'recruiter', mentor_q + ebuild_q + non_q - ['ebuild_q1', 'non_q1'], + {:content => "Some reference answer", :reference => true} + +advanced = seeder.objects['advanced'] for ans in advanced.answers Comment.create( :answer => ans, :owner => advanced.mentor, :content => "some comment") end -for q in [ebuild_q1, ebuild_q2, ebuild_q3] - Comment.create( :answer => q.answer_of(advanced), :owner => advanced.mentor, :content => "some other comment") +for q in ebuild_q + Comment.create( :answer => (seeder.objects[q].answer_of advanced), :owner => advanced.mentor, + :content => "Some other comment") end |