From 59f2b636c55318211024fbd4647f0a1261034e60 Mon Sep 17 00:00:00 2001 From: Stella Date: Sun, 9 Nov 2014 18:25:36 +0200 Subject: [PATCH] Migrate registration attributes to questions --- app/models/qanswer.rb | 2 + app/models/question.rb | 11 ++- app/models/registration.rb | 6 +- ...require_handicapped_access_to_questions.rb | 96 +++++++++++++++++++ ...add_attending_with_partner_to_questions.rb | 96 +++++++++++++++++++ ...staying_at_suggested_hotel_to_questions.rb | 96 +++++++++++++++++++ ...dd_attending_social_events_to_questions.rb | 96 +++++++++++++++++++ db/schema.rb | 6 +- db/seeds.rb | 7 +- 9 files changed, 401 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20141031225545_add_require_handicapped_access_to_questions.rb create mode 100644 db/migrate/20141031225606_add_attending_with_partner_to_questions.rb create mode 100644 db/migrate/20141031225620_add_staying_at_suggested_hotel_to_questions.rb create mode 100644 db/migrate/20141031225635_add_attending_social_events_to_questions.rb diff --git a/app/models/qanswer.rb b/app/models/qanswer.rb index bcb72da9..48448b04 100644 --- a/app/models/qanswer.rb +++ b/app/models/qanswer.rb @@ -5,4 +5,6 @@ class Qanswer < ActiveRecord::Base belongs_to :answer, dependent: :delete has_and_belongs_to_many :registrations + + validates :question, :answer, presence: true end diff --git a/app/models/question.rb b/app/models/question.rb index 96a5b8db..095ac508 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -7,8 +7,13 @@ class Question < ActiveRecord::Base has_many :qanswers, dependent: :delete_all has_many :answers, through: :qanswers, dependent: :delete_all - validates :title, presence: true - validates :answers, presence: true - + validates :title, :question_type_id, presence: true + validate :existing_answers accepts_nested_attributes_for :answers, allow_destroy: true + + private + + def existing_answers + errors.add(:base, 'The question must have answers') if self.answers.blank? + end end diff --git a/app/models/registration.rb b/app/models/registration.rb index 59162c95..b595b310 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -11,10 +11,8 @@ class Registration < ActiveRecord::Base has_many :events_registrations has_many :workshops, through: :events_registrations, source: :event - attr_accessible :user_id, :conference_id, :attending_social_events, :attending_with_partner, - :using_affiliated_lodging, :arrival, :departure, :user_attributes, :attended, - :other_dietary_choice, :dietary_choice_id, :handicapped_access_required, - :social_event_ids, :other_special_needs, + attr_accessible :user_id, :conference_id, :arrival, :departure, :user_attributes, :attended, + :other_dietary_choice, :dietary_choice_id, :social_event_ids, :other_special_needs, :event_ids, :volunteer, :vchoice_ids, :qanswer_ids, :qanswers_attributes accepts_nested_attributes_for :user diff --git a/db/migrate/20141031225545_add_require_handicapped_access_to_questions.rb b/db/migrate/20141031225545_add_require_handicapped_access_to_questions.rb new file mode 100644 index 00000000..8e25ee05 --- /dev/null +++ b/db/migrate/20141031225545_add_require_handicapped_access_to_questions.rb @@ -0,0 +1,96 @@ +class AddRequireHandicappedAccessToQuestions < ActiveRecord::Migration + class TempRegistration < ActiveRecord::Base + self.table_name = 'registrations' + + belongs_to :temp_conference + attr_accessible :handicapped_access_required + end + + class TempConference < ActiveRecord::Base + self.table_name = 'conferences' + + has_many :temp_registrations + has_many :temp_questions + end + + class TempQuestionType < ActiveRecord::Base + self.table_name = 'question_types' + + attr_accessible :title + has_many :temp_questions + end + + class TempQuestion < ActiveRecord::Base + self.table_name = 'questions' + + attr_accessible :title, :global, :question_type_id + has_many :temp_qanswers + has_many :temp_answers, through: :temp_qanswers + belongs_to :temp_question_type + has_and_belongs_to_many :temp_conferences + end + + class TempAnswer < ActiveRecord::Base + self.table_name = 'answers' + + attr_accessible :title + has_many :temp_qanswers + has_many :temp_questions, through: :temp_qanswers + end + + class TempQanswer < ActiveRecord::Base + self.table_name = 'qanswers' + + attr_accessible :question_id, :answer_id + belongs_to :temp_question + belongs_to :temp_answer + end + + class TempConferencesQuestions < ActiveRecord::Base + self.table_name = 'conferences_questions' + + attr_accessible :question_id, :conference_id + end + + class TempQanswerRegistration < ActiveRecord::Base + self.table_name = 'qanswers_registrations' + + attr_accessible :registration_id, :qanswer_id + end + + def change + # Create Question of yes/no type + qtype = TempQuestionType.find_or_create_by!(title: 'Yes/No') + answer_yes = TempAnswer.find_or_create_by!(title: 'Yes') + answer_no = TempAnswer.find_or_create_by!(title: 'No') + + # Find existing question or initialize it + q = TempQuestion.find_or_initialize_by(title: 'Do you need handicapped access?', + question_type_id: qtype.id, + global: true) + # Save question + q.save! + + # Associate answers with the question, unless they already exist + qa_yes = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_yes.id) + qa_no = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_no.id) + + # Save question-answer associations + qa_yes.save! + qa_no.save! + + TempConference.all.each do |c| + # Make the question available for the conference + TempConferencesQuestions.find_or_create_by!(conference_id: c.id, question_id: q.id) + + TempRegistration.where(conference_id: c.id).each do |r| + if r.handicapped_access_required + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_yes.id) + else + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_no.id) + end + end + end + remove_column :registrations, :handicapped_access_required, :boolean + end +end diff --git a/db/migrate/20141031225606_add_attending_with_partner_to_questions.rb b/db/migrate/20141031225606_add_attending_with_partner_to_questions.rb new file mode 100644 index 00000000..07f30891 --- /dev/null +++ b/db/migrate/20141031225606_add_attending_with_partner_to_questions.rb @@ -0,0 +1,96 @@ +class AddAttendingWithPartnerToQuestions < ActiveRecord::Migration + class TempRegistration < ActiveRecord::Base + self.table_name = 'registrations' + + belongs_to :temp_conference + attr_accessible :attending_with_partner + end + + class TempConference < ActiveRecord::Base + self.table_name = 'conferences' + + has_many :temp_registrations + has_many :temp_questions + end + + class TempQuestionType < ActiveRecord::Base + self.table_name = 'question_types' + + attr_accessible :title + has_many :temp_questions + end + + class TempQuestion < ActiveRecord::Base + self.table_name = 'questions' + + attr_accessible :title, :global, :question_type_id + has_many :temp_qanswers + has_many :temp_answers, through: :temp_qanswers + belongs_to :temp_question_type + has_and_belongs_to_many :temp_conferences + end + + class TempAnswer < ActiveRecord::Base + self.table_name = 'answers' + + attr_accessible :title + has_many :temp_qanswers + has_many :temp_questions, through: :temp_qanswers + end + + class TempQanswer < ActiveRecord::Base + self.table_name = 'qanswers' + + attr_accessible :question_id, :answer_id + belongs_to :temp_question + belongs_to :temp_answer + end + + class TempConferencesQuestions < ActiveRecord::Base + self.table_name = 'conferences_questions' + + attr_accessible :question_id, :conference_id + end + + class TempQanswerRegistration < ActiveRecord::Base + self.table_name = 'qanswers_registrations' + + attr_accessible :registration_id, :qanswer_id + end + + def change + # Create Question of yes/no type + qtype = TempQuestionType.find_or_create_by!(title: 'Yes/No') + answer_yes = TempAnswer.find_or_create_by!(title: 'Yes') + answer_no = TempAnswer.find_or_create_by!(title: 'No') + + # Find existing question or initialize it + q = TempQuestion.find_or_initialize_by(title: 'Will you attend with a partner?', + question_type_id: qtype.id, + global: true) + # Save question + q.save! + + # Associate answers with the question, unless they already exist + qa_yes = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_yes.id) + qa_no = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_no.id) + + # Save question-answer associations + qa_yes.save! + qa_no.save! + + TempConference.all.each do |c| + # Make the question available for the conference + TempConferencesQuestions.find_or_create_by!(conference_id: c.id, question_id: q.id) + + TempRegistration.where(conference_id: c.id).each do |r| + if r.attending_with_partner + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_yes.id) + else + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_no.id) + end + end + end + remove_column :registrations, :attending_with_partner, :boolean + end +end diff --git a/db/migrate/20141031225620_add_staying_at_suggested_hotel_to_questions.rb b/db/migrate/20141031225620_add_staying_at_suggested_hotel_to_questions.rb new file mode 100644 index 00000000..55ad4869 --- /dev/null +++ b/db/migrate/20141031225620_add_staying_at_suggested_hotel_to_questions.rb @@ -0,0 +1,96 @@ +class AddStayingAtSuggestedHotelToQuestions < ActiveRecord::Migration + class TempRegistration < ActiveRecord::Base + self.table_name = 'registrations' + + belongs_to :temp_conference + attr_accessible :using_affiliated_lodging + end + + class TempConference < ActiveRecord::Base + self.table_name = 'conferences' + + has_many :temp_registrations + has_many :temp_questions + end + + class TempQuestionType < ActiveRecord::Base + self.table_name = 'question_types' + + attr_accessible :title + has_many :temp_questions + end + + class TempQuestion < ActiveRecord::Base + self.table_name = 'questions' + + attr_accessible :title, :global, :question_type_id + has_many :temp_qanswers + has_many :temp_answers, through: :temp_qanswers + belongs_to :temp_question_type + has_and_belongs_to_many :temp_conferences + end + + class TempAnswer < ActiveRecord::Base + self.table_name = 'answers' + + attr_accessible :title + has_many :temp_qanswers + has_many :temp_questions, through: :temp_qanswers + end + + class TempQanswer < ActiveRecord::Base + self.table_name = 'qanswers' + + attr_accessible :question_id, :answer_id + belongs_to :temp_question + belongs_to :temp_answer + end + + class TempConferencesQuestions < ActiveRecord::Base + self.table_name = 'conferences_questions' + + attr_accessible :question_id, :conference_id + end + + class TempQanswerRegistration < ActiveRecord::Base + self.table_name = 'qanswers_registrations' + + attr_accessible :registration_id, :qanswer_id + end + + def change + # Create Question of yes/no type + qtype = TempQuestionType.find_or_create_by!(title: 'Yes/No') + answer_yes = TempAnswer.find_or_create_by!(title: 'Yes') + answer_no = TempAnswer.find_or_create_by!(title: 'No') + + # Find existing question or initialize it + q = TempQuestion.find_or_initialize_by(title: 'Will you stay at one of the suggested hotels?', + question_type_id: qtype.id, + global: true) + # Save question + q.save! + + # Associate answers with the question, unless they already exist + qa_yes = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_yes.id) + qa_no = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_no.id) + + # Save question-answer associations + qa_yes.save! + qa_no.save! + + TempConference.all.each do |c| + # Make the question available for the conference + TempConferencesQuestions.find_or_create_by!(conference_id: c.id, question_id: q.id) + + TempRegistration.where(conference_id: c.id).each do |r| + if r.using_affiliated_lodging + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_yes.id) + else + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_no.id) + end + end + end + remove_column :registrations, :using_affiliated_lodging, :boolean + end +end diff --git a/db/migrate/20141031225635_add_attending_social_events_to_questions.rb b/db/migrate/20141031225635_add_attending_social_events_to_questions.rb new file mode 100644 index 00000000..985b6e17 --- /dev/null +++ b/db/migrate/20141031225635_add_attending_social_events_to_questions.rb @@ -0,0 +1,96 @@ +class AddAttendingSocialEventsToQuestions < ActiveRecord::Migration + class TempRegistration < ActiveRecord::Base + self.table_name = 'registrations' + + attr_accessible :attending_social_events + belongs_to :temp_conference + end + + class TempConference < ActiveRecord::Base + self.table_name = 'conferences' + + has_many :temp_registrations + has_many :temp_questions + end + + class TempQuestionType < ActiveRecord::Base + self.table_name = 'question_types' + + attr_accessible :title + has_many :temp_questions + end + + class TempQuestion < ActiveRecord::Base + self.table_name = 'questions' + + attr_accessible :title, :global, :question_type_id + has_many :temp_qanswers + has_many :temp_answers, through: :temp_qanswers + belongs_to :temp_question_type + has_and_belongs_to_many :temp_conferences + end + + class TempAnswer < ActiveRecord::Base + self.table_name = 'answers' + + attr_accessible :title + has_many :temp_qanswers + has_many :temp_questions, through: :temp_qanswers + end + + class TempQanswer < ActiveRecord::Base + self.table_name = 'qanswers' + + attr_accessible :question_id, :answer_id + belongs_to :temp_question + belongs_to :temp_answer + end + + class TempConferencesQuestions < ActiveRecord::Base + self.table_name = 'conferences_questions' + + attr_accessible :question_id, :conference_id + end + + class TempQanswerRegistration < ActiveRecord::Base + self.table_name = 'qanswers_registrations' + + attr_accessible :registration_id, :qanswer_id + end + + def change + # Create Question of yes/no type + qtype = TempQuestionType.find_or_create_by!(title: 'Yes/No') + answer_yes = TempAnswer.find_or_create_by!(title: 'Yes') + answer_no = TempAnswer.find_or_create_by!(title: 'No') + + # Find existing question or initialize it + q = TempQuestion.find_or_initialize_by(title: 'Will you attend the social event(s)?', + question_type_id: qtype.id, + global: true) + # Save question + q.save! + + # Associate answers with the question, unless they already exist + qa_yes = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_yes.id) + qa_no = TempQanswer.find_or_initialize_by(question_id: q.id, answer_id: answer_no.id) + + # Save question-answer associations + qa_yes.save! + qa_no.save! + + TempConference.all.each do |c| + # Make the question available for the conference + TempConferencesQuestions.find_or_create_by!(conference_id: c.id, question_id: q.id) + + TempRegistration.where(conference_id: c.id).each do |r| + if r.attending_social_events + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_yes.id) + else + TempQanswerRegistration.find_or_create_by!(registration_id: r.id, qanswer_id: qa_no.id) + end + end + end + remove_column :registrations, :attending_social_events, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 4cf7d7b4..39af7835 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -322,18 +322,14 @@ ActiveRecord::Schema.define(version: 20141109172204) do create_table "registrations", force: true do |t| t.integer "conference_id" - t.boolean "attending_social_events", default: true - t.boolean "attending_with_partner", default: false - t.boolean "using_affiliated_lodging", default: false t.datetime "arrival" t.datetime "departure" t.datetime "created_at" t.datetime "updated_at" t.integer "dietary_choice_id" t.text "other_dietary_choice" - t.boolean "handicapped_access_required", default: false t.text "other_special_needs" - t.boolean "attended", default: false + t.boolean "attended", default: false t.boolean "volunteer" t.integer "user_id" t.integer "week" diff --git a/db/seeds.rb b/db/seeds.rb index 166393cf..9cad18ec 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -21,9 +21,10 @@ 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?'] questions_yes_no.each do |i| q = Question.create(title: i, question_type_id: qtype_yesno.id, global: true)