diff --git a/app/controllers/annotations_controller.rb b/app/controllers/annotations_controller.rb new file mode 100644 index 0000000..2067ee0 --- /dev/null +++ b/app/controllers/annotations_controller.rb @@ -0,0 +1,53 @@ +class AnnotationsController < ApplicationController + respond_to :html + after_action :verify_authorized + + #create suggested answers + def admin_create + @suggested_answer = Annotation.new(params[:suggested_answer]) + authorize @suggested_answer + if @suggested_answer.save + redirect_to admin_show_phase_path(id: @suggested_answer.question.section.phase_id, section_id: @suggested_answer.question.section_id, question_id: @suggested_answer.question.id, edit: 'true'), notice: _('Information was successfully created.') + else + @phase = @suggested_answer.question.section.phase + @section = @suggested_answer.question.section + @open = true + @sections = @phase.sections + @section_id = @section.id + @question_id = @suggested_answer.question + flash[:notice] = failed_create_error(@suggested_answer, _('suggested answer')) + render "phases/admin_show" + end + end + + + #update a suggested answer of a template + def admin_update + @suggested_answer = SuggestedAnswer.includes(question: { section: {phase: :template}}).find(params[:id]) + authorize @suggested_answer #.question.section.phase.template + @question = @suggested_answer.question + @section = @question.section + @phase = @section.phase + if @suggested_answer.update_attributes(params[:suggested_answer]) + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.') + else + flash[:notice] = failed_update_error(@suggested_answer, _('suggested answer')) + render action: "phases/admin_show" + end + end + + #delete a suggested answer + def admin_destroy + @suggested_answer = SuggestedAnswer.includes(question: { section: {phase: :template}}).find(params[:id]) + authorize @suggested_answer + @question = @suggested_answer.question + @section = @question.section + @phase = @section.phase + if @suggested_answer.destroy + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.') + else + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: flash[:notice] = failed_destroy_error(@suggested_answer, _('suggested answer')) + end + end + +end \ No newline at end of file diff --git a/app/controllers/suggested_answers_controller.rb b/app/controllers/suggested_answers_controller.rb deleted file mode 100644 index cfe3da0..0000000 --- a/app/controllers/suggested_answers_controller.rb +++ /dev/null @@ -1,53 +0,0 @@ -class SuggestedAnswersController < ApplicationController - respond_to :html - after_action :verify_authorized - - #create suggested answers - def admin_create - @suggested_answer = SuggestedAnswer.new(params[:suggested_answer]) - authorize @suggested_answer - if @suggested_answer.save - redirect_to admin_show_phase_path(id: @suggested_answer.question.section.phase_id, section_id: @suggested_answer.question.section_id, question_id: @suggested_answer.question.id, edit: 'true'), notice: _('Information was successfully created.') - else - @phase = @suggested_answer.question.section.phase - @section = @suggested_answer.question.section - @open = true - @sections = @phase.sections - @section_id = @section.id - @question_id = @suggested_answer.question - flash[:notice] = failed_create_error(@suggested_answer, _('suggested answer')) - render "phases/admin_show" - end - end - - - #update a suggested answer of a template - def admin_update - @suggested_answer = SuggestedAnswer.includes(question: { section: {phase: :template}}).find(params[:id]) - authorize @suggested_answer #.question.section.phase.template - @question = @suggested_answer.question - @section = @question.section - @phase = @section.phase - if @suggested_answer.update_attributes(params[:suggested_answer]) - redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.') - else - flash[:notice] = failed_update_error(@suggested_answer, _('suggested answer')) - render action: "phases/admin_show" - end - end - - #delete a suggested answer - def admin_destroy - @suggested_answer = SuggestedAnswer.includes(question: { section: {phase: :template}}).find(params[:id]) - authorize @suggested_answer - @question = @suggested_answer.question - @section = @question.section - @phase = @section.phase - if @suggested_answer.destroy - redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.') - else - redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: flash[:notice] = failed_destroy_error(@suggested_answer, _('suggested answer')) - end - end - -end \ No newline at end of file diff --git a/app/models/suggested_answer.rb b/app/models/suggested_answer.rb deleted file mode 100644 index 881f302..0000000 --- a/app/models/suggested_answer.rb +++ /dev/null @@ -1,42 +0,0 @@ -class SuggestedAnswer < ActiveRecord::Base - - ## - # Associations - belongs_to :org - belongs_to :question - - ## - # Possibly needed for active_admin - # -relies on protected_attributes gem as syntax depricated in rails 4.2 - attr_accessible :org_id, :question_id, :text, :is_example, - :org, :question, :as => [:default, :admin] - - - validates :question, :org, presence: {message: _("can't be blank")} - - # EVALUATE CLASS AND INSTANCE METHODS BELOW - # - # What do they do? do they do it efficiently, and do we need them? - - - - ## - # returns the text from the suggested_answer - # - # @return [String] the text from the suggested_answer - def to_s - "#{text}" - end - - - ## - # deep copy the given question_option and all it's associations - # - # @params [QuestionOption] question_option to be deep copied - # @return [QuestionOption] the saved, copied question_option - def self.deep_copy(suggested_answer) - suggested_answer_copy = suggested_answer.dup - suggested_answer_copy.save! - return suggested_answer_copy - end -end \ No newline at end of file diff --git a/app/policies/annotation_policy.rb b/app/policies/annotation_policy.rb new file mode 100644 index 0000000..19ff01a --- /dev/null +++ b/app/policies/annotation_policy.rb @@ -0,0 +1,28 @@ +class AnnotationPolicy < ApplicationPolicy + attr_reader :user, :annotation + + def initialize(user, annotation) + raise Pundit::NotAuthorizedError, "must be logged in" unless user + @user = user + @annotation = annotation + end + + ## + # Users can modify annotations if: + # - They can modify templates + # - The template which they are modifying belongs to their org + ## + + def admin_create? + user.can_modify_templates? && (annotation.question.section.phase.template.org_id == user.org_id) + end + + def admin_update? + user.can_modify_templates? && (annotation.question.section.phase.template.org_id == user.org_id) + end + + def admin_destroy? + user.can_modify_templates? && (annotation.question.section.phase.template.org_id == user.org_id) + end + +endgi \ No newline at end of file diff --git a/app/policies/suggested_answer_policy.rb b/app/policies/suggested_answer_policy.rb deleted file mode 100644 index e6cd49c..0000000 --- a/app/policies/suggested_answer_policy.rb +++ /dev/null @@ -1,28 +0,0 @@ -class SuggestedAnswerPolicy < ApplicationPolicy - attr_reader :user, :suggested_answer - - def initialize(user, suggested_answer) - raise Pundit::NotAuthorizedError, "must be logged in" unless user - @user = user - @suggested_answer = suggested_answer - end - - ## - # Users can modify suggested answers if: - # - They can modify templates - # - The template which they are modifying belongs to their org - ## - - def admin_create? - user.can_modify_templates? && (suggested_answer.question.section.phase.template.org_id == user.org_id) - end - - def admin_update? - user.can_modify_templates? && (suggested_answer.question.section.phase.template.org_id == user.org_id) - end - - def admin_destroy? - user.can_modify_templates? && (suggested_answer.question.section.phase.template.org_id == user.org_id) - end - -end \ No newline at end of file diff --git a/app/views/annotations/_add_annotation.html.erb b/app/views/annotations/_add_annotation.html.erb new file mode 100644 index 0000000..fa5a8bf --- /dev/null +++ b/app/views/annotations/_add_annotation.html.erb @@ -0,0 +1,25 @@ + +<%= form_for :suggested_answer, url: admin_create_suggested_answer_path do |f| %> + <%= f.hidden_field :org_id, value: current_user.org_id %> + <%= f.hidden_field :question_id, value: question.id %> + + + + + + +
<%= _('Suggested answer/ Example')%> +
    +
  • <%= f.select :is_example, {_('Example of answer') => true, _('Suggested answer') => false} %>
  • +
  • <%= f.text_area :text, rows: 5 %> +
  • +
