From 39ba1a8369fbfef8d4c72dff8b8a508279b98fe5 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Mon, 13 Feb 2017 16:12:59 -0600 Subject: [PATCH] comment updates --- app/controllers/admin/comment_controller.rb | 29 +++++++++++++------ app/views/admin/result/_comment_form.html.erb | 1 + config/routes.rb | 1 + .../admin/comment_controller_test.rb | 17 +++++++++-- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/comment_controller.rb b/app/controllers/admin/comment_controller.rb index d02ee24..cef5cff 100644 --- a/app/controllers/admin/comment_controller.rb +++ b/app/controllers/admin/comment_controller.rb @@ -1,26 +1,37 @@ # frozen_string_literal: true module Admin class CommentController < AdminController - before_action { authorize QuizComment } + def update + comment = QuizComment.find_by(id: params[:id], test_hash: params[:test_hash]) + authorize comment + + comment.update(comment_params) + flash_message = if comment.save + { success: "Sucessfully updated comment" } + else + { error: "Failed to update comment" } + end + redirect_to admin_result_path(params[:test_hash]), flash: flash_message + end def create + authorize QuizComment comment = QuizComment.create( comment_params.merge(user_id: current_user.id, test_hash: params[:test_hash]) ) - if comment.persisted? - redirect_to admin_result_path(params[:test_hash]), - flash: { success: "Sucessfully created comment" } - else - redirect_to admin_result_path(params[:test_hash]), - flash: { error: "Failed to save comment" } - end + flash_message = if comment.persisted? + { success: "Sucessfully created comment" } + else + { error: "Failed to save comment" } + end + redirect_to admin_result_path(params[:test_hash]), flash: flash_message end private def comment_params - params.require(:quiz_comment).permit(:message, :id) + params.require(:quiz_comment).permit(:message) end end end diff --git a/app/views/admin/result/_comment_form.html.erb b/app/views/admin/result/_comment_form.html.erb index d4c1b55..c2ab89c 100644 --- a/app/views/admin/result/_comment_form.html.erb +++ b/app/views/admin/result/_comment_form.html.erb @@ -1,5 +1,6 @@ <%= render partial: 'shared/form_model_errors', locals: { obj: @comment } %> +<% # admin_update_comment_path # admin_create_comment_path %> <%= form_for @comment, url: admin_create_comment_path(test_hash), method: :post do |form| %>
<%= form.label :message, "Comment" %> diff --git a/config/routes.rb b/config/routes.rb index edff83f..c806362 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,6 +52,7 @@ Rails.application.routes.draw do get "/admin/results", to: "admin/result#index", as: :admin_results get "/admin/result/:test_hash", to: "admin/result#view", as: :admin_result + post "/admin/comment/:test_hash/:id", to: "admin/comment#update", as: :admin_update_comment post "/admin/comment/:test_hash", to: "admin/comment#create", as: :admin_create_comment get "admin/vote/:test_hash/up", to: "admin/vote#up", as: :admin_up_vote, defaults: { format: 'json' } diff --git a/test/controllers/admin/comment_controller_test.rb b/test/controllers/admin/comment_controller_test.rb index d900269..d864127 100644 --- a/test/controllers/admin/comment_controller_test.rb +++ b/test/controllers/admin/comment_controller_test.rb @@ -3,15 +3,26 @@ require 'test_helper' module Admin class CommentControllerTest < ActionDispatch::IntegrationTest + test "should post update" do + auth_manager + comment = quiz_comments(:com5) + post admin_update_comment_url(test_hash: comment.test_hash, id: comment.id), + params: { quiz_comment: { message: 'updated comment' } } + + assert_redirected_to admin_result_url(test_hash: comment.test_hash) + assert flash[:success] + refute_equal comment.message, QuizComment.find_by(id: comment.id).message + end + test "should post create" do auth_reviewer candidate = candidates(:stacy) assert_difference("QuizComment.count") do - post admin_create_comment_url(test_hash: candidate.test_hash), params: { quiz_comment: { - message: 'this is a test comment' - } } + post admin_create_comment_url(test_hash: candidate.test_hash), + params: { quiz_comment: { message: 'this is a test comment' } } end + assert_redirected_to admin_result_url(test_hash: candidate.test_hash) assert flash[:success] end