comment updates
This commit is contained in:
parent
da5dc4bd94
commit
39ba1a8369
@ -1,26 +1,37 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
module Admin
|
module Admin
|
||||||
class CommentController < AdminController
|
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
|
def create
|
||||||
|
authorize QuizComment
|
||||||
comment = QuizComment.create(
|
comment = QuizComment.create(
|
||||||
comment_params.merge(user_id: current_user.id, test_hash: params[:test_hash])
|
comment_params.merge(user_id: current_user.id, test_hash: params[:test_hash])
|
||||||
)
|
)
|
||||||
|
|
||||||
if comment.persisted?
|
flash_message = if comment.persisted?
|
||||||
redirect_to admin_result_path(params[:test_hash]),
|
{ success: "Sucessfully created comment" }
|
||||||
flash: { success: "Sucessfully created comment" }
|
|
||||||
else
|
else
|
||||||
redirect_to admin_result_path(params[:test_hash]),
|
{ error: "Failed to save comment" }
|
||||||
flash: { error: "Failed to save comment" }
|
|
||||||
end
|
end
|
||||||
|
redirect_to admin_result_path(params[:test_hash]), flash: flash_message
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def comment_params
|
def comment_params
|
||||||
params.require(:quiz_comment).permit(:message, :id)
|
params.require(:quiz_comment).permit(:message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<%= render partial: 'shared/form_model_errors', locals: { obj: @comment } %>
|
<%= 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_for @comment, url: admin_create_comment_path(test_hash), method: :post do |form| %>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<%= form.label :message, "Comment" %>
|
<%= form.label :message, "Comment" %>
|
||||||
|
@ -52,6 +52,7 @@ Rails.application.routes.draw do
|
|||||||
get "/admin/results", to: "admin/result#index", as: :admin_results
|
get "/admin/results", to: "admin/result#index", as: :admin_results
|
||||||
get "/admin/result/:test_hash", to: "admin/result#view", as: :admin_result
|
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
|
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/up", to: "admin/vote#up", as: :admin_up_vote, defaults: { format: 'json' }
|
||||||
|
@ -3,15 +3,26 @@ require 'test_helper'
|
|||||||
|
|
||||||
module Admin
|
module Admin
|
||||||
class CommentControllerTest < ActionDispatch::IntegrationTest
|
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
|
test "should post create" do
|
||||||
auth_reviewer
|
auth_reviewer
|
||||||
candidate = candidates(:stacy)
|
candidate = candidates(:stacy)
|
||||||
|
|
||||||
assert_difference("QuizComment.count") do
|
assert_difference("QuizComment.count") do
|
||||||
post admin_create_comment_url(test_hash: candidate.test_hash), params: { quiz_comment: {
|
post admin_create_comment_url(test_hash: candidate.test_hash),
|
||||||
message: 'this is a test comment'
|
params: { quiz_comment: { message: 'this is a test comment' } }
|
||||||
} }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_redirected_to admin_result_url(test_hash: candidate.test_hash)
|
assert_redirected_to admin_result_url(test_hash: candidate.test_hash)
|
||||||
assert flash[:success]
|
assert flash[:success]
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user