voting requires a comment - completes #102

This commit is contained in:
Mark Moser 2017-02-27 13:41:46 -06:00
parent 538190b6bf
commit 3f41773c76
11 changed files with 84 additions and 18 deletions

View File

@ -24,6 +24,10 @@
padding-left: 30px;
width: 33%;
> div {
margin-bottom: 30px;
}
.comment-message {
margin-right: 5px;
}

View File

@ -19,6 +19,10 @@ class User < ApplicationRecord
save
end
def commented_on? test_hash
quiz_comments.where(test_hash: test_hash).count.positive?
end
# Voting
# TODO: Refactor this out of User, belongs on ReviewerVote
# ie: cast_yea(candidate, user)

View File

@ -10,7 +10,10 @@ class QuizCommentPolicy < ApplicationPolicy
end
def create?
user.acts_as_reviewer? && record.candidate.reviewers.where(id: user.id).count.positive?
return true if user.acts_as_admin?
user.acts_as_reviewer? &&
record.candidate.reviewers.where(id: user.id).count.positive?
end
def update?

View File

@ -2,20 +2,22 @@
class ReviewerVotePolicy < ApplicationPolicy
# Voting Policy
#
# Only Reviewers, Managers, and Admins, can cast a vote on a quiz result
# Only Reviewers and Managers can cast a vote on a quiz result
#
# Reviewers can vote any quiz they are linked to
# Only Managers, and Admins, can veto a quiz result
def up?
return true if user.acts_as_admin?
return false unless user.commented_on?(record.candidate.test_hash)
return false unless record.candidate.reviewers.include? user
return false if user.admin?
user.acts_as_reviewer?
end
def down?
return true if user.acts_as_admin?
return false unless user.commented_on?(record.candidate.test_hash)
return false unless record.candidate.reviewers.include? user
return false if user.admin?
user.acts_as_reviewer?
end

View File

@ -13,8 +13,6 @@
<strong>Client/Project:</strong> <%= @candidate.project %><br />
<strong>Recruiter Email:</strong> <%= mail_to @candidate.recruiter.name, @candidate.recruiter.email %><br />
</div>
<div><%= render partial: 'voting' %></div>
</div>
<% @quiz.each do |question| %>
@ -44,10 +42,19 @@
</div>
<div class="review-comments">
<div>
<h2 class="prft-heading">Voting</h2>
<div class="review_meta">
<div><%= render partial: 'voting' %></div>
</div>
</div>
<div>
<h2 class="prft-heading">Comments</h2>
<%= render partial: 'comment', collection: @comments, locals: { test_hash: @candidate.test_hash } %>
<% if policy(QuizComment).new? %>
<%= render partial: 'comment_form', locals: {comment: @comment, test_hash: @candidate.test_hash } %>
<% end %>
</div>
</div>
</div>

View File

@ -40,6 +40,19 @@ module Admin
assert flash[:success]
end
test "admin can post comment" do
auth_admin
candidate = candidates(:stacy)
assert_difference("QuizComment.count") do
post admin_create_comment_url(test_hash: candidate.test_hash),
params: { quiz_comment: { message: 'this is an admin comment' } }
end
assert_redirected_to admin_result_url(test_hash: candidate.test_hash)
assert flash[:success]
end
test "should queue emails on create" do
auth_reviewer
candidate = candidates(:stacy)

View File

@ -6,7 +6,7 @@ module Admin
include ActiveJob::TestHelper
test "reviewer can up vote henry" do
auth_user users(:reviewer)
auth_user users(:reviewer2)
henry = candidates(:henry)
assert_difference("Candidate.find(#{henry.id}).votes.yea.count", 1) do
@ -16,7 +16,7 @@ module Admin
end
test "reviewer can down vote henry" do
auth_user users(:reviewer)
auth_user users(:reviewer2)
henry = candidates(:henry)
assert_difference("Candidate.find(#{henry.id}).votes.nay.count", 1) do
@ -26,7 +26,7 @@ module Admin
end
test "reviewer can change vote on henry" do
auth_user users(:reviewer)
auth_user users(:reviewer2)
henry = candidates(:henry)
get admin_up_vote_url(henry.test_hash)
@ -72,5 +72,23 @@ module Admin
data = JSON.parse(response.body)
assert_match 'requested', data["message"]
end
test "reviewer can only vote after commenting" do
auth_user users(:reviewer)
henry = candidates(:henry)
assert_difference("Candidate.find(#{henry.id}).votes.yea.count", 0) do
get admin_up_vote_url(henry.test_hash)
end
post admin_create_comment_url(test_hash: henry.test_hash),
params: { quiz_comment: { message: 'this is a comment to vote' } }
assert_difference("Candidate.find(#{henry.id}).votes.yea.count", 1) do
get admin_up_vote_url(henry.test_hash)
end
assert_response :success
end
end
end

View File

@ -70,3 +70,18 @@ com14:
user: reviewer2
message: Ultricies Vulputate Bibendum Parturient
com15:
test_hash: 6NjnourLE6Y #richard
user: manager
message: gibberish
com16:
test_hash: egPomAuVDeCEp #henry
user: admin
message: word.
com17:
test_hash: 6NjnourLE6Y #richard
user: reviewer
message: more gibberish

View File

@ -9,7 +9,7 @@ class QuizCommentTest < ActiveSupport::TestCase
test "user to comments association" do
manager = users(:manager)
assert_equal 4, manager.quiz_comments.size
assert_equal 5, manager.quiz_comments.size
end
test "candidate to comments association" do

View File

@ -14,8 +14,8 @@ class QuizCommentPolicyTest < PolicyAssertions::Test
assert_permit users(:manager), comment
assert_permit users(:reviewer), comment
assert_permit users(:admin), comment
refute_permit users(:admin), comment
refute_permit users(:recruiter), comment
end

View File

@ -31,8 +31,8 @@ class ReviewerVotePolicyTest < PolicyAssertions::Test
def test_up
assert_permit users(:manager), reviewer_votes(:manager_richard)
assert_permit users(:reviewer), reviewer_votes(:reviewer_richard)
assert_permit users(:admin), reviewer_votes(:manager_henry)
refute_permit users(:admin), reviewer_votes(:manager_henry)
refute_permit users(:recruiter), reviewer_votes(:manager_henry)
refute_permit users(:reviewer), reviewer_votes(:gustov)
refute_permit users(:manager), reviewer_votes(:gustov)
@ -41,8 +41,8 @@ class ReviewerVotePolicyTest < PolicyAssertions::Test
def test_down
assert_permit users(:manager), reviewer_votes(:manager_richard)
assert_permit users(:reviewer), reviewer_votes(:reviewer_richard)
assert_permit users(:admin), reviewer_votes(:manager_henry)
refute_permit users(:admin), reviewer_votes(:manager_henry)
refute_permit users(:recruiter), reviewer_votes(:manager_henry)
refute_permit users(:reviewer), reviewer_votes(:gustov)
refute_permit users(:manager), reviewer_votes(:gustov)