From da5dc4bd94a44a53616e7aeae2e98785c617519e Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Mon, 13 Feb 2017 15:54:57 -0600 Subject: [PATCH] comment creation --- app/controllers/admin/comment_controller.rb | 26 +++++++++++++++++++ app/views/admin/result/_comment_form.html.erb | 2 +- app/views/admin/result/view.html.erb | 4 +-- config/routes.rb | 2 ++ .../admin/comment_controller_test.rb | 19 ++++++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/comment_controller.rb create mode 100644 test/controllers/admin/comment_controller_test.rb diff --git a/app/controllers/admin/comment_controller.rb b/app/controllers/admin/comment_controller.rb new file mode 100644 index 0000000..d02ee24 --- /dev/null +++ b/app/controllers/admin/comment_controller.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true +module Admin + class CommentController < AdminController + before_action { authorize QuizComment } + + def create + 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 + end + + private + + def comment_params + params.require(:quiz_comment).permit(:message, :id) + end + end +end diff --git a/app/views/admin/result/_comment_form.html.erb b/app/views/admin/result/_comment_form.html.erb index 8867b8e..d4c1b55 100644 --- a/app/views/admin/result/_comment_form.html.erb +++ b/app/views/admin/result/_comment_form.html.erb @@ -1,6 +1,6 @@ <%= render partial: 'shared/form_model_errors', locals: { obj: @comment } %> -<%= form_for @comment, url: "#", method: :post do |form| %> +<%= form_for @comment, url: admin_create_comment_path(test_hash), method: :post do |form| %>
<%= form.label :message, "Comment" %> <%= form.text_area :message %> diff --git a/app/views/admin/result/view.html.erb b/app/views/admin/result/view.html.erb index 3b1abaa..b07c69a 100644 --- a/app/views/admin/result/view.html.erb +++ b/app/views/admin/result/view.html.erb @@ -45,7 +45,7 @@

Comments

- <%= render partial: 'comment', collection: @comments %> -
<%= render partial: 'comment_form' %>
+ <%= render partial: 'comment', collection: @comments, locals: { test_hash: @candidate.test_hash } %> +
<%= render partial: 'comment_form', locals: { test_hash: @candidate.test_hash } %>
diff --git a/config/routes.rb b/config/routes.rb index e7a7558..edff83f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,6 +52,8 @@ 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", 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' } get "admin/vote/:test_hash/down", to: "admin/vote#down", as: :admin_down_vote, defaults: { format: 'json' } get "admin/vote/:test_hash/approve", to: "admin/vote#approve", as: :admin_approve_vote, defaults: { format: 'json' } diff --git a/test/controllers/admin/comment_controller_test.rb b/test/controllers/admin/comment_controller_test.rb new file mode 100644 index 0000000..d900269 --- /dev/null +++ b/test/controllers/admin/comment_controller_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true +require 'test_helper' + +module Admin + class CommentControllerTest < ActionDispatch::IntegrationTest + 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' + } } + end + assert_redirected_to admin_result_url(test_hash: candidate.test_hash) + assert flash[:success] + end + end +end