diff --git a/app/models/answer.rb b/app/models/answer.rb index a989c8f..73e07e9 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -6,6 +6,13 @@ belongs_to :plan has_and_belongs_to_many :options, join_table: "answers_options" - - attr_accessible :text, :plan_id, :question_id, :user_id, :option_ids , :as => [:default, :admin] + + # TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! + attr_accessible :text, :plan_id, :question_id, :user_id, :option_ids, :plan, :user, :question, + :as => [:default, :admin] + + validates :user, :plan, :question, :text, presence: true + + # Make sure there is only one answer per question! + validates :question, uniqueness: {scope: [:user, :plan], message: I18n.t('helpers.errors.answer.only_one_per_question')} end diff --git a/test/unit/answer_test.rb b/test/unit/answer_test.rb index 04cf87a..6e05a24 100644 --- a/test/unit/answer_test.rb +++ b/test/unit/answer_test.rb @@ -15,6 +15,8 @@ project.dmptemplate = template project.save! + @plan = project.plans.first + qs = template.phases.first.versions.first.sections.first.questions @text_area_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Area' ) }.first @text_field_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Field' ) }.first @@ -24,35 +26,30 @@ @multi_select_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Multi Select Box' ) }.first end -=begin # --------------------------------------------------- test "required fields are required" do assert_not Answer.new.valid? - assert_not Answer.new(user: @user).valid? - assert_not Answer.new(plan: @plan).valid? - assert_not Answer.new(question: @text_area_question).valid? - assert_not Answer.new(user: @user, plan: @plan).valid? - assert_not Answer.new(user: @user, question: @text_area_question).valid? - assert_not Answer.new(plan: @plan, question: @text_area_question).valid? + assert_not Answer.new(user: @user, question: @text_area_question).valid?, "expected the 'text' field to be required" + assert_not Answer.new(plan: @plan, question: @text_area_question, text: 'Testing').valid?, "expected the 'user' field to be required" + assert_not Answer.new(user: @user, question: @text_area_question, text: 'Testing').valid?, "expected the 'plan' field to be required" + assert_not Answer.new(user: @user, plan: @plan, text: 'Testing').valid?, "expected the 'question' field to be required" # Ensure the bar minimum and complete versions are valid - assert Answer.new(user: @user, plan: @plan, question: @text_area_question).valid? + a = Answer.new(user: @user, plan: @plan, question: @text_area_question, text: 'Testing') + assert a.valid?, "expected the 'text', 'plan', 'user' and 'question' fields to be enough to create an Answer! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end # --------------------------------------------------- test "cannot have multiple answers to the same question within a plan" do Answer.create(user: @user, plan: @plan, question: @text_area_question, text: 'Tested ABC') - assert_not Answer.new(user: @user, plan: @plan, question: @text_area_question).valid? - -puts @plan.answers.inspect - - assert_not Answer.new(user: @user, plan: @plan, question: @text_area_question, text: 'ABCD').valid? + + assert_not Answer.new(user: @user, plan: @plan, question: @text_area_question, text: 'Another answer to the same question!').valid?, "expected to NOT be able to add an answer to a question that already has an answer!" end # --------------------------------------------------- test "can CRUD answers for text based questions" do [@text_area_question, @text_field_question].each do |q| - answr = Answer.new(user: @user, plan: @plan, question: q, text: 'Tested ABC') + answr = Answer.create(user: @user, plan: @plan, question: q, text: 'Tested ABC') assert_not answr.id.nil?, "was expecting to be able to create a new Answer for a #{q.question_format.title} question: #{answr.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" answr.text = 'Testing an update' @@ -63,6 +60,5 @@ assert answr.destroy!, "Was unable to delete the Answer for a #{q.question_format.title} question!" end end -=end end