diff --git a/app/controllers/comment_votes_controller.rb b/app/controllers/comment_votes_controller.rb index bea09d334..0402c2408 100644 --- a/app/controllers/comment_votes_controller.rb +++ b/app/controllers/comment_votes_controller.rb @@ -2,20 +2,17 @@ class CommentVotesController < ApplicationController respond_to :js, :json, :xml before_action :member_only skip_before_action :api_check + rescue_with CommentVote::Error, ActiveRecord::RecordInvalid, status: 422 def create @comment = Comment.find(params[:comment_id]) @comment_vote = @comment.vote!(params[:score]) - rescue CommentVote::Error, ActiveRecord::RecordInvalid => x - @error = x - render status: 422 + respond_with(@comment) end def destroy @comment = Comment.find(params[:comment_id]) @comment.unvote! - rescue CommentVote::Error => x - @error = x - render status: 422 + respond_with(@comment) end end diff --git a/app/views/comment_votes/create.js.erb b/app/views/comment_votes/create.js.erb index 1bf69a8af..966e785c4 100644 --- a/app/views/comment_votes/create.js.erb +++ b/app/views/comment_votes/create.js.erb @@ -1,8 +1,4 @@ -<% if @error %> - Danbooru.error("<%= j @error.to_s %>"); -<% elsif @comment_vote.errors.any? %> - Danbooru.error("<%= j @comment_vote.errors.full_messages.join('; ') %>"); -<% elsif @comment_vote.is_negative? %> +<% if @comment_vote.is_negative? %> $(".comment[data-comment-id=<%= @comment.id %>]").remove(); <% end %> diff --git a/app/views/comment_votes/create.json.erb b/app/views/comment_votes/create.json.erb deleted file mode 100644 index 313a907c0..000000000 --- a/app/views/comment_votes/create.json.erb +++ /dev/null @@ -1,7 +0,0 @@ -<% if @error %> - {"success": false, "errors": <%= @error.to_s.to_json.html_safe %>} -<% elsif @comment_vote.errors.any? %> - {"success": false, "errors": <%= @comment_vote.errors.full_messages.to_json.html_safe %>} -<% else %> - {"success": true} -<% end %> diff --git a/app/views/comment_votes/create.xml.erb b/app/views/comment_votes/create.xml.erb deleted file mode 100644 index 4bdbca812..000000000 --- a/app/views/comment_votes/create.xml.erb +++ /dev/null @@ -1,6 +0,0 @@ - -<% if @comment_vote.errors.any? %> - <%= @comment_vote.errors.full_messages.to_json.html_safe %> -<% else %> - -<% end %> diff --git a/app/views/comment_votes/destroy.js.erb b/app/views/comment_votes/destroy.js.erb index 754b5568a..a1f4739a7 100644 --- a/app/views/comment_votes/destroy.js.erb +++ b/app/views/comment_votes/destroy.js.erb @@ -1,7 +1,3 @@ -<% if @error %> - Danbooru.error("<%= j @error.to_s %>"); -<% else %> - $("#comment-vote-up-link-for-<%= @comment.id %>").show(); - $("#comment-vote-down-link-for-<%= @comment.id %>").show(); - $("#comment-unvote-link-for-<%= @comment.id %>").hide(); -<% end %> +$("#comment-vote-up-link-for-<%= @comment.id %>").show(); +$("#comment-vote-down-link-for-<%= @comment.id %>").show(); +$("#comment-unvote-link-for-<%= @comment.id %>").hide(); diff --git a/app/views/comment_votes/destroy.json.erb b/app/views/comment_votes/destroy.json.erb deleted file mode 100644 index 052990686..000000000 --- a/app/views/comment_votes/destroy.json.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% if @error %> - {"success": false, "reason": <%= @error.to_s.to_json.html_safe %>} -<% else %> - {"success": true} -<% end %> diff --git a/app/views/comment_votes/destroy.xml.erb b/app/views/comment_votes/destroy.xml.erb deleted file mode 100644 index 46666469e..000000000 --- a/app/views/comment_votes/destroy.xml.erb +++ /dev/null @@ -1,6 +0,0 @@ - -<% if @error %> - <%= @error.to_s %> -<% else %> - -<% end %> diff --git a/test/functional/comment_votes_controller_test.rb b/test/functional/comment_votes_controller_test.rb index b62bcd063..abad8c93d 100644 --- a/test/functional/comment_votes_controller_test.rb +++ b/test/functional/comment_votes_controller_test.rb @@ -17,18 +17,24 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest context "#create.json" do should "create a vote" do assert_difference("CommentVote.count", 1) do - post_auth comment_votes_path(comment_id: @comment.id, format: "json"), @user + post_auth comment_votes_path(comment_id: @comment.id, score: "down", format: "json"), @user assert_response :success - assert_equal("{\"success\": true}", @response.body.strip) + + comment = JSON.parse(@response.body) + assert_equal(@comment.id, comment["id"]) + assert_equal(-1, comment["score"]) end end should "fail silently on errors" do create(:comment_vote, comment: @comment, score: -1) assert_difference("CommentVote.count", 0) do - post_auth comment_votes_path(comment_id: @comment.id, score: "-1", format: "json"), @user + post_auth comment_votes_path(comment_id: @comment.id, score: "down", format: "json"), @user assert_response 422 - assert_equal("{\"success\": false, \"errors\": \"Validation failed: You have already voted for this comment\"}", @response.body.strip) + + comment = JSON.parse(@response.body) + assert_equal(false, comment["success"]) + assert_equal("Validation failed: You have already voted for this comment", comment["message"]) end end end @@ -36,7 +42,7 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest context "#create.js" do should "create a vote" do assert_difference("CommentVote.count", 1) do - post_auth comment_votes_path(comment_id: @comment.id, format: "json", score: 1), @user + post_auth comment_votes_path(comment_id: @comment.id, format: "json", score: "down"), @user assert_response :success end end @@ -44,7 +50,7 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest should "fail on errors" do create(:comment_vote, :comment => @comment, :score => -1) assert_difference("CommentVote.count", 0) do - post_auth comment_votes_path(comment_id: @comment.id, :score => -1, format: "js"), @user + post_auth comment_votes_path(comment_id: @comment.id, :score => "down", format: "js"), @user assert_response 422 end end