From 55cc1ec090e1a4430d6f382618cd055f8695f302 Mon Sep 17 00:00:00 2001 From: Stella Date: Sat, 8 Mar 2014 18:37:41 +0200 Subject: [PATCH] migrations to integrate answers from attributes into questions --- ...require_handicapped_access_to_questions.rb | 36 +++++++++++++++++++ ...140308100155_add_data_to_question_types.rb | 13 +++++++ .../20140308103633_add_yes_no_to_answers.rb | 9 +++++ ...add_attending_with_partner_to_questions.rb | 36 +++++++++++++++++++ ...staying_at_suggested_hotel_to_questions.rb | 36 +++++++++++++++++++ ...dd_attending_social_events_to_questions.rb | 36 +++++++++++++++++++ db/seeds.rb | 2 +- 7 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20140308095249_add_require_handicapped_access_to_questions.rb create mode 100644 db/migrate/20140308100155_add_data_to_question_types.rb create mode 100644 db/migrate/20140308103633_add_yes_no_to_answers.rb create mode 100644 db/migrate/20140308152659_add_attending_with_partner_to_questions.rb create mode 100644 db/migrate/20140308160034_add_staying_at_suggested_hotel_to_questions.rb create mode 100644 db/migrate/20140308160623_add_attending_social_events_to_questions.rb diff --git a/db/migrate/20140308095249_add_require_handicapped_access_to_questions.rb b/db/migrate/20140308095249_add_require_handicapped_access_to_questions.rb new file mode 100644 index 00000000..248979a8 --- /dev/null +++ b/db/migrate/20140308095249_add_require_handicapped_access_to_questions.rb @@ -0,0 +1,36 @@ +require "./db/migrate/20140308100155_add_data_to_question_types.rb" +require "./db/migrate/20140308103633_add_yes_no_to_answers.rb" + +class AddRequireHandicappedAccessToQuestions < ActiveRecord::Migration + def change + AddDataToQuestionTypes.new.migrate(:up) + AddYesNoToAnswers.new.migrate(:up) + + qtitle = "Do you need handicapped access?" + # Find Answer 'Yes' + answer_yes = Answer.where(:title => "Yes").first + # Check if Question already exists + q = Question.where(:title => qtitle).first + # If question does not exist + if q == nil + # Get Question Type and Answer 'No' + qtype = QuestionType.where(:title => "Yes/No").first + answer_no = Answer.where(:title => "No").first + # Create Question + q = Question.create(:title => qtitle, :question_type_id => qtype.id, :global => true, :answer_ids => [answer_yes.id, answer_no.id]) + end + + # Find Qanswer with answer 'Yes' and associate it with registrations + qa_yes = Qanswer.where(:question_id => q.id).where(:answer_id => answer_yes.id).first + + Conference.all.each do |c| + if c.registrations.where(:handicapped_access_required => true).count > 0 + c.question_ids = c.question_ids << q.id + c.registrations.where(:handicapped_access_required => true).each do |r| + r.qanswer_ids = r.qanswer_ids << qa_yes.id + end + end + end + + end +end diff --git a/db/migrate/20140308100155_add_data_to_question_types.rb b/db/migrate/20140308100155_add_data_to_question_types.rb new file mode 100644 index 00000000..be9647ab --- /dev/null +++ b/db/migrate/20140308100155_add_data_to_question_types.rb @@ -0,0 +1,13 @@ +class AddDataToQuestionTypes < ActiveRecord::Migration + def change + #Create Question Types (yes/no, single choice, multiple choice) if they don't already exist + qtype_yesno = QuestionType.where(:title => "Yes/No").first + QuestionType.create(:title => "Yes/No") if qtype_yesno == nil + + qtype_single = QuestionType.where(:title => "Single Choice").first + QuestionType.create(:title => "Single Choice") if qtype_single == nil + + qtype_multiple = QuestionType.where(:title => "Multiple Choice").first + QuestionType.create(:title => "Multiple Choice") if qtype_multiple == nil + end +end diff --git a/db/migrate/20140308103633_add_yes_no_to_answers.rb b/db/migrate/20140308103633_add_yes_no_to_answers.rb new file mode 100644 index 00000000..f2749d25 --- /dev/null +++ b/db/migrate/20140308103633_add_yes_no_to_answers.rb @@ -0,0 +1,9 @@ +class AddYesNoToAnswers < ActiveRecord::Migration + def change + answer_yes = Answer.where(:title => "Yes").first + Answer.create(:title => "Yes") if answer_yes == nil + + answer_no = Answer.where(:title => "No").first + Answer.create(:title => "No") if answer_no == nil + end +end diff --git a/db/migrate/20140308152659_add_attending_with_partner_to_questions.rb b/db/migrate/20140308152659_add_attending_with_partner_to_questions.rb new file mode 100644 index 00000000..746cb50f --- /dev/null +++ b/db/migrate/20140308152659_add_attending_with_partner_to_questions.rb @@ -0,0 +1,36 @@ +require "./db/migrate/20140308100155_add_data_to_question_types.rb" +require "./db/migrate/20140308103633_add_yes_no_to_answers.rb" + +class AddAttendingWithPartnerToQuestions < ActiveRecord::Migration + def change + AddDataToQuestionTypes.new.migrate(:up) + AddYesNoToAnswers.new.migrate(:up) + + qtitle = "Will you attend with a partner?" + # Find Answer 'Yes' + answer_yes = Answer.where(:title => "Yes").first + # Check if Question already exists + q = Question.where(:title => qtitle).first + # If question does not exist + if q == nil + # Get Question Type and Answer 'No' + qtype = QuestionType.where(:title => "Yes/No").first + answer_no = Answer.where(:title => "No").first + # Create Question + q = Question.create(:title => qtitle, :question_type_id => qtype.id, :global => true, :answer_ids => [answer_yes.id, answer_no.id]) + end + + # Find Qanswer with answer 'Yes' and associate it with registrations + qa_yes = Qanswer.where(:question_id => q.id).where(:answer_id => answer_yes.id).first + + Conference.all.each do |c| + if c.registrations.where(:attending_with_partner => true).count > 0 + c.question_ids = c.question_ids << q.id + c.registrations.where(:attending_with_partner => true).each do |r| + r.qanswer_ids = r.qanswer_ids << qa_yes.id + end + end + end + + end +end diff --git a/db/migrate/20140308160034_add_staying_at_suggested_hotel_to_questions.rb b/db/migrate/20140308160034_add_staying_at_suggested_hotel_to_questions.rb new file mode 100644 index 00000000..be6c05e1 --- /dev/null +++ b/db/migrate/20140308160034_add_staying_at_suggested_hotel_to_questions.rb @@ -0,0 +1,36 @@ +require "./db/migrate/20140308100155_add_data_to_question_types.rb" +require "./db/migrate/20140308103633_add_yes_no_to_answers.rb" + +class AddStayingAtSuggestedHotelToQuestions < ActiveRecord::Migration + def change + AddDataToQuestionTypes.new.migrate(:up) + AddYesNoToAnswers.new.migrate(:up) + + qtitle = "Will you stay at one of the suggested hotels?" + # Find Answer 'Yes' + answer_yes = Answer.where(:title => "Yes").first + # Check if Question already exists + q = Question.where(:title => qtitle).first + # If question does not exist + if q == nil + # Get Question Type and Answer 'No' + qtype = QuestionType.where(:title => "Yes/No").first + answer_no = Answer.where(:title => "No").first + # Create Question + q = Question.create(:title => qtitle, :question_type_id => qtype.id, :global => true, :answer_ids => [answer_yes.id, answer_no.id]) + end + + # Find Qanswer with answer 'Yes' and associate it with registrations + qa_yes = Qanswer.where(:question_id => q.id).where(:answer_id => answer_yes.id).first + + Conference.all.each do |c| + if c.registrations.where(:using_affiliated_lodging => true).count > 0 + c.question_ids = c.question_ids << q.id + c.registrations.where(:using_affiliated_lodging => true).each do |r| + r.qanswer_ids = r.qanswer_ids << qa_yes.id + end + end + end + + end +end diff --git a/db/migrate/20140308160623_add_attending_social_events_to_questions.rb b/db/migrate/20140308160623_add_attending_social_events_to_questions.rb new file mode 100644 index 00000000..a2a877ce --- /dev/null +++ b/db/migrate/20140308160623_add_attending_social_events_to_questions.rb @@ -0,0 +1,36 @@ +require "./db/migrate/20140308100155_add_data_to_question_types.rb" +require "./db/migrate/20140308103633_add_yes_no_to_answers.rb" + +class AddAttendingSocialEventsToQuestions < ActiveRecord::Migration + def change + AddDataToQuestionTypes.new.migrate(:up) + AddYesNoToAnswers.new.migrate(:up) + + qtitle = "Will you attend the social event(s)?" + # Find Answer 'Yes' + answer_yes = Answer.where(:title => "Yes").first + # Check if Question already exists + q = Question.where(:title => qtitle).first + # If question does not exist + if q == nil + # Get Question Type and Answer 'No' + qtype = QuestionType.where(:title => "Yes/No").first + answer_no = Answer.where(:title => "No").first + # Create Question + q = Question.create(:title => qtitle, :question_type_id => qtype.id, :global => true, :answer_ids => [answer_yes.id, answer_no.id]) + end + + # Find Qanswer with answer 'Yes' and associate it with registrations + qa_yes = Qanswer.where(:question_id => q.id).where(:answer_id => answer_yes.id).first + + Conference.all.each do |c| + if c.registrations.where(:attending_social_events => true).count > 0 + c.question_ids = c.question_ids << q.id + c.registrations.where(:attending_social_events => true).each do |r| + r.qanswer_ids = r.qanswer_ids << qa_yes.id + end + end + end + + end +end diff --git a/db/seeds.rb b/db/seeds.rb index 6b7063dc..94232293 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -16,7 +16,7 @@ qtype_multiple = QuestionType.create(:title => "Multiple Choice") answer_yes = Answer.create(:title => "Yes") answer_no = Answer.create(:title => "No") -questions_yes_no = ["Do you need handicapped access to the venue?", "Are you attending with partner?", "Will you attend the social event(s)?", "Will you stay at suggested hotel?"] +questions_yes_no = ["Do you need handicapped access?", "Will you attend with a partner?", "Will you attend the social event(s)?", "Will you stay at one of the suggested hotels?"] for i in questions_yes_no do q = Question.create(:title => i, :question_type_id => qtype_yesno.id, :global => true)