+
+
+ + +
+ <%= f.submit _('Save'), class: "btn btn-primary" %> + <%= link_to _('Cancel'), "#", id: "cancel_suugested_answer", class: "btn cancel" %> +
+<%end%> diff --git a/app/views/annotations/_edit_annotation.html.erb b/app/views/annotations/_edit_annotation.html.erb new file mode 100644 index 0000000..f0d92b7 --- /dev/null +++ b/app/views/annotations/_edit_annotation.html.erb @@ -0,0 +1,26 @@ + +<%= form_for(suggested_answer, url: admin_update_suggested_answer_path(suggested_answer), html: { method: :put}) do |f| %> + <%= f.hidden_field :org_id, value: current_user.org_id %> + + + + + + +
<%= _('Suggested answer/ Example')%> +
    +
  • <%= f.select :is_example, {_('Example of answer') => true, _('Suggested answer') => false} %>
  • +
  • <%= f.text_area :text, rows: 5 %>
  • +
+
+
+ + +
+ <%= f.submit _('Save'), class: 'btn btn-primary' %> + <%= link_to _('Delete'), admin_destroy_suggested_answer_path(id: suggested_answer.id), + confirm: _("You are about to delete a suggested answer/ example for '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%> + <%= hidden_field_tag :question_id, question.id, class: "question_id" %> + <%= link_to _('Cancel'), '#', class: 'btn cancel cancel_edit_suggested_answer' %> +
+<% end %> diff --git a/app/views/annotations/_show_annotation.html.erb b/app/views/annotations/_show_annotation.html.erb new file mode 100644 index 0000000..5d04606 --- /dev/null +++ b/app/views/annotations/_show_annotation.html.erb @@ -0,0 +1,19 @@ + + + + + + +
+ <% if suggested_answer.is_example? then %> + <%= _('Example of answer')%> + <% else %> + <%= _('Suggested answer')%> + <% end %> + <%= raw suggested_answer.text %>
+
+ +
+ <%= hidden_field_tag :question_id, question.id, class: "question_id" %> + <%= link_to _('Edit suggested answer/ example'), '# ', class: "btn btn-primary edit_form_for_suggested_answer"%> +
diff --git a/app/views/suggested_answers/_add_suggested_answer.html.erb b/app/views/suggested_answers/_add_suggested_answer.html.erb deleted file mode 100644 index fa5a8bf..0000000 --- a/app/views/suggested_answers/_add_suggested_answer.html.erb +++ /dev/null @@ -1,25 +0,0 @@ - -<%= form_for :suggested_answer, url: admin_create_suggested_answer_path do |f| %> - <%= f.hidden_field :org_id, value: current_user.org_id %> - <%= f.hidden_field :question_id, value: question.id %> - - - - - - -
<%= _('Suggested answer/ Example')%> -
    -
  • <%= f.select :is_example, {_('Example of answer') => true, _('Suggested answer') => false} %>
  • -
  • <%= f.text_area :text, rows: 5 %> -
  • -
-
-
- - -
- <%= f.submit _('Save'), class: "btn btn-primary" %> - <%= link_to _('Cancel'), "#", id: "cancel_suugested_answer", class: "btn cancel" %> -
-<%end%> diff --git a/app/views/suggested_answers/_edit_suggested_answer.html.erb b/app/views/suggested_answers/_edit_suggested_answer.html.erb deleted file mode 100644 index f0d92b7..0000000 --- a/app/views/suggested_answers/_edit_suggested_answer.html.erb +++ /dev/null @@ -1,26 +0,0 @@ - -<%= form_for(suggested_answer, url: admin_update_suggested_answer_path(suggested_answer), html: { method: :put}) do |f| %> - <%= f.hidden_field :org_id, value: current_user.org_id %> - - - - - - -
<%= _('Suggested answer/ Example')%> -
    -
  • <%= f.select :is_example, {_('Example of answer') => true, _('Suggested answer') => false} %>
  • -
  • <%= f.text_area :text, rows: 5 %>
  • -
-
-
- - -
- <%= f.submit _('Save'), class: 'btn btn-primary' %> - <%= link_to _('Delete'), admin_destroy_suggested_answer_path(id: suggested_answer.id), - confirm: _("You are about to delete a suggested answer/ example for '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%> - <%= hidden_field_tag :question_id, question.id, class: "question_id" %> - <%= link_to _('Cancel'), '#', class: 'btn cancel cancel_edit_suggested_answer' %> -
-<% end %> diff --git a/app/views/suggested_answers/_show_suggested_answer.html.erb b/app/views/suggested_answers/_show_suggested_answer.html.erb deleted file mode 100644 index 5d04606..0000000 --- a/app/views/suggested_answers/_show_suggested_answer.html.erb +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - -
- <% if suggested_answer.is_example? then %> - <%= _('Example of answer')%> - <% else %> - <%= _('Suggested answer')%> - <% end %> - <%= raw suggested_answer.text %>
-
- -
- <%= hidden_field_tag :question_id, question.id, class: "question_id" %> - <%= link_to _('Edit suggested answer/ example'), '# ', class: "btn btn-primary edit_form_for_suggested_answer"%> -