From 70feaf07f64574a1775ece20e197dcc4bb257ed2 Mon Sep 17 00:00:00 2001 From: Rei Date: Tue, 14 Jan 2014 22:41:19 +0800 Subject: [PATCH] posts#vote action --- app/controllers/posts_controller.rb | 14 ++++++++++++++ app/models/post.rb | 1 + app/models/post_vote.rb | 13 ++++++++++--- config/routes.rb | 4 ++++ test/controllers/posts_controller_test.rb | 14 ++++++++++++++ test/factories/post_votes.rb | 1 + test/models/post_vote_test.rb | 6 ++++++ 7 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 60c88d7..14519ea 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -40,6 +40,20 @@ class PostsController < ApplicationController render layout: false end + def vote + @post = Post.find_by! id: params[:id] + if PostVote::VALUE.keys.include? params[:type] + post_vote = @post.post_votes.find_or_initialize_by(user_id: current_user.id) + post_vote.update_attribute :value, params[:type] + elsif params[:type] == 'cancel' + @post.post_votes.find_by(user_id: current_user.id).try(:destroy) + end + + respond_to do |format| + format.js { render :show } + end + end + private def post_params diff --git a/app/models/post.rb b/app/models/post.rb index 45a8b4a..55c01da 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,6 +1,7 @@ class Post < ActiveRecord::Base belongs_to :topic belongs_to :user + has_many :post_votes after_create :get_post_number diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index ad27289..b43cceb 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -5,11 +5,18 @@ class PostVote < ActiveRecord::Base validates :value, presence: true - after_save :update_post_votes + after_create do + post.increment! :votes, VALUE[value] + end - def update_post_votes + after_update do if value_changed? - post.increment! :votes, VALUE[value] + # TODO value and value_was type is different, maybe change later. + post.increment! :votes, (VALUE[value] - value_was) end end + + after_destroy do + post.decrement! :votes, VALUE[value] + end end diff --git a/config/routes.rb b/config/routes.rb index 2d1068f..d1b69d2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,5 +19,9 @@ Rails.application.routes.draw do collection do post 'preview' end + + member do + patch :vote + end end end diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb index a2bb696..5810bc1 100644 --- a/test/controllers/posts_controller_test.rb +++ b/test/controllers/posts_controller_test.rb @@ -34,4 +34,18 @@ class PostsControllerTest < ActionController::TestCase assert_response :success, @response.body assert_equal 'change', topic_post.reload.content end + + test "should vote post" do + topic_post = create(:post) + assert_require_logined do + xhr :patch, :vote, id: topic_post, type: 'up' + end + assert_equal 1, topic_post.reload.votes + + xhr :patch, :vote, id: topic_post, type: 'down' + assert_equal(-1, topic_post.reload.votes) + + xhr :patch, :vote, id: topic_post, type: 'cancel' + assert_equal(0, topic_post.reload.votes) + end end diff --git a/test/factories/post_votes.rb b/test/factories/post_votes.rb index 8b1a4b5..fe94654 100644 --- a/test/factories/post_votes.rb +++ b/test/factories/post_votes.rb @@ -2,5 +2,6 @@ FactoryGirl.define do factory :post_vote do user post + value 'up' end end diff --git a/test/models/post_vote_test.rb b/test/models/post_vote_test.rb index 3119ff7..e30e516 100644 --- a/test/models/post_vote_test.rb +++ b/test/models/post_vote_test.rb @@ -26,6 +26,12 @@ class PostVoteTest < ActiveSupport::TestCase assert_equal 1, post.votes vote.update_attribute :value, 'down' + assert_equal(-1, post.votes) + + vote.update_attribute :value, 'up' + assert_equal 1, post.votes + + vote.destroy assert_equal 0, post.votes end end