- <%= link_to( image_tag("help_button.png"), "#", class: "suggested_answer_popover", rel: "popover", "data-html" => "true", "data-content" => _('You can add an example or suggested answer to help users respond. These will be presented above the answer box and can be copied/ pasted.'))%>
+ <%= link_to( image_tag("help_button.png"), "#", class: "suggested_answer_popover", rel: "popover", "data-html" => "true", "data-content" => _('You can add an example answer to help users respond. These will be presented above the answer box and can be copied/ pasted.'))%>
<%= link_to( image_tag("help_button.png"), "#", class: "question_guidance_popover", rel: "popover", "data-html" => "true", "data-content" => _("Enter specific guidance to accompany this question. If you have guidance by themes too, this will be pulled in based on your selections below so it's best not to duplicate too much text."))%>
diff --git a/app/views/questions/_preview_question.html.erb b/app/views/questions/_preview_question.html.erb
index dc51641..e4a86a8 100644
--- a/app/views/questions/_preview_question.html.erb
+++ b/app/views/questions/_preview_question.html.erb
@@ -57,19 +57,15 @@
<% end %>
- <% suggested_answer = question.suggested_answers.find_by(org_id: current_user.org_id) %>
- <% if !suggested_answer.blank? %>
+ <% if question.annotations.where('type <> ?',Annotation.types[:example_answer]).any? %>
+ <% annotation = question.annotations.where('type <> ?',Annotation.types[:example_answer]).order(:created_at).first %>
- <% if suggested_answer.is_example? %>
<%= _('Example of answer')%>
- <% else %>
- <%= _('Suggested answer')%>
- <% end %>
- <%= raw suggested_answer.text %>
+ <%= raw annotation.text %>
<% end %>
@@ -129,12 +126,12 @@
confirm: _("You are about to delete '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%>
diff --git a/config/routes.rb b/config/routes.rb
index c09d22f..27c1d67 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -12,7 +12,7 @@
resources :questions
resources :question_formats
resources :question_options
- resources :suggested_answers
+ resources :annotations
resources :answers
resources :guidances
resources :guidance_groups
@@ -161,7 +161,7 @@
end
end
- resources :suggested_answers, path: 'org/admin/templates/suggested_answers', only: [] do
+ resources :annotations, path: 'org/admin/templates/annotations', only: [] do
member do
post 'admin_create'
put 'admin_update'
diff --git a/db/schema.rb b/db/schema.rb
index fe00e7b..f0bfbd2 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -16,6 +16,15 @@
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
+ create_table "annotations", force: :cascade do |t|
+ t.integer "question_id"
+ t.integer "org_id"
+ t.text "text"
+ t.integer "type", default: 0, null: false
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
create_table "answers", force: :cascade do |t|
t.text "text"
t.integer "plan_id"
@@ -271,15 +280,6 @@
t.datetime "updated_at", null: false
end
- create_table "suggested_answers", force: :cascade do |t|
- t.integer "question_id"
- t.integer "org_id"
- t.text "text"
- t.boolean "is_example"
- t.datetime "created_at"
- t.datetime "updated_at"
- end
-
create_table "templates", force: :cascade do |t|
t.string "title"
t.text "description"
@@ -330,8 +330,8 @@
t.string "email", default: "", null: false
t.string "orcid_id"
t.string "shibboleth_id"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
+ t.datetime "created_at"
+ t.datetime "updated_at"
t.string "encrypted_password", default: ""
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
@@ -370,6 +370,8 @@
add_index "users_perms", ["user_id", "perm_id"], name: "index_users_perms_on_user_id_and_perm_id", using: :btree
+ add_foreign_key "annotations", "orgs"
+ add_foreign_key "annotations", "questions"
add_foreign_key "answers", "plans"
add_foreign_key "answers", "questions"
add_foreign_key "answers", "users"
@@ -395,8 +397,6 @@
add_foreign_key "roles", "plans"
add_foreign_key "roles", "users"
add_foreign_key "sections", "phases"
- add_foreign_key "suggested_answers", "orgs"
- add_foreign_key "suggested_answers", "questions"
add_foreign_key "templates", "orgs"
add_foreign_key "themes_in_guidance", "guidances"
add_foreign_key "themes_in_guidance", "themes"
diff --git a/db/seeds.rb b/db/seeds.rb
index 9277a5a..7da10a5 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -671,17 +671,17 @@
# Create suggested answers for a few questions
# -------------------------------------------------------
-suggested_answers = [
+annotations = [
{text: "We will preserve it in Dryad or a similar data repository service.",
- is_example: true,
+ type: Annotation.types[:example_answer],
org: Org.find_by(abbreviation: 'GA'),
question: Question.find_by(text: "What is your policy for long term access to your dataset?")},
{text: "We recommend that you identify the type(s) of content as well as the type of file(s) involved",
- is_example: false,
+ type: Annotation.types[:example_answer],
org: Org.find_by(abbreviation: 'GA'),
question: Question.find_by(text: "What types of data will you collect and how will it be stored?")},
]
-suggested_answers.map{ |s| SuggestedAnswer.create!(s) if SuggestedAnswer.find_by(text: s[:text]).nil? }
+annotations.map{ |s| Annotation.create!(s) if Annotation.find_by(text: s[:text]).nil? }
# Create options for the dropdown, multi-select and radio buttons
# -------------------------------------------------------
diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake
index cc9e87e..0bd54fd 100644
--- a/lib/tasks/migrate.rake
+++ b/lib/tasks/migrate.rake
@@ -248,5 +248,22 @@
end
end
end
+
+ desc "enforce single published version for templates"
+ task single_published_template: :environment do
+ # for each group of versions of a template
+ Template.all.pluck(:dmptemplate_id).uniq.each do |dmptemplate_id|
+ published = false
+ Template.where(dmptemplate_id: dmptemplate_id).order(version: :desc).each do |template|
+ # leave the first published template we find alone
+ if !published && template.published
+ published = true
+ elsif published && template.published
+ template.published = false
+ template.save!
+ end
+ end
+ end
+ end
end
diff --git a/test/functional/annotations_controller_test.rb b/test/functional/annotations_controller_test.rb
new file mode 100644
index 0000000..8615d16
--- /dev/null
+++ b/test/functional/annotations_controller_test.rb
@@ -0,0 +1,116 @@
+require 'test_helper'
+
+class AnnotationsControllerTest < ActionDispatch::IntegrationTest
+
+ include Devise::Test::IntegrationHelpers
+
+ setup do
+ @question = Annotation.first.question
+
+ # Get the first Org Admin
+ scaffold_org_admin(@question.section.phase.template.org)
+ end
+
+# TODO: The following methods SHOULD replace the old 'admin_' prefixed methods. The routes file already has
+# these defined. They are defined multiple times though and we need to clean this up! In particular
+# look at the unnamed routes after 'new_plan_phase' below. They are not named because they are duplicates.
+# We should just have:
+#
+# SHOULD BE:
+# --------------------------------------------------
+# suggested_answers GET /templates/:template_id/phases/:phase_id/sections/:section_id/questions/:id sections#index
+# POST /templates/:template_id/phases/:phase_id/sections/:section_id/questions/:id sections#create
+# suggested_answer GET /templates/:template_id/phases/:phase_id/sections/:section_id/questions/:question_id/suggested_answer/:id sections#show
+# PATCH /templates/:template_id/phases/:phase_id/section/:section_id/questions/:question_id/suggested_answer/:id sections#update
+# PUT /templates/:template_id/phases/:phase_id/section/:section_id/questions/:question_id/suggested_answer/:id sections#update
+# DELETE /templates/:template_id/phases/:phase_id/section/:section_id/questions/:question_id/suggested_answer/:id sections#destroy
+#
+# CURRENT RESULTS OF `rake routes`
+# --------------------------------------------------
+# admin_create_suggested_answer POST /org/admin/templates/suggested_answers/:id/admin_create suggested_answers#admin_create
+# admin_update_suggested_answer PUT /org/admin/templates/suggested_answers/:id/admin_update suggested_answers#admin_update
+# admin_destroy_suggested_answer DELETE /org/admin/templates/suggested_answers/:id/admin_destroy suggested_answers#admin_destroy
+
+
+
+ # POST /org/admin/templates/suggested_answers/:id/admin_create (admin_create_annotation_path)
+ # ----------------------------------------------------------
+ test "create a new annotation" do
+ params = {org_id: @user.org.id, question_id: @question.id, text: "Here's a suggestion"}
+
+ # Should redirect user to the root path if they are not logged in!
+ post admin_create_annotation_path(@question.id), {annotation: params}
+ assert_unauthorized_redirect_to_root_path
+
+ sign_in @user
+
+ post admin_create_annotation_path(@question.id), {annotation: params}
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}?edit=true&question_id=#{@question.id}§ion_id=#{@question.section.id}"
+ assert_equal _('Information was successfully created.'), flash[:notice]
+ assert_equal "Here's a suggestion", Annotation.last.text, "expected the record to have been created!"
+ assert assigns(:example_answer)
+
+ # Invalid object
+ post admin_create_annotation_path(@question.id), {annotation: {question_id: @question.id}}
+ assert flash[:notice].starts_with?(_('Could not create your'))
+ assert_response :success
+ assert assigns(:example_answer)
+ end
+
+ # PUT /org/admin/templates/suggested_answers/:id/admin_update (admin_update_suggested_answer_path)
+ # ----------------------------------------------------------
+ test "update the annotation" do
+ params = {text: 'UPDATE'}
+
+ # Should redirect user to the root path if they are not logged in!
+ put admin_update_annotation_path(Annotation.first), {annotation: params}
+ assert_unauthorized_redirect_to_root_path
+
+ sign_in @user
+
+ # Valid save
+ put admin_update_annotation_path(Annotation.first), {annotation: params}
+ assert_equal _('Information was successfully updated.'), flash[:notice]
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}?edit=true&question_id=#{@question.id}§ion_id=#{@question.section.id}"
+ assert assigns(:example_answer)
+ assert assigns(:question)
+ assert assigns(:section)
+ assert assigns(:phase)
+ assert_equal 'UPDATE', Annotation.first.text, "expected the record to have been updated"
+
+# TODO: We need to add in validation checks on the model and reactivate this test
+ # Invalid save
+# put admin_update_suggested_answer_path(SuggestedAnswer.first), {suggested_answer: {text: nil}}
+# assert flash[:notice].starts_with?(_('Could not update your'))
+# assert_response :success
+# assert assigns(:suggested_answer)
+# assert assigns(:question)
+# assert assigns(:section)
+# assert assigns(:phase)
+ end
+
+ # DELETE /org/admin/templates/suggested_answers/:id/admin_destroy (admin_destroy_suggested_answer_path)
+ # ----------------------------------------------------------
+ test "delete the section" do
+ id = Annotation.first.id
+ # Should redirect user to the root path if they are not logged in!
+ delete admin_destroy_annotation_path(id: id)
+ assert_unauthorized_redirect_to_root_path
+
+ sign_in @user
+
+ delete admin_destroy_annotation_path(id: id)
+ assert_equal _('Information was successfully deleted.'), flash[:notice]
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}?edit=true§ion_id=#{@question.section.id}"
+ assert assigns(:question)
+ assert assigns(:section)
+ assert assigns(:phase)
+ assert_raise ActiveRecord::RecordNotFound do
+ Annotation.find(id).nil?
+ end
+ end
+
+end
\ No newline at end of file
diff --git a/test/functional/questions_controller_test.rb b/test/functional/questions_controller_test.rb
index 2a9cbcf..9a156f5 100644
--- a/test/functional/questions_controller_test.rb
+++ b/test/functional/questions_controller_test.rb
@@ -38,7 +38,7 @@
# POST /org/admin/templates/questions/:id/admin_create (admin_create_question_path)
# ----------------------------------------------------------
test "create a new question" do
- params = {section_id: @section.id, text: 'Test Question', number: 9, question_format_id: @question_format.id}
+ params = {section_id: @section.id, text: 'Test Question', number: 9, question_format_id: @question_format.id, annotations_attributes: {0 => {text: "some text", org_id: Org.first.id}}}
@section.phase.template.dirty = false
@section.phase.template.save!
@@ -48,7 +48,11 @@
assert_unauthorized_redirect_to_root_path
sign_in @user
-
+ @new_question = Question.new
+ @new_question.number = @section.questions.count + 1
+ example_answer = @new_question.annotations.build
+ example_answer.type = :example_answer
+ example_answer.save
post admin_create_question_path(@section), {question: params}
assert_response :redirect
assert assigns(:question)
diff --git a/test/functional/suggested_answers_controller_test.rb b/test/functional/suggested_answers_controller_test.rb
deleted file mode 100644
index dcdd2ce..0000000
--- a/test/functional/suggested_answers_controller_test.rb
+++ /dev/null
@@ -1,116 +0,0 @@
-require 'test_helper'
-
-class SuggestedAnswersControllerTest < ActionDispatch::IntegrationTest
-
- include Devise::Test::IntegrationHelpers
-
- setup do
- @question = SuggestedAnswer.first.question
-
- # Get the first Org Admin
- scaffold_org_admin(@question.section.phase.template.org)
- end
-
-# TODO: The following methods SHOULD replace the old 'admin_' prefixed methods. The routes file already has
-# these defined. They are defined multiple times though and we need to clean this up! In particular
-# look at the unnamed routes after 'new_plan_phase' below. They are not named because they are duplicates.
-# We should just have:
-#
-# SHOULD BE:
-# --------------------------------------------------
-# suggested_answers GET /templates/:template_id/phases/:phase_id/sections/:section_id/questions/:id sections#index
-# POST /templates/:template_id/phases/:phase_id/sections/:section_id/questions/:id sections#create
-# suggested_answer GET /templates/:template_id/phases/:phase_id/sections/:section_id/questions/:question_id/suggested_answer/:id sections#show
-# PATCH /templates/:template_id/phases/:phase_id/section/:section_id/questions/:question_id/suggested_answer/:id sections#update
-# PUT /templates/:template_id/phases/:phase_id/section/:section_id/questions/:question_id/suggested_answer/:id sections#update
-# DELETE /templates/:template_id/phases/:phase_id/section/:section_id/questions/:question_id/suggested_answer/:id sections#destroy
-#
-# CURRENT RESULTS OF `rake routes`
-# --------------------------------------------------
-# admin_create_suggested_answer POST /org/admin/templates/suggested_answers/:id/admin_create suggested_answers#admin_create
-# admin_update_suggested_answer PUT /org/admin/templates/suggested_answers/:id/admin_update suggested_answers#admin_update
-# admin_destroy_suggested_answer DELETE /org/admin/templates/suggested_answers/:id/admin_destroy suggested_answers#admin_destroy
-
-
-
- # POST /org/admin/templates/suggested_answers/:id/admin_create (admin_create_suggested_answer_path)
- # ----------------------------------------------------------
- test "create a new section" do
- params = {org_id: @user.org.id, question_id: @question.id, text: "Here's a suggestion"}
-
- # Should redirect user to the root path if they are not logged in!
- post admin_create_suggested_answer_path(@question.id), {suggested_answer: params}
- assert_unauthorized_redirect_to_root_path
-
- sign_in @user
-
- post admin_create_suggested_answer_path(@question.id), {suggested_answer: params}
- assert_response :redirect
- assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}?edit=true&question_id=#{@question.id}§ion_id=#{@question.section.id}"
- assert_equal _('Information was successfully created.'), flash[:notice]
- assert_equal "Here's a suggestion", SuggestedAnswer.last.text, "expected the record to have been created!"
- assert assigns(:suggested_answer)
-
- # Invalid object
- post admin_create_suggested_answer_path(@question.id), {suggested_answer: {question_id: @question.id}}
- assert flash[:notice].starts_with?(_('Could not create your'))
- assert_response :success
- assert assigns(:suggested_answer)
- end
-
- # PUT /org/admin/templates/suggested_answers/:id/admin_update (admin_update_suggested_answer_path)
- # ----------------------------------------------------------
- test "update the section" do
- params = {text: 'UPDATE'}
-
- # Should redirect user to the root path if they are not logged in!
- put admin_update_suggested_answer_path(SuggestedAnswer.first), {suggested_answer: params}
- assert_unauthorized_redirect_to_root_path
-
- sign_in @user
-
- # Valid save
- put admin_update_suggested_answer_path(SuggestedAnswer.first), {suggested_answer: params}
- assert_equal _('Information was successfully updated.'), flash[:notice]
- assert_response :redirect
- assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}?edit=true&question_id=#{@question.id}§ion_id=#{@question.section.id}"
- assert assigns(:suggested_answer)
- assert assigns(:question)
- assert assigns(:section)
- assert assigns(:phase)
- assert_equal 'UPDATE', SuggestedAnswer.first.text, "expected the record to have been updated"
-
-# TODO: We need to add in validation checks on the model and reactivate this test
- # Invalid save
-# put admin_update_suggested_answer_path(SuggestedAnswer.first), {suggested_answer: {text: nil}}
-# assert flash[:notice].starts_with?(_('Could not update your'))
-# assert_response :success
-# assert assigns(:suggested_answer)
-# assert assigns(:question)
-# assert assigns(:section)
-# assert assigns(:phase)
- end
-
- # DELETE /org/admin/templates/suggested_answers/:id/admin_destroy (admin_destroy_suggested_answer_path)
- # ----------------------------------------------------------
- test "delete the section" do
- id = SuggestedAnswer.first.id
- # Should redirect user to the root path if they are not logged in!
- delete admin_destroy_suggested_answer_path(id: id)
- assert_unauthorized_redirect_to_root_path
-
- sign_in @user
-
- delete admin_destroy_suggested_answer_path(id: id)
- assert_equal _('Information was successfully deleted.'), flash[:notice]
- assert_response :redirect
- assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}?edit=true§ion_id=#{@question.section.id}"
- assert assigns(:question)
- assert assigns(:section)
- assert assigns(:phase)
- assert_raise ActiveRecord::RecordNotFound do
- SuggestedAnswer.find(id).nil?
- end
- end
-
-end
\ No newline at end of file
diff --git a/test/unit/annotation_test.rb b/test/unit/annotation_test.rb
new file mode 100644
index 0000000..9734c10
--- /dev/null
+++ b/test/unit/annotation_test.rb
@@ -0,0 +1,63 @@
+require 'test_helper'
+
+class AnnotationTest < ActiveSupport::TestCase
+
+ setup do
+ scaffold_template
+
+ @org = Org.last
+ @question = @template.phases.first.sections.first.questions.first
+
+ @annotation = Annotation.create(org: @org, question: @question, text: 'Test',
+ type: Annotation.types[:example_answer])
+ end
+
+ # ---------------------------------------------------
+ test "required fields are required" do
+ assert_not Annotation.new.valid?
+ assert_not Annotation.new(org: @org, text: 'Tester').valid?, "expected the 'question' field to be required"
+ assert_not Annotation.new(question: @question, text: 'Tester').valid?, "expected the 'org' field to be required"
+
+ # TODO: introduce validation on the model that requires text to be provided.
+ #assert_not Annotation.new(org: @org, question: @question).valid?, "expected the 'text' field to be required"
+
+ # Ensure the bare minimum and complete versions are valid
+ a = Annotation.new(org: @org, question: @question, text: 'Tester')
+ assert a.valid?, "expected the 'org', 'question' and 'text' fields to be enough to create an Annotation! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
+ end
+
+ # ---------------------------------------------------
+ test "to_s returns the text" do
+ assert_equal @annotation.text, @annotation.to_s
+ end
+
+ # ---------------------------------------------------
+ test "deep_copy" do
+ verify_deep_copy(@annotation, ['id', 'created_at', 'updated_at'])
+ end
+
+ # ---------------------------------------------------
+ test "can CRUD Annotation" do
+ obj = Annotation.create(org: @org, question: @question, text: 'Tester')
+ assert_not obj.id.nil?, "was expecting to be able to create a new Annotation: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
+
+ obj.text = 'my tester'
+ obj.save!
+ obj.reload
+ assert_equal 'my tester', obj.text, "Was expecting to be able to update the text of the Annotation!"
+
+ assert obj.destroy!, "Was unable to delete the Annotation!"
+ end
+
+ # ---------------------------------------------------
+ test "can manage belongs_to relationship with Org" do
+ annotation = Annotation.new(question: @question, text: 'Testing')
+ verify_belongs_to_relationship(annotation, @org)
+ end
+
+ # ---------------------------------------------------
+ test "can manage belongs_to relationship with Question" do
+ annotation = Annotation.new(org: @org, text: 'Testing')
+ verify_belongs_to_relationship(annotation, @question)
+ end
+end
\ No newline at end of file
diff --git a/test/unit/org_test.rb b/test/unit/org_test.rb
index c2c497d..3e7fd34 100644
--- a/test/unit/org_test.rb
+++ b/test/unit/org_test.rb
@@ -181,9 +181,9 @@
end
# ---------------------------------------------------
- test "can manage has_many relationship with SuggestedAnswers" do
- sa = SuggestedAnswer.new(question: Question.first, text: 'Test Suggested Answer')
- verify_has_many_relationship(@org, sa, @org.suggested_answers.count)
+ test "can manage has_many relationship with Annotations" do
+ a = Annotation.new(question: Question.first, text: 'Test Annotation')
+ verify_has_many_relationship(@org, a, @org.annotations.count)
end
# ---------------------------------------------------
diff --git a/test/unit/question_test.rb b/test/unit/question_test.rb
index be8981e..a5bb6c3 100644
--- a/test/unit/question_test.rb
+++ b/test/unit/question_test.rb
@@ -4,18 +4,18 @@
setup do
@user = User.last
-
+
scaffold_template
-
+
@section = @template.phases.first.sections.first
-
+
@question = Question.create(text: 'Test question', default_value: 'ABCD', guidance: 'Hello',
- number: 999, section: @section,
- question_format: QuestionFormat.where(option_based: false).first,
+ number: 999, section: @section,
+ question_format: QuestionFormat.where(option_based: false).first,
option_comment_display: true, modifiable: true,
- themes: [Theme.first],
- suggested_answers: [SuggestedAnswer.new(org: @user.org,
- text: "just a suggestion")])
+ themes: [Theme.first],
+ annotations: [Annotation.new(org: @user.org,
+ text: "just a suggestion")])
end
# ---------------------------------------------------
@@ -24,21 +24,21 @@
assert_not Question.new(section: @section, number: 7).valid?, "expected the 'text' field to be required"
assert_not Question.new(number: 7, text: 'Testing').valid?, "expected the 'section' field to be required"
assert_not Question.new(section: @section, text: 'Testing').valid?, "expected the 'number' field to be required"
-
+
# Ensure the bar minimum and complete versions are valid
a = Question.new(section: @section, text: 'Testing', number: 7)
assert a.valid?, "expected the 'text', 'section' and 'number' fields to be enough to create an Question! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
end
-
+
# ---------------------------------------------------
test "to_s returns the Question text" do
assert_equal @question.text, @question.to_s
end
-
+
# ---------------------------------------------------
test "returns the correct themed guidance for the org" do
all = Theme.first.guidances + Theme.last.guidances
-
+
# Attach 2 themes to the question
@question.themes = [Theme.first, Theme.last]
@question.save!
@@ -53,25 +53,25 @@
assert @question.guidance_for_org(@user.org).first.first.include?(Theme.first.guidances.first.guidance_group.name), "expected the guidance_group.name"
assert_equal Theme.first.guidances.first, @question.guidance_for_org(@user.org).first.last, "expected the guidance object to be returned"
end
-
+
# ---------------------------------------------------
- test "returns the correct suggested answer for the org" do
- @question.suggested_answers = [SuggestedAnswer.new(org: @user.org, text: 'Test 1', is_example: false),
- SuggestedAnswer.new(org: Org.first, text: 'Test 2', is_example: false)]
+ test "returns the correct annotation for the org" do
+ @question.annotations = [Annotation.create(org: @user.org, text: 'Test 1', type: Annotation.types[:example_answer]),
+ Annotation.create(org: Org.first, text: 'Test 2', type: Annotation.types[:example_answer])]
@question.save!
-
- assert_equal 'Test 1', @question.get_suggested_answer(@user.org.id).text, "expected the correct suggested answer"
- assert_equal 'Test 2', @question.get_suggested_answer(Org.first.id).text, "expected the correct suggested answer"
-
+
+ assert_equal 'Test 1', @question.annotations.where(org_id: @user.org.id).first.text, "expected the correct annotation"
+ assert_equal 'Test 2', @question.annotations.where(org_id: Org.first.id).first.text, "expected the correct annotation"
+
org = Org.create(name: 'New One')
- assert_equal nil, @question.get_suggested_answer(org.id), "expected no suggested answer for a new org"
+ assert_equal nil, @question.get_example_answer(org.id), "expected no annotation for a new org"
end
-
+
# ---------------------------------------------------
test "deep copy" do
verify_deep_copy(@question, ['id', 'created_at', 'updated_at'])
end
-
+
# ---------------------------------------------------
test "can CRUD Question" do
obj = Question.create(section: @section, text: 'Test ABC', number: 7)
@@ -81,39 +81,39 @@
obj.save!
obj.reload
assert_equal 'Testing an update', obj.text, "Was expecting to be able to update the text of the Question!"
-
+
assert obj.destroy!, "Was unable to delete the Question!"
end
-
+
# ---------------------------------------------------
test "can manage belongs_to relationship with Section" do
verify_belongs_to_relationship(@question, @template.phases.first.sections.last)
end
-
+
# ---------------------------------------------------
test "can manage belongs_to relationship with QuestionFormat" do
verify_belongs_to_relationship(@question, QuestionFormat.where(option_based: false).last)
end
-
+
# ---------------------------------------------------
test "can manage has_many relationship with Answer" do
scaffold_plan
a = Answer.new(user: @user, plan: @plan, text: 'Test Answer')
verify_has_many_relationship(@question, a, @question.answers.count)
end
-
+
# ---------------------------------------------------
test "can manage has_many relationship with QuestionOption" do
qo = QuestionOption.new(text: 'Test', number: 9)
verify_has_many_relationship(@question, qo, @question.question_options.count)
end
-
+
# ---------------------------------------------------
- test "can manage has_many relationship with SuggestedAnswer" do
- sa = SuggestedAnswer.new(text: 'Suggested Answer', org: @user.org)
- verify_has_many_relationship(@question, sa, @question.suggested_answers.count)
+ test "can manage has_many relationship with Annotation" do
+ sa = Annotation.new(text: 'Suggested Answer', org: @user.org)
+ verify_has_many_relationship(@question, sa, @question.annotations.count)
end
-
+
# ---------------------------------------------------
test "can manage has_many relationship with Themes" do
t = Theme.new(title: 'Test Theme')
diff --git a/test/unit/suggested_answer_test.rb b/test/unit/suggested_answer_test.rb
deleted file mode 100644
index 31be604..0000000
--- a/test/unit/suggested_answer_test.rb
+++ /dev/null
@@ -1,63 +0,0 @@
-require 'test_helper'
-
-class SuggestedAnswerTest < ActiveSupport::TestCase
-
- setup do
- scaffold_template
-
- @org = Org.last
- @question = @template.phases.first.sections.first.questions.first
-
- @suggested_answer = SuggestedAnswer.create(org: @org, question: @question, text: 'Test',
- is_example: true)
- end
-
- # ---------------------------------------------------
- test "required fields are required" do
- assert_not SuggestedAnswer.new.valid?
- assert_not SuggestedAnswer.new(org: @org, text: 'Tester').valid?, "expected the 'question' field to be required"
- assert_not SuggestedAnswer.new(question: @question, text: 'Tester').valid?, "expected the 'org' field to be required"
-
- # TODO: introduce validation on the model that requires text to be provided.
- #assert_not SuggestedAnswer.new(org: @org, question: @question).valid?, "expected the 'text' field to be required"
-
- # Ensure the bare minimum and complete versions are valid
- a = SuggestedAnswer.new(org: @org, question: @question, text: 'Tester')
- assert a.valid?, "expected the 'org', 'question' and 'text' fields to be enough to create an SuggestedAnswer! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
- end
-
- # ---------------------------------------------------
- test "to_s returns the text" do
- assert_equal @suggested_answer.text, @suggested_answer.to_s
- end
-
- # ---------------------------------------------------
- test "deep_copy" do
- verify_deep_copy(@suggested_answer, ['id', 'created_at', 'updated_at'])
- end
-
- # ---------------------------------------------------
- test "can CRUD SuggestedAnswer" do
- obj = SuggestedAnswer.create(org: @org, question: @question, text: 'Tester')
- assert_not obj.id.nil?, "was expecting to be able to create a new SuggestedAnswer: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
-
- obj.text = 'my tester'
- obj.save!
- obj.reload
- assert_equal 'my tester', obj.text, "Was expecting to be able to update the text of the SuggestedAnswer!"
-
- assert obj.destroy!, "Was unable to delete the SuggestedAnswer!"
- end
-
- # ---------------------------------------------------
- test "can manage belongs_to relationship with Org" do
- suggested_answer = SuggestedAnswer.new(question: @question, text: 'Testing')
- verify_belongs_to_relationship(suggested_answer, @org)
- end
-
- # ---------------------------------------------------
- test "can manage belongs_to relationship with Question" do
- suggested_answer = SuggestedAnswer.new(org: @org, text: 'Testing')
- verify_belongs_to_relationship(suggested_answer, @question)
- end
-end
\ No newline at end of file