diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index adddd92..e157e76 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -5,37 +5,33 @@ def create @answer = Answer.new(params[:answer]) authorize @answer - if (user_signed_in?) && @answer.plan.editable_by(current_user.id) then - old_answer = @answer.plan.answer(@answer.question_id, false) - proceed = false - @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 + old_answer = @answer.plan.answer(@answer.question_id, false) + proceed = false + @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 + 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 - - 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 - end - if proceed - respond_to do |format| - if @answer.save - format.html { redirect_to :back, status: :found, notice: I18n.t('helpers.project.answer_recorded') } - else - format.html { redirect_to :back, notice: I18n.t('helpers.project.answer_error') } - end - end - else - respond_to do |format| - format.html { redirect_to :back, notice: I18n.t('helpers.project.answer_no_change') } + end + if proceed + respond_to do |format| + if @answer.save + format.html { redirect_to :back, status: :found, notice: I18n.t('helpers.project.answer_recorded') } + else + format.html { redirect_to :back, notice: I18n.t('helpers.project.answer_error') } end end else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) + respond_to do |format| + format.html { redirect_to :back, notice: I18n.t('helpers.project.answer_no_change') } + end end - end + end end \ No newline at end of file diff --git a/app/policies/answer_policy.rb b/app/policies/answer_policy.rb new file mode 100644 index 0000000..d3b8d02 --- /dev/null +++ b/app/policies/answer_policy.rb @@ -0,0 +1,14 @@ +class AnswerPolicy < ApplicationPolicy + attr_reader :user + + def initialize(user, answer) + raise Pundit::NotAuthorizedError, "must be logged in" unless user + @user = user + @answer = answer + end + + def create? + @answer.plan.editable_by(@user.id) + end + +end \ No newline at end of file