Migrate registration attributes to questions

This commit is contained in:
Stella 2014-11-09 18:25:36 +02:00
parent 08a47339a4
commit 59f2b636c5
9 changed files with 401 additions and 15 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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