diff --git a/app/models/answer.rb b/app/models/answer.rb index 836234f..e99036b 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -10,9 +10,8 @@ ## # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 - attr_accessible :text, :plan_id, :question_id, :user_id, :option_ids, - :question, :user, :plan, :question_options, - :as => [:default, :admin] + attr_accessible :text, :plan_id, :question_id, :user_id, :question_option_ids, + :question, :user, :plan, :question_options, :as => [:default, :admin] ## # Validations @@ -22,6 +21,8 @@ validates :question, uniqueness: {scope: [:plan], message: I18n.t('helpers.errors.answer.only_one_per_question')} - # TODO: We should validate that the text attribute is set or that an - # option was selected + # The answer MUST have a text value if the question is NOT option based or a question_option if + # it is option based. + validates :text, presence: true, if: Proc.new{|a| !a.question.question_format.option_based? } + validates :question_options, presence: true, if: Proc.new{|a| a.question.question_format.option_based? } end diff --git a/db/migrate/20131118094629_change_versions_published.rb b/db/migrate/20131118094629_change_versions_published.rb index 5e1035c..6f05c55 100644 --- a/db/migrate/20131118094629_change_versions_published.rb +++ b/db/migrate/20131118094629_change_versions_published.rb @@ -4,7 +4,7 @@ # Since we ultimately drop the Version model we must check for it before # attempting to manipulate data - if Object.const_defined?('Version') + if table_exists?('versions') Version.reset_column_information # make the new column available to model methods Version.all.each do |v| v.published_tmp = v.published == 't' ? true : false diff --git a/db/migrate/20150809210811_add_field_to_questions.rb b/db/migrate/20150809210811_add_field_to_questions.rb index ffbfdab..95ad8c6 100644 --- a/db/migrate/20150809210811_add_field_to_questions.rb +++ b/db/migrate/20150809210811_add_field_to_questions.rb @@ -2,7 +2,7 @@ def change add_column :questions, :option_comment_display, :boolean, :default => true - if Object.const_defined?('Question') + if table_exists?('questions') Question.find_each do |question| question.option_comment_display = true question.save! diff --git a/db/migrate/20151208142029_add_field_to_guidances.rb b/db/migrate/20151208142029_add_field_to_guidances.rb index 4acafe8..a6dbe1c 100644 --- a/db/migrate/20151208142029_add_field_to_guidances.rb +++ b/db/migrate/20151208142029_add_field_to_guidances.rb @@ -2,7 +2,7 @@ def change add_column :guidances, :published, :boolean - if Object.const_defined?('Guidance') + if table_exists?('guidances') Guidance.find_each do |guidance| guidance.published = true guidance.save! diff --git a/db/migrate/20151208142836_update_field_in_guidance_groups.rb b/db/migrate/20151208142836_update_field_in_guidance_groups.rb index 0a89167..2062073 100644 --- a/db/migrate/20151208142836_update_field_in_guidance_groups.rb +++ b/db/migrate/20151208142836_update_field_in_guidance_groups.rb @@ -1,6 +1,6 @@ class UpdateFieldInGuidanceGroups < ActiveRecord::Migration def change - if Object.const_defined?('GuidanceGroup') + if table_exists?('guidance_groups') GuidanceGroup.find_each do |guidance_group| guidance_group.published = true guidance_group.save! diff --git a/db/migrate/20161021100420_single_organisation_for_users.rb b/db/migrate/20161021100420_single_organisation_for_users.rb index a487721..20cceec 100644 --- a/db/migrate/20161021100420_single_organisation_for_users.rb +++ b/db/migrate/20161021100420_single_organisation_for_users.rb @@ -2,7 +2,7 @@ def up unless Rails.env.test? - if Object.const_defined?('User') + if table_exists?('users') User.class_eval do belongs_to :organisation, :class_name => "Organisation", diff --git a/db/migrate/20161122152339_new_plan_template_structure.rb b/db/migrate/20161122152339_new_plan_template_structure.rb index 70fcc73..2432dea 100644 --- a/db/migrate/20161122152339_new_plan_template_structure.rb +++ b/db/migrate/20161122152339_new_plan_template_structure.rb @@ -139,7 +139,8 @@ # migrate most current template into templates (org facing) proj_number = 0 - if Object.const_defined?('Project') && Object.const_defined?('Template') + if table_exists?('projects') && table_exists?('templates') && table_exists?('answers') && + table_exists?('comments') && table_exists?('sections') # migrating uncustomised plans Template.transaction do Project.includes( { dmptemplate: [ { phases: [ { versions: [:sections] } ] } ] }, {plans: [:version ]}, :organisation).find_each(batch_size: 20) do |project| @@ -309,26 +310,31 @@ def initTemplate(dmptemp, modifiable, organisation_id) - template = Template.new - template.title = dmptemp.title - template.description = dmptemp.description - template.published = dmptemp.published - template.organisation_id = organisation_id.present? ? organisation_id : dmptemp.organisation_id - template.locale = dmptemp.locale - template.is_default = dmptemp.is_default - template.created_at = dmptemp.created_at - template.updated_at = dmptemp.updated_at - template.visibility = 0 # dummy value for private - template.customization_of = modifiable ? nil : dmptemp.id - template.dmptemplate_id = dmptemp.id - # if no templates with the same dmptemplate_id and organisation_id exist - # 0 - # otherwise - # take the maximum version from templates with the same dmptemplate_id and organisation_id and add 1 - template.version = Template.where(dmptemplate_id: dmptemp.id, organisation_id: template.organisation_id).blank? ? - 0 : Template.where(dmptemplate_id: dmptemp.id, organisation_id: template.organisation_id).pluck(:version).max + 1 - puts "NEW TEMPLATE: \n title: #{template.title} \n version: #{template.version} \n others_present? #{Template.where(dmptemplate_id: dmptemp.id).count}" - return template + if table_exists?('templates') + template = Template.new + template.title = dmptemp.title + template.description = dmptemp.description + template.published = dmptemp.published + template.organisation_id = organisation_id.present? ? organisation_id : dmptemp.organisation_id + template.locale = dmptemp.locale + template.is_default = dmptemp.is_default + template.created_at = dmptemp.created_at + template.updated_at = dmptemp.updated_at + template.visibility = 0 # dummy value for private + template.customization_of = modifiable ? nil : dmptemp.id + template.dmptemplate_id = dmptemp.id + # if no templates with the same dmptemplate_id and organisation_id exist + # 0 + # otherwise + # take the maximum version from templates with the same dmptemplate_id and organisation_id and add 1 + template.version = Template.where(dmptemplate_id: dmptemp.id, organisation_id: template.organisation_id).blank? ? + 0 : Template.where(dmptemplate_id: dmptemp.id, organisation_id: template.organisation_id).pluck(:version).max + 1 + puts "NEW TEMPLATE: \n title: #{template.title} \n version: #{template.version} \n others_present? #{Template.where(dmptemplate_id: dmptemp.id).count}" + return template + + else + return nil + end end def initNewPhase(phase, version, temp, modifiable) @@ -363,8 +369,10 @@ new_question.text = question.text new_question.default_value = question.default_value new_question.guidance = question.guidance.nil? ? "" : question.guidance - Guidance.where(question_id: question.id).each do |guidance| - new_question.guidance += guidance.text + if table_exists?('guidances') + Guidance.where(question_id: question.id).each do |guidance| + new_question.guidance += guidance.text + end end new_question.number = question.number new_question.new_section_id = new_section.id @@ -387,35 +395,45 @@ new_answer.created_at = answer.created_at new_answer.updated_at = answer.updated_at # not sure if these get saved properly as new_answer has no id yet - answer.options.each do |option| - new_answer.question_options << QuestionOption.find_by(option_id: option.id) + if table_exists?('question_options') + answer.options.each do |option| + new_answer.question_options << QuestionOption.find_by(option_id: option.id) + end end end return new_answer end def initQuestionOption(option, new_question) - question_option = QuestionOption.new - question_option.new_question_id = new_question.id - question_option.option_id = option.id - question_option.text = option.text - question_option.number = option.number - question_option.is_default = option.is_default - question_option.created_at = option.created_at - question_option.updated_at = option.updated_at - return question_option + if table_exists?('question_options') + question_option = QuestionOption.new + question_option.new_question_id = new_question.id + question_option.option_id = option.id + question_option.text = option.text + question_option.number = option.number + question_option.is_default = option.is_default + question_option.created_at = option.created_at + question_option.updated_at = option.updated_at + return question_option + else + return nil + end end def initNote(comment, new_answer) - note = Note.new - note.user_id = comment.user_id - note.text = comment.text - note.archived = comment.archived - note.archived_by = comment.archived_by - note.new_answer_id = new_answer.id - note.created_at = comment.created_at - note.updated_at = comment.updated_at - return note + if table_exists?('notes') + note = Note.new + note.user_id = comment.user_id + note.text = comment.text + note.archived = comment.archived + note.archived_by = comment.archived_by + note.new_answer_id = new_answer.id + note.created_at = comment.created_at + note.updated_at = comment.updated_at + return note + else + return nil + end end def initNewPlan(project) @@ -447,13 +465,17 @@ end def initRole(project_group, new_plan) - role = Role.new - role.creator = project_group.project_creator - role.administrator = project_group.project_administrator - role.editor = project_group.project_editor - role.created_at = project_group.created_at - role.updated_at = project_group.updated_at - role.user_id = project_group.user_id - role.new_plan_id = new_plan.id - return role + if table_exists?('roles') + role = Role.new + role.creator = project_group.project_creator + role.administrator = project_group.project_administrator + role.editor = project_group.project_editor + role.created_at = project_group.created_at + role.updated_at = project_group.updated_at + role.user_id = project_group.user_id + role.new_plan_id = new_plan.id + return role + else + return nil + end end diff --git a/db/migrate/20161205095624_replacing_organisation_types_with_bitflags.rb b/db/migrate/20161205095624_replacing_organisation_types_with_bitflags.rb index d1d3fcf..0ebed56 100644 --- a/db/migrate/20161205095624_replacing_organisation_types_with_bitflags.rb +++ b/db/migrate/20161205095624_replacing_organisation_types_with_bitflags.rb @@ -13,7 +13,7 @@ # t.boolean :Project end - if Object.const_defined?('Org') + if table_exists?('orgs') # migrate old org_type data to bitfield Org.includes(:organisation_type).all.each do |org| unless org.organisation_type.nil? diff --git a/db/migrate/20161205095625_replacing_plan_roles_with_bitflags.rb b/db/migrate/20161205095625_replacing_plan_roles_with_bitflags.rb index b7b35fa..859586c 100644 --- a/db/migrate/20161205095625_replacing_plan_roles_with_bitflags.rb +++ b/db/migrate/20161205095625_replacing_plan_roles_with_bitflags.rb @@ -8,7 +8,7 @@ rename_column :roles, :administrator, :admin # transfer the data from the other fields to the bitfield - if Object.const_defined?('Role') + if table_exists?('roles') Role.find_each do |role| if role.admin role.administrator = true diff --git a/db/migrate/20161206122926_add_foreign_keys.rb b/db/migrate/20161206122926_add_foreign_keys.rb index 5b6f7a3..7574279 100644 --- a/db/migrate/20161206122926_add_foreign_keys.rb +++ b/db/migrate/20161206122926_add_foreign_keys.rb @@ -83,7 +83,7 @@ def scrub_references # answers i = 0 - if Object.const_defined?('Answer') + if table_exists?('answers') Answer.includes(:user, :plan, :question).find_each do |ans| if ans.user.nil? && ans.user_id.present? ans.user_id = nil @@ -104,7 +104,7 @@ # notes i = 0 - if Object.const_defined?('Note') + if table_exists?('notes') Note.includes(:answer, :user).find_each do |note| if note.answer.nil? && note.answer_id.present? note.answer_id = nil @@ -121,7 +121,7 @@ # templates i = 0 - if Object.const_defined?('Template') + if table_exists?('templates') Template.includes(:org).find_each do |temp| if temp.org.nil? && temp.org_id.present? temp.org_id = nil @@ -134,7 +134,7 @@ # guidance_groups # i = 0 - # if Object.const_defined?('GuidanceGroup') + # if table_exists?('guidance_groups') # GuidanceGroup.includes(:org).find_each do |gg| # if gg.org.nil? && gg.org_id.present? # gg.org_id = nil @@ -147,7 +147,7 @@ # # question_options # i = 0 - # if Object.const_defined?('QuestionOption') + # if table_exists?('question_options') # QuestionOption.includes(:question).find_each do |opt| # if opt.question.nil? && opt.question_id.present? # opt.question_id = nil @@ -160,7 +160,7 @@ # # orgs # i = 0 - # if Object.const_defined?('Org') + # if table_exists?('orgs') # Org.includes( :language).find_each do |org| # if org.language.nil? && org.language_id.present? # org.language_id = nil @@ -179,7 +179,7 @@ # roles i = 0 - if Object.const_defined?('Role') + if table_exists?('roles') Role.includes(:user, :plan).find_each do |role| if role.user.nil? && role.user_id.present? Role.delete_all(user_id: role.user_id) @@ -200,7 +200,7 @@ # # suggested_answers # i = 0 - # if Object.const_defined?('SuggestedAnswer') + # if table_exists?('suggested_answers') # SuggestedAnswer.includes(:org, :question).find_each do |sa| # if sa.org.nil? && sa.org_id.present? # sa.org_id = nil @@ -220,7 +220,7 @@ # # users # i = 0 - # if Object.const_defined?('User') + # if table_exists?('users') # User.includes(:org, :language).find_each do |u| # if u.org.nil? && u.org_id.present? # u.org_id = nil @@ -236,7 +236,7 @@ # puts "#{i} users scrubbed" # users_perms - if Object.const_defined?('UsersPerm') + if table_exists?('users_perms') UsersPerm.includes(:user).all.each do |u| if u.user.nil? UsersPerm.delete_all(user_id: u.user_id) diff --git a/db/migrate/20161208122123_single_group_for_guidance.rb b/db/migrate/20161208122123_single_group_for_guidance.rb index 0893cdd..562673c 100644 --- a/db/migrate/20161208122123_single_group_for_guidance.rb +++ b/db/migrate/20161208122123_single_group_for_guidance.rb @@ -1,11 +1,11 @@ class SingleGroupForGuidance < ActiveRecord::Migration def change unless Rails.env.test? - Guidance.class_eval do - belongs_to :guidance_group, class_name: "GuidanceGroup", foreign_key: "guidance_group_id" - end + if table_exists?('guidances') + Guidance.class_eval do + belongs_to :guidance_group, class_name: "GuidanceGroup", foreign_key: "guidance_group_id" + end - if Object.const_defined?('Guidance') Guidance.includes( :guidance_groups).all.each do |guidance| guidance.guidance_group_id = guidance.guidance_groups.first.id unless guidance.guidance_groups.empty? if guidance.guidance_group_id.nil? diff --git a/db/migrate/20170130173049_add_option_based_to_question_formats.rb b/db/migrate/20170130173049_add_option_based_to_question_formats.rb new file mode 100644 index 0000000..936fa3e --- /dev/null +++ b/db/migrate/20170130173049_add_option_based_to_question_formats.rb @@ -0,0 +1,15 @@ +class AddOptionBasedToQuestionFormats < ActiveRecord::Migration + def change + add_column :question_formats, :option_based, :boolean, default: false + + # Set the new field to true for the question formats that have options + if table_exists?('question_formats') + QuestionFormat.all.each do |qf| + unless ['text area', 'text field', 'date'].include?(qf.title.downcase) + qf.option_based = true + qf.save! + end + end + end + end +end diff --git a/db/migrate/20170130173612_move_orcid_id_from_users_to_user_identifiers.rb b/db/migrate/20170130173612_move_orcid_id_from_users_to_user_identifiers.rb new file mode 100644 index 0000000..c20c37f --- /dev/null +++ b/db/migrate/20170130173612_move_orcid_id_from_users_to_user_identifiers.rb @@ -0,0 +1,20 @@ +class MoveOrcidIdFromUsersToUserIdentifiers < ActiveRecord::Migration + def change + + if table_exists?('users') && table_exists?('identifier_schemes') + scheme = IdentifierScheme.find_by(name: 'orcid') + + unless scheme.nil? + User.all.each do |u| + unless u.orcid_id.nil? + u.user_identifiers << UserIdentifier.new(identifier_scheme: scheme, identifier: u.orcid_id) + u.save! + end + end + + remove_column :users, :orcid_id + end + end + + end +end diff --git a/db/schema.rb b/db/schema.rb index 15a41ea..b749bfe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170124235829) do +ActiveRecord::Schema.define(version: 20170130173612) do create_table "answers", force: :cascade do |t| t.text "text", limit: 65535 @@ -31,8 +31,8 @@ t.integer "question_option_id", limit: 4, null: false end - add_index "answers_question_options", ["answer_id", "question_option_id"], name: "answer_question_option_index" - add_index "answers_question_options", ["question_option_id", "answer_id"], name: "question_option_answer_index" + add_index "answers_question_options", ["answer_id", "question_option_id"], name: "answer_question_option_index", using: :btree + add_index "answers_question_options", ["question_option_id", "answer_id"], name: "question_option_answer_index", using: :btree create_table "exported_plans", force: :cascade do |t| t.integer "plan_id", limit: 4 @@ -70,9 +70,9 @@ t.datetime "created_at" end - add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", unique: true - add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id" - add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type" + add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", unique: true, using: :btree + add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id", using: :btree + add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree create_table "guidance_groups", force: :cascade do |t| t.string "name", limit: 255 @@ -105,9 +105,9 @@ end create_table "languages", force: :cascade do |t| - t.string "abbreviation" - t.string "description" - t.string "name" + t.string "abbreviation", limit: 255 + t.string "description", limit: 255 + t.string "name", limit: 255 t.boolean "default_language" end @@ -163,8 +163,8 @@ t.datetime "updated_at" end - add_index "perms", ["name"], name: "index_perms_on_name" - add_index "perms", ["name"], name: "index_roles_on_name_and_resource_type_and_resource_id" + add_index "perms", ["name"], name: "index_perms_on_name", using: :btree + add_index "perms", ["name"], name: "index_roles_on_name_and_resource_type_and_resource_id", using: :btree create_table "phases", force: :cascade do |t| t.string "title", limit: 255 @@ -199,10 +199,11 @@ add_index "plans", ["template_id"], name: "fk_rails_3424ca281f", using: :btree create_table "question_formats", force: :cascade do |t| - t.string "title", limit: 255 - t.text "description", limit: 65535 + t.string "title", limit: 255 + t.text "description", limit: 65535 t.datetime "created_at" t.datetime "updated_at" + t.boolean "option_based", default: false end create_table "question_options", force: :cascade do |t| @@ -237,18 +238,18 @@ t.integer "theme_id", limit: 4, null: false end - add_index "questions_themes", ["question_id", "theme_id"], name: "question_theme_index" - add_index "questions_themes", ["theme_id", "question_id"], name: "theme_question_index" + add_index "questions_themes", ["question_id", "theme_id"], name: "question_theme_index", using: :btree + add_index "questions_themes", ["theme_id", "question_id"], name: "theme_question_index", using: :btree create_table "region_groups", force: :cascade do |t| - t.integer "super_region_id" - t.integer "region_id" + t.integer "super_region_id", limit: 4 + t.integer "region_id", limit: 4 end create_table "regions", force: :cascade do |t| - t.string "abbreviation" - t.string "description" - t.string "name" + t.string "abbreviation", limit: 255 + t.string "description", limit: 255 + t.string "name", limit: 255 end create_table "roles", force: :cascade do |t| @@ -284,7 +285,7 @@ t.datetime "updated_at" end - add_index "settings", ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true + add_index "settings", ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true, using: :btree create_table "splash_logs", force: :cascade do |t| t.string "destination", limit: 255 @@ -338,8 +339,8 @@ add_index "themes_in_guidance", ["theme_id"], name: "fk_rails_7d708f6f1e", using: :btree create_table "token_permission_types", force: :cascade do |t| - t.string "token_type" - t.text "text_description" + t.string "token_type", limit: 255 + t.text "text_description", limit: 65535 t.datetime "created_at" t.datetime "updated_at" end @@ -359,7 +360,6 @@ t.string "firstname", limit: 255 t.string "surname", limit: 255 t.string "email", limit: 255, default: "", null: false - t.string "orcid_id", limit: 255 t.string "shibboleth_id", limit: 255 t.datetime "created_at" t.datetime "updated_at" @@ -370,12 +370,12 @@ t.integer "sign_in_count", limit: 4, default: 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" - t.string "current_sign_in_ip" - t.string "last_sign_in_ip" - t.string "confirmation_token" + t.string "current_sign_in_ip", limit: 255 + t.string "last_sign_in_ip", limit: 255 + t.string "confirmation_token", limit: 255 t.datetime "confirmed_at" t.datetime "confirmation_sent_at" - t.string "invitation_token" + t.string "invitation_token", limit: 255 t.datetime "invitation_created_at" t.datetime "invitation_sent_at" t.datetime "invitation_accepted_at" diff --git a/db/seeds.rb b/db/seeds.rb index da9896b..2467322 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -14,13 +14,13 @@ # Question Formats # ------------------------------------------------------- question_formats = [ - {title: "Text area"}, - {title: "Text field"}, - {title: "Radio buttons"}, - {title: "Check box"}, - {title: "Dropdown"}, - {title: "Multi select box"}, - {title: "Date"} + {title: "Text area", option_based: false}, + {title: "Text field", option_based: false}, + {title: "Radio buttons", option_based: true}, + {title: "Check box", option_based: true}, + {title: "Dropdown", option_based: true}, + {title: "Multi select box", option_based: true}, + {title: "Date", option_based: false} ] question_formats.map{ |qf| QuestionFormat.create!(qf) if QuestionFormat.find_by(title: qf[:title]).nil? } diff --git a/test/unit/answer_test.rb b/test/unit/answer_test.rb index c8161e1..92dc082 100644 --- a/test/unit/answer_test.rb +++ b/test/unit/answer_test.rb @@ -5,7 +5,10 @@ setup do @user = User.last - @plan = Plan.first + @text_based_questions = QuestionFormat.joins(:questions).joins(:question_options)#.where("question_options.id IS NULL") + #@option_based_questions = QuestionFormat.joins(:questions).includes(:question_options).where("question_options.id IS NOT NULL") + + puts @text_based_questions.inspect @text_area_question = QuestionFormat.find_by(title: 'Text Area').questions.first @text_field_question = QuestionFormat.find_by(title: 'Text Field').questions.first @@ -14,20 +17,35 @@ @select_box_question = QuestionFormat.find_by(title: 'Dropdown').questions.first @multi_select_box_question = QuestionFormat.find_by(title: 'Multi Select Box').questions.first @date_question = QuestionFormat.find_by(title: 'Date').questions.first - - @unanswered_question = Question() end # --------------------------------------------------- test "required fields are required" do assert_not Answer.new.valid? - assert_not Answer.new(user: @user, question: @text_area_question).valid?, "expected the 'plan' field to be required" - assert_not Answer.new(plan: @plan, question: @text_area_question).valid?, "expected the 'user' field to be required" - assert_not Answer.new(user: @user, plan: @plan).valid?, "expected the 'question' field to be required" + + # Validate the creation of text based answers + [].each do |q| + assert_not Answer.new(user: @user, question: q, text: 'Testing').valid?, "expected the 'plan' field to be required for a #{q.question_format.class.name}" + assert_not Answer.new(plan: @plan, question: q, text: 'Testing').valid?, "expected the 'user' field to be required for a #{q.question_format.class.name}" + assert_not Answer.new(user: @user, plan: @plan, text: 'Testing').valid?, "expected the 'question' field to be required for a #{q.question_format.class.name}" + assert_not Answer.new(user: @user, question: q, plan: @plan).valid?, "expected the 'text' field to be required for a #{q.question_format.class.name}" + + # Ensure the bar minimum and complete versions are valid + a = Answer.new(user: @user, plan: @plan, question: q, text: 'Testing') + assert a.valid?, "expected the 'plan', 'user' and 'question' and 'text' fields to be enough to create an Answer for a #{q.question_format.class.name}! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # Validate the creation of option based answers (a selection is not required) + [@radio_button_question, @check_box_question, @select_box_question, @multi_select_box_question].each do |q| + assert_not Answer.new(user: @user, question: q).valid?, "expected the 'plan' field to be required for a #{q.question_format.class.name}" + assert_not Answer.new(plan: @plan, question: q).valid?, "expected the 'user' field to be required for a #{q.question_format.class.name}" + assert_not Answer.new(user: @user, plan: @plan).valid?, "expected the 'question' field to be required for a #{q.question_format.class.name}" + + # Ensure the bar minimum and complete versions are valid + a = Answer.new(user: @user, plan: @plan, question: q) + assert a.valid?, "expected the 'plan', 'user' and 'question' fields to be enough to create an Answer for a #{q.question_format.class.name}! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end - # Ensure the bar minimum and complete versions are valid - a = Answer.new(user: @user, plan: @plan, question: @text_area_question) - assert a.valid?, "expected the 'plan', 'user' and 'question' fields to be enough to create an Answer! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end # ---------------------------------------------------