comment updates

This commit is contained in:
Mark Moser 2017-02-13 16:12:59 -06:00
parent da5dc4bd94
commit 39ba1a8369
4 changed files with 36 additions and 12 deletions

View File

@ -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" }
flash_message = if comment.persisted?
{ success: "Sucessfully created comment" }
else
redirect_to admin_result_path(params[:test_hash]),
flash: { error: "Failed to save comment" }
{ 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

View File

@ -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| %>
<div class="form-group">
<%= form.label :message, "Comment" %>

View File

@ -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' }

View File

@ -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