diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index e157e76..2ba72d7 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -1,21 +1,31 @@ class AnswersController < ApplicationController after_action :verify_authorized + ## # POST /answers + # current implimentation creates a new answer each time one is submitted + # + # Probably should rename from from create to update + # Maybe better to just update the existing answer rather than generate a new + # one. Especiall since comments connect to answers def create + # create a new answer based off the passed params @answer = Answer.new(params[:answer]) authorize @answer + # find the prevous answer to this question for this plan (created_at: "DESC").first old_answer = @answer.plan.answer(@answer.question_id, false) proceed = false + # confused why we are passing the text through like this if we have clearly passed the answer's other attr through plainly + # We can re-name it as it's defined in app/views/plans/_answer_form.html.erb @answer.text = params["answer-text-#{@answer.question_id}".to_sym] if (old_answer.nil? && @answer.text != "") || ((!old_answer.nil?) && (old_answer.text != @answer.text)) then proceed = true end - - if (@answer.question.question_format.title == I18n.t("helpers.checkbox") || - @answer.question.question_format.title == I18n.t("helpers.multi_select_box") || - @answer.question.question_format.title == I18n.t("helpers.radio_buttons") || - @answer.question.question_format.title == I18n.t("helpers.dropdown")) then + # Is this validation necissary? + if (@answer.question.question_format.title == I18n.t("helpers.checkbox") || + @answer.question.question_format.title == I18n.t("helpers.multi_select_box") || + @answer.question.question_format.title == I18n.t("helpers.radio_buttons") || + @answer.question.question_format.title == I18n.t("helpers.dropdown")) then if (old_answer.nil? && @answer.option_ids.count > 0) || ((!old_answer.nil?) && (old_answer.option_ids - @answer.option_ids).count != 0 && (@answer.option_ids - old_answer.option_ids).count != 0) then proceed = true end diff --git a/app/policies/answer_policy.rb b/app/policies/answer_policy.rb index 61f6989..805efac 100644 --- a/app/policies/answer_policy.rb +++ b/app/policies/answer_policy.rb @@ -9,7 +9,8 @@ end def create? - @answer.plan.editable_by(@user.id) + # is the plan editable by the user, and is the user_id that of the user + @answer.plan.editable_by(@user.id) && (@answer.user_id == @user.id) end end \ No newline at end of file diff --git a/app/views/plans/_answer_form.html.erb b/app/views/plans/_answer_form.html.erb index 7a416d8..29be29c 100644 --- a/app/views/plans/_answer_form.html.erb +++ b/app/views/plans/_answer_form.html.erb @@ -1,7 +1,7 @@ <% answer = @plan.answer(question.id) %> @@ -17,16 +17,16 @@ <%= f.input :plan_id, :as => :hidden %> <%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %> <%= f.input :question_id, :as => :hidden, :input_html => { :class => "question_id" } %> - + - <%= raw question.text %> - + <% suggested_answer = question.suggested_answers.find_by_organisation_id(@plan.project.organisation_id) %> <% if !suggested_answer.nil? && suggested_answer.text != "" then %> @@ -45,7 +45,7 @@ <% end %> - + <% if q_format.title == t("helpers.checkbox") || q_format.title == t("helpers.multi_select_box") || q_format.title == t("helpers.radio_buttons") || q_format.title == t("helpers.dropdown") then%> <% options = question.options.order("number") %> @@ -80,9 +80,9 @@ <% else %> <%= f.radio_button :option_ids, op.id, :checked => false, id: "answer_option_ids_#{op.id}"%> <% end %> - <%end%> + <% end %> <%= op.text %> - <%end%> + <% end %> <% elsif q_format.title == t("helpers.dropdown") then%> @@ -97,7 +97,7 @@ <%= label_tag("answer-text-#{question.id}".to_sym, t("helpers.comment")) %> <%= text_area_tag("answer-text-#{question.id}".to_sym, answer.text, class: "tinymce") %> <%end%> - + <% elsif q_format.title == t("helpers.text_field") then %> <%= text_field_tag("answer-text-#{question.id}".to_sym, strip_tags(answer.text), class: "question_text_field") %> @@ -119,9 +119,8 @@ <% end %> <% end %> - <% end %> - - + <% end %> +
>

<%= question.text %>

@@ -141,20 +140,17 @@
- <% if answer.created_at.nil? then %> <%= t("helpers.notanswered") %> <% else %> <%= t("helpers.answered_by")%><%= answer.created_at %><%= t("helpers.answered_by_part2")%><%= answer.user.name %> <% end %> -
-
<% @comments = Comment.where("question_id = ? AND plan_id = ?", question.id, answer.plan_id ) %> <%= hidden_field_tag :question_id, question.id, :class => "question_id" %> @@ -206,7 +202,7 @@ <%= t("helpers.policy_expectations")%> <%else%> <%= t("helpers.guidance")%> - <%end%> + <%end%>
@@ -238,19 +234,16 @@ <% end %> <% end %> <% end %> - - - +
<%= render :partial => "comments", locals: {questionId: question.id, plan_id: answer.plan_id }%> -
- - + + <% if last_question_id == question.id then %>