diff --git a/app/controllers/annotations_controller.rb b/app/controllers/annotations_controller.rb
index 0558760..36dbc1c 100644
--- a/app/controllers/annotations_controller.rb
+++ b/app/controllers/annotations_controller.rb
@@ -2,110 +2,40 @@
respond_to :html
after_action :verify_authorized
- #create annotations
- def admin_create
- # authorize the question (includes to reduce queries)
- @question = Question.includes(section: { phase: :template}).find(params[:question_id])
- authorize @question
- if params[:example_answer_text].present?
- example_answer = init_annotation(params[:example_answer_text], @question, current_user.org, Annotation.types[:example_answer])
- end
- if params[:guidance_text].present?
- guidance = init_annotation(params[:guidance_text], @question, current_user.org, Annotation.types[:guidance])
- end
- # if they dont exist, no requirement for them to be saved
- ex_save = example_answer.present? ? example_answer.save : true
- guid_save = guidance.present? ? guidance.save : true
- @question.section.phase.template.dirty = true
-
- if ex_save && guid_save
- typ = (example_answer.present? && guidance.present? ? 'example answer and guidance' : (guidance.present? ? 'guidance' : 'example answer'))
- redirect_to admin_show_phase_path(id: @question.section.phase_id, section_id: @question.section_id, question_id: @question.id, edit: 'true'), notice: success_message(typ, _('created'))
- else
- @section = @question.section
- @phase = @section.phase
- @open = true
- @sections = @phase.sections
- @section_id = @section.id
- @question_id = @example_answer.question
- if !ex_save && !guid_save
- flash[:alert] = failed_create_error(example_answer, _('example answer')) + '\n' +
- failed_create_error(gudiance, _('guidance'))
- elsif !guid_save
- flash[:alert] = failed_create_error(gudiance, _('guidance'))
- elsif !ex_save
- flash[:alert] = failed_create_error(example_answer, _('example answer'))
- end
- render "phases/admin_show"
- end
- end
-
-
#update a example answer of a template
def admin_update
- @question = Question.includes(section: { phase: :template}).find(params[:question_id])
- if params[:guidance_id].present?
- guidance = Annotation.includes(question: {section: {phase: :template}}).find(params[:guidance_id])
- authorize guidance
- end
- if params[:example_answer_id].present?
- example_answer = Annotation.includes(question: {section: {phase: :template}}).find(params[:example_answer_id])
- authorize example_answer
- end
- verify_authorized
- # if guidance present, update
- if params[:guidance_text].present?
- if guidance.present?
- guidance.text = params[:guidance_text]
- else
- guidance = init_annotation(params[:guidance_text], @question, current_user.org, Annotation.types[:guidance])
- end
- end
- # if example answer present, update
- if params[:example_answer_text].present?
- if example_answer.present?
- example_answer.text = params[:example_answer_text]
- else
- example_answer = init_annotation(params[:example_answer_text], @question, current_user.org, Annotation.types[:example_answer])
- end
- end
- # only required to save if we updated/created one
- ex_save = example_answer.present? ? example_answer.save : true
- guid_save = guidance.present? ? guidance.save : true
+ question = Question.includes(section: { phase: :template}).find(params[:question_id])
+ authorize question
- @section = @question.section
- @phase = @section.phase
- @phase.template.dirty = true
+# example_answer = Annotation.find_or_create_by(question: question, org: current_user.org, type: Annotation.types[:example_answer])
+# guidance = Annotation.find_or_create_by(question: question, org: current_user.org, type: Annotation.types[:guidance])
+
+ hash = {question_id: question.id, org_id: current_user.org.id}
+ process_changes(hash.merge({type: Annotation.types[:example_answer]}), params[:example_answer_text], _('example answer'))
+ process_changes(hash.merge({type: Annotation.types[:guidance]}), params[:guidance_text], _('guidance'))
- if ex_save && guid_save
- typ = (example_answer.present? && guidance.present? ? 'example answer and guidance' : (guidance.present? ? 'guidance' : 'example answer'))
- redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: success_message(typ, _('saved'))
- else
- if !ex_save && !guid_save
- flash[:alert] = failed_create_error(example_answer, _('example answer')) + '\n' +
- failed_create_error(gudiance, _('guidance'))
- elsif !guid_save
- flash[:alert] = failed_create_error(gudiance, _('guidance'))
- elsif !ex_save
- flash[:alert] = failed_create_error(example_answer, _('example answer'))
- end
- render action: "phases/admin_show"
+ if !flash[:notice].blank? || !flash[:alert].blank?
+ template = question.section.phase.template
+ template.dirty = true
+ template.save!
end
+ redirect_to controller: :phases, action: :admin_show, id: question.section.phase.id
end
#delete an annotation
def admin_destroy
- @example_answer = Annotation.includes(question: { section: {phase: :template}}).find(params[:id])
- authorize @example_answer
- @question = @example_answer.question
- @section = @question.section
- @phase = @section.phase
- @phase.template.dirty = true
- if @example_answer.destroy
- redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: success_message(_('information'), _('deleted'))
- else
- redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: flash[:alert] = failed_destroy_error(@example_answer, _('example answer'))
+ annotation = Annotation.find(params[:id])
+ authorize annotation
+ phase_id = Annotation.joins("INNER JOIN questions ON annotations.question_id = questions.id").joins("INNER JOIN sections ON questions.section_id = sections.id").joins("INNER JOIN phases ON sections.phase_id = phases.id").where("annotations.id": params[:id]).pluck("phases.id").first #annotation.question.section.phase.id
+ if annotation.present?
+ type = (annotation.type == Annotation.types[:example_answer] ? 'example answer' : 'guidance')
+ if annotation.destroy!
+ flash[:notice] = success_message(type, _('removed'))
+ else
+ flash[:alert] = failed_destroy_error(annotation, type)
+ end
end
+ redirect_to controller: :phases, action: :admin_show, id: phase_id
end
private
@@ -119,4 +49,28 @@
return annotation
end
+ def process_changes(hash, input, type)
+ # If the input is available update the annotation otherwise remove it if it exists
+ if input.present?
+ annotation = Annotation.find_or_create_by(hash)
+ if annotation.text != input
+ annotation.text = input
+ if annotation.save!
+ flash[:notice] = "#{(flash[:notice].nil? ? '' : flash[:notice] + '
')}#{success_message(type, _('updated'))}"
+ else
+ flash[:alert] = "#{(flash[:alert].nil? ? '' : flash[:alert] + '
')}#{failed_update_error(annotation, type)}"
+ end
+ end
+ else
+ # If the user cleared the text and the record exists, delete it
+ annotation = Annotation.find_by(hash)
+ if annotation.present?
+ if annotation.destroy!
+ flash[:notice] = "#{(flash[:notice].nil? ? '' : flash[:notice] + '
')}#{success_message(type, _('removed'))}"
+ else
+ flash[:alert] = "#{(flash[:alert].nil? ? '' : flash[:alert] + '
')}#{failed_update_error(annotation, type)}"
+ end
+ end
+ end
+ end
end
diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb
index dc04b8e..9ec3198 100644
--- a/app/controllers/roles_controller.rb
+++ b/app/controllers/roles_controller.rb
@@ -84,6 +84,8 @@
end
if role.save
flash[:notice] = _('Plan removed')
+ else
+ flash[:alert] = _('Unable to remove the plan')
end
redirect_to(plans_path)
end
diff --git a/app/views/annotations/_add.html.erb b/app/views/annotations/_add.html.erb
deleted file mode 100644
index 96d290d..0000000
--- a/app/views/annotations/_add.html.erb
+++ /dev/null
@@ -1,23 +0,0 @@
-
-
-
<%= _('Add Annotations') %>
-<%= form_tag admin_create_annotation_path , class: 'add_annotation_form' do %>
-
-
-
-
- <%= submit_tag _('Save'), class: "btn btn-primary" %>
- <%= hidden_field_tag :question_id, question.id, class: "question_id" %>
- <%= link_to _('Cancel'), "#add_annotations_div_#{question.id}", class: "btn cancel btn-default cancel_add_annotations", role: 'button'%>
-
-<%end%>
diff --git a/app/views/annotations/_edit.html.erb b/app/views/annotations/_edit.html.erb
deleted file mode 100644
index 821c3d4..0000000
--- a/app/views/annotations/_edit.html.erb
+++ /dev/null
@@ -1,38 +0,0 @@
-
-<%= _('Edit Annotations') %>
-<%= form_tag admin_update_annotation_path, method: :put do %>
- <% example_answer_text = example_answer.present? ? example_answer.text : '' %>
- <% guidance_text = guidance.present? ? guidance.text : '' %>
- <%= hidden_field_tag :example_answer_id, example_answer.present? ? example_answer.id : nil %>
- <%= hidden_field_tag :guidance_id, guidance.present? ? guidance.id : nil %>
-
-
-
-
-
-
-
- <%= submit_tag _('Save'), class: 'btn btn-primary' %>
- <% if example_answer.present? %>
- <%= link_to _('Delete Example Answer'), admin_destroy_annotation_path(id: example_answer.id),
- confirm: _("You are about to delete an example answer for '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%>
- <% end %>
- <% if guidance.present? %>
- <%= link_to _('Delete Example Answer'), admin_destroy_annotation_path(id: guidance.id),
- confirm: _("You are about to delete a guidance for '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%>
- <% end %>
-
- <%= hidden_field_tag :question_id, question.id, class: "question_id" %>
- <%= link_to _('Cancel'), "#edit_annotations_div_#{question.id}", class: 'btn cancel btn-default cancel_edit_annotations', role: 'button' %>
-
-<% end %>
diff --git a/app/views/annotations/_new_edit.html.erb b/app/views/annotations/_new_edit.html.erb
new file mode 100644
index 0000000..52c20c6
--- /dev/null
+++ b/app/views/annotations/_new_edit.html.erb
@@ -0,0 +1,22 @@
+
+<%= form_tag options[:url], method: options[:method] do %>
+
+
+ <%= label_tag :example_answer_text, _('Example answer'), class: "control-label" %>
+ <%= text_area_tag :example_answer_text, (example_answer.present? ? example_answer.text : ''), class: "question" %>
+
+
+
+ <%= label_tag :guidance_text, _('Guidance'), class: 'control-label' %>
+ <%= text_area_tag :guidance_text, (guidance.present? ? guidance.text : ''), class: 'question' %>
+
+
+
+
+<% end %>
diff --git a/app/views/annotations/_show.html.erb b/app/views/annotations/_show.html.erb
index 8ce44e2..65439aa 100644
--- a/app/views/annotations/_show.html.erb
+++ b/app/views/annotations/_show.html.erb
@@ -1,11 +1,13 @@
- <%= _('Annotations') %>
-
- <% if example_answer.present? %>
- - <%= _('Example Answer') %>
- - <%= example_answer.text %>
-
- <% end %>
- <% if guidance.present? %>
-
- <%= _('Guidance') %>
- - <%=guidance.text%>
- <% end %>
-
+<% org = template.org.abbreviation.present? ? template.org.abbreviation : template.org.name %>
+<% if example_answer.present? || guidance.present? %>
+ >
+ <% if example_answer.present? %>
+ - <%= "#{for_plan ? "#{org} " : ''}#{_('Example answer')}" %>
+ - <%= raw example_answer.text %>
-
+ <% end %>
+ <% if guidance.present? %>
+
- <%= "#{for_plan ? "#{org} " : ''}#{_('Guidance')}" %>
+ - <%= raw guidance.text %>
+ <% end %>
+
+<% end %>
diff --git a/app/views/paginable/plans/_privately_visible.html.erb b/app/views/paginable/plans/_privately_visible.html.erb
index 326118a..93fe83b 100644
--- a/app/views/paginable/plans/_privately_visible.html.erb
+++ b/app/views/paginable/plans/_privately_visible.html.erb
@@ -77,8 +77,7 @@
<% end %>
<% role = plan.roles.where(user_id: current_user.id).first %>
<% conf = (role.creator? && plan.publicly_visible?) ? _("Are you sure you wish to remove this public plan? This will remove it from the Public DMPs page but any collaborators will still be able to access it.") : _("Are you sure you wish to remove this plan? Any collaborators will still be able to access it.") %>
- <%= link_to _('Remove'), deactivate_role_path(role),
- method: :put, data: { confirm: conf } %>
+ <%= link_to _('Remove'), deactivate_role_path(role), 'data-method': 'put', rel: 'nofollow', 'data-confirm': conf %>
diff --git a/app/views/phases/_admin_show.html.erb b/app/views/phases/_admin_show.html.erb
index da337a5..62e1ace 100644
--- a/app/views/phases/_admin_show.html.erb
+++ b/app/views/phases/_admin_show.html.erb
@@ -42,9 +42,9 @@
aria-labelledby="<%= "headingSection#{section.id}" %>">
<% if edit && section.modifiable %>
- <%= render partial: 'sections/edit_section', locals: { section: section, edit: edit, phase: phase } %>
+ <%= render partial: 'sections/edit_section', locals: { template: template, section: section, edit: edit, phase: phase } %>
<% else %>
- <%= render partial: 'sections/show_section', locals: { section: section } %>
+ <%= render partial: 'sections/show_section', locals: { template: template, section: section } %>
<% end %>
diff --git a/app/views/phases/_guidances_notes.html.erb b/app/views/phases/_guidances_notes.html.erb
index 9d0781f..7f33c87 100644
--- a/app/views/phases/_guidances_notes.html.erb
+++ b/app/views/phases/_guidances_notes.html.erb
@@ -41,7 +41,7 @@
template: template,
example_answer: (annotation.example_answer? ? annotation : nil),
guidance: (annotation.guidance? ? annotation : nil),
- question: question
+ for_plan: true
} %>
<% num_annotations += 1%>
<% i += 1 %>
diff --git a/app/views/questions/_show_question.html.erb b/app/views/questions/_show_question.html.erb
index 9da57b9..a5b2f52 100644
--- a/app/views/questions/_show_question.html.erb
+++ b/app/views/questions/_show_question.html.erb
@@ -62,17 +62,38 @@
<% example_answer = question.get_example_answer(current_user.org_id) %>
<% guidance = question.get_guidance_annotation(current_user.org_id) %>
+ <% editing = (example_answer.present? || guidance.present?) %>
<% if !question.modifiable %>
- <% if example_answer.present? || guidance.present? %>
-
- <%= render partial: 'annotations/show', locals: {example_answer: example_answer, guidance: guidance, question: question} %>
-
-
- <%= render partial: 'annotations/edit', locals: {example_answer: example_answer, guidance: guidance, question: question} %>
-
- <% end %>
-
- <%= render partial: 'annotations/add', locals: {question: question} %>
+
<%= _('Annotations') %>
+
+ <%= render partial: 'annotations/show',
+ locals: { example_answer: example_answer,
+ guidance: guidance,
+ template: template,
+ for_plan: false } %>
+ <% unless question.modifiable %>
+
+ <%= link_to _('%{add_or_edit} Annotations') % { add_or_edit: (editing ? 'Edit' : 'Add') },
+ "#annotations_div_#{question.id}", class: "btn btn-default annotations_button", role:"button" %>
+ <% if example_answer.present? %>
+ <%= link_to _('Delete Example Answer'), admin_destroy_annotation_path(id: example_answer.id), 'data-method': 'delete',
+ rel: 'nofollow', class: "btn btn-default", 'data-confirm': _("Are you sure you would like to remove the example answer for this question?") %>
+ <% end %>
+ <% if guidance.present? %>
+ <%= link_to _('Delete Guidance'), admin_destroy_annotation_path(id: guidance.id), 'data-method': 'delete',
+ rel: 'nofollow', class: "btn btn-default", 'data-confirm': _("Are you sure you would like to remove the guidance for this question?") %>
+ <% end %>
+
+ <% end %>
+
+
+ <%= render partial: 'annotations/new_edit',
+ locals: {
+ example_answer: example_answer,
+ guidance: guidance,
+ question: question,
+ options: { url: admin_update_annotation_path, method: 'PUT' }
+ } %>
<% end %>
@@ -80,16 +101,6 @@
<%= link_to _('Edit question'), "#question_edit#{question.id}", class: "btn btn-default question_edit_link", role: "button" %>
<%= link_to _('Delete question'), admin_destroy_question_path(question_id: question.id),
confirm: _("You are about to delete '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-default", role:"button" %>
- <% else %>
- <% if example_answer.nil? && guidance.nil? %>
-
- <%= link_to _('Add Annotations'), "#add_annotations_div_#{question.id}", class: "btn btn-default add_annotations_button", role:"button"%>
-
- <% else %>
-
- <%= link_to _('Edit Annotations'), "#edit_annotations_div_#{question.id}", class: "btn btn-default edit_annotations_button", role:"button"%>
-
- <% end %>
<% end %>
diff --git a/app/views/sections/_edit_section.html.erb b/app/views/sections/_edit_section.html.erb
index db36fa1..1e638a1 100644
--- a/app/views/sections/_edit_section.html.erb
+++ b/app/views/sections/_edit_section.html.erb
@@ -44,10 +44,10 @@
<% questions.each do |question| %>
">
- <%= render partial: 'questions/show_question', locals: {question: question} %>
+ <%= render partial: 'questions/show_question', locals: {template: template, question: question} %>
" style="display: none;">
- <%= render partial: 'questions/edit_question', locals: {question: question} %>
+ <%= render partial: 'questions/edit_question', locals: {template: template, question: question} %>
<%= raw("
") if questions.last.id == question.id %>
<% end %>
diff --git a/app/views/sections/_show_section.html.erb b/app/views/sections/_show_section.html.erb
index 5e15582..c8f14c0 100644
--- a/app/views/sections/_show_section.html.erb
+++ b/app/views/sections/_show_section.html.erb
@@ -13,11 +13,11 @@
<% questions.each do |question| %>
">
- <%= render partial: 'questions/show_question', locals: {question: question} %>
+ <%= render partial: 'questions/show_question', locals: {template: template, question: question} %>
<% if question.modifiable %>
" style="display: none;">
- <%= render partial: 'questions/edit_question', locals: {question: question} %>
+ <%= render partial: 'questions/edit_question', locals: {template: template, question: question} %>
<% end %>
<% if questions.last.id != question.id %>
diff --git a/config/routes.rb b/config/routes.rb
index 310794e..ec70933 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -163,7 +163,6 @@
resources :annotations, path: 'org/admin/templates/annotations', only: [] do
member do
- post 'admin_create'
put 'admin_update'
delete 'admin_destroy'
end
diff --git a/lib/assets/javascripts/views/questions/show.js b/lib/assets/javascripts/views/questions/show.js
index 718fefa..7a744fd 100644
--- a/lib/assets/javascripts/views/questions/show.js
+++ b/lib/assets/javascripts/views/questions/show.js
@@ -6,18 +6,17 @@
$(target).show();
});
- $('.add_annotations_button').on('click', (e) => {
- const source = e.target;
- const target = $(source).attr('href');
+ $('.annotations_button').on('click', (e) => {
+ e.preventDefault();
+ const target = $(e.target).attr('href');
$(target).show();
- $(source).hide();
+ $(target).closest('.col-md-12').find('.show_annotations_div').hide();
});
- $('.edit_annotations_button').on('click', (e) => {
- const source = e.target;
- const target = $(source).attr('href');
- $(target).show();
- $(source).hide();
- $(target).closest('.col-md-12').find('.show_annotations_div').hide();
+ $('.cancel_edit_annotations').on('click', (e) => {
+ e.preventDefault();
+ const target = $(e.target).attr('href');
+ $(target).hide();
+ $(target).closest('.col-md-12').find('.show_annotations_div').show();
});
});
diff --git a/test/functional/annotations_controller_test.rb b/test/functional/annotations_controller_test.rb
index cf299b7..39f7cd4 100644
--- a/test/functional/annotations_controller_test.rb
+++ b/test/functional/annotations_controller_test.rb
@@ -6,124 +6,93 @@
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_guid = {question_id: @question.id, guidance_text: "some guidance text"}
- params_example = {question_id: @question.id, example_answer_text: "example answer text"}
- params_both = {question_id: @question.id, example_answer_text: "example answer text", guidance_text: "some guidance text"}
-
- # Should redirect user to the root path if they are not logged in!
- post admin_create_annotation_path(id: Annotation.first.id), params_both
- assert_unauthorized_redirect_to_root_path
-
- sign_in @user
-
- # both
- post admin_create_annotation_path(id: Annotation.first.id), params_both
- 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 flash[:notice].start_with?('Successfully') && flash[:notice].include?('created')
- assert_equal "some guidance text", Annotation.last.text, "expected the guidance to have been created!"
- assert_equal "example answer text", Annotation.all[-2].text, "expected the example answer to have been created"
- # just an example answer
- post admin_create_annotation_path(id: Annotation.first.id), params_example
- 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 flash[:notice].start_with?('Successfully') && flash[:notice].include?('created')
- assert_equal "example answer text", Annotation.last.text, "expected the record to have been created!"
- # just some guidance
- post admin_create_annotation_path(id: Annotation.first.id), params_guid
- 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 flash[:notice].start_with?('Successfully') && flash[:notice].include?('created')
- assert_equal "some guidance text", Annotation.last.text, "expected the record to have been created!"
-
- end
-
- # PUT /org/admin/templates/suggested_answers/:id/admin_update (admin_update_suggested_answer_path)
- # ----------------------------------------------------------
- test "update the annotation" do
- q = Annotation.first.question
- params_guid = {question_id: q.id, guidance_id: Annotation.first.id ,guidance_text: 'UPDATE'}
- params_example = {question_id: q.id, example_answer_id: Annotation.first.id, example_answer_text: 'UPDATE'}
- params_both = {question_id: q.id, guidance_id: Annotation.first.id ,guidance_text: 'gUPDATE',example_answer_id: Annotation.last.id, example_answer_text: 'eUPDATE'}
-
- # Should redirect user to the root path if they are not logged in!
- put admin_update_annotation_path(id: Annotation.first.id), params_guid
- assert_unauthorized_redirect_to_root_path
-
- sign_in @user
-
- # Valid save for guidance only
- put admin_update_annotation_path(id: Annotation.first.id), params_guid
- assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('saved')
- 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 'UPDATE', Annotation.first.text, "expected the record to have been updated"
- # valid save for example only
- put admin_update_annotation_path(id: Annotation.first.id), params_example
- assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('saved')
- 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 'UPDATE', Annotation.first.text, "expected the record to have been updated"
- # valid save for both example answer and guidance
- put admin_update_annotation_path(id: Annotation.first.id), params_both
- assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('saved')
- 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 'gUPDATE', Annotation.first.text, "expected the record to have been updated"
- assert_equal 'eUPDATE', Annotation.last.text, "expected the record to have been updated"
-
- 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 flash[:notice].start_with?('Successfully') && flash[:notice].include?('deleted')
- 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?
+ # clear the existing annotations
+ @question.annotations.where(org: @user.org).each do |annotation|
+ annotation.destroy!
end
+
+ @create_hash = {question_id: @question.id, example_answer_text: "New example", guidance_text: "New guidance"}
+ @example_answer_qry = {question: @question, org: @user.org, type: Annotation.types[:example_answer]}
+ @guidance_qry = {question: @question, org: @user.org, type: Annotation.types[:guidance]}
end
+ test "cannot create/update if not logged in" do
+ # Should redirect user to the root path if they are not logged in!
+ put admin_update_annotation_path(id: @question.section.phase.id), @create_hash
+ assert_unauthorized_redirect_to_root_path
+ end
+
+ test "can create example answer and guidance at the same time" do
+ sign_in @user
+ put admin_update_annotation_path(id: @question.section.phase.id), @create_hash
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}"
+ assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('example answer') && flash[:notice].include?('guidance')
+ assert_equal 'New example', Annotation.find_by(@example_answer_qry).text, "expected example answer to have been created."
+ assert_equal 'New guidance', Annotation.find_by(@guidance_qry).text, "expected guidance to have been created."
+ end
+ test "can create example answer without a guidance" do
+ sign_in @user
+ put admin_update_annotation_path(id: @question.section.phase.id), {question_id: @question.id, example_answer_text: "New example"}
+ assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('updated')
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}"
+ assert_equal 'New example', Annotation.find_by(@example_answer_qry).text, "expected example answer to have been created."
+ assert Annotation.find_by(@guidance_qry).nil?, "expected no guidance to have been created."
+ end
+ test "can create guidance without an example answer" do
+ sign_in @user
+ put admin_update_annotation_path(id: @question.section.phase.id), {question_id: @question.id, guidance_text: "New guidance"}
+ assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('updated')
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}"
+ assert Annotation.find_by(@example_answer_qry).nil?, "expected no example answer to have been created."
+ assert_equal 'New guidance', Annotation.find_by(@guidance_qry).text, "expected guidance to have been created."
+ end
+
+ test "can update example answer and guidance at the same time" do
+ put admin_update_annotation_path(id: @question.section.phase.id), @create_hash
+ sign_in @user
+ put admin_update_annotation_path(id: @question.section.phase.id), {question_id: @question.id, example_answer_text: "Updated example", guidance_text: "Updated guidance"}
+ assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('updated')
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}"
+ assert_equal 'Updated example', Annotation.find_by(@example_answer_qry).text, "expected example answer to have been updated."
+ assert_equal 'Updated guidance', Annotation.find_by(@guidance_qry).text, "expected guidance to have been updated."
+ end
+ test "can remove example answer by not submitting it during save" do
+ put admin_update_annotation_path(id: @question.section.phase.id), @create_hash
+ sign_in @user
+ put admin_update_annotation_path(id: @question.section.phase.id), {question_id: @question.id, guidance_text: "Updated guidance"}
+ assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('updated')
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}"
+ assert Annotation.find_by(@example_answer_qry).nil?, "expected example answer to have been removed."
+ assert_equal 'Updated guidance', Annotation.find_by(@guidance_qry).text, "expected guidance to have been updated."
+ end
+ test "can remove guidance by not submitting it during save" do
+ put admin_update_annotation_path(id: @question.section.phase.id), @create_hash
+ sign_in @user
+ put admin_update_annotation_path(id: @question.section.phase.id), {question_id: @question.id, example_answer_text: "Updated example"}
+ assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('updated')
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}"
+ assert_equal 'Updated example', Annotation.find_by(@example_answer_qry).text, "expected example answer to have been updated."
+ assert Annotation.find_by(@guidance_qry).nil?, "expected guidance to have been removed."
+ end
+
+ test "can delete a specific annotation" do
+ sign_in @user
+ put admin_update_annotation_path(id: @question.section.phase.id), @create_hash
+ delete admin_destroy_annotation_path(Annotation.find_by(@example_answer_qry))
+ assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('removed')
+ assert_response :redirect
+ assert_redirected_to "#{admin_show_phase_path(@question.section.phase.id)}"
+ assert Annotation.find_by(@example_answer_qry).nil?
+ assert_equal 'New guidance', Annotation.find_by(@guidance_qry).text, "expected guidance to have been unchanged."
+ end
end
\ No newline at end of file