migrations to integrate answers from attributes into questions

This commit is contained in:
Stella 2014-03-08 18:37:41 +02:00
parent 628211f0fc
commit 55cc1ec090
7 changed files with 167 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)