Skip to content

Commit

Permalink
增加 liked_count
Browse files Browse the repository at this point in the history
  • Loading branch information
windy committed Mar 31, 2014
1 parent 0e523d6 commit bbb3f21
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/assets/javascripts/angularjs/likes.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
$http.post url
.success (res)->
if res.success
$scope.count += 1
$scope.like = $cookies.like = res.id
$scope.count = res.count

$scope.cancel = ->
$http.delete url + "/" + $scope.like
$http.delete url + "/" + $scope.like, (res)->
$scope.count = res.count
# anyway, clear cookie
delete $cookies["like"]
$scope.like = null
$scope.get_count()
2 changes: 1 addition & 1 deletion app/controllers/archives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def build_summary(post)
type: post.type,
created_at: format_date(post.created_at),
id: post.id.to_s,
liked_count: post.likes.size,
liked_count: post.liked_count,
visited_count: post.visited_count,
labels: post.labels_content
}
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ class LikesController < ApplicationController

def index
post = Post.find( params[:blog_id] )
render :json=> { success: true, count: post.likes.size }
render :json=> { success: true, count: post.liked_count }
end

def create
post = Post.find( params[:blog_id] )
like = Like.new
like.post = post
if like.save
render :json=> { success: true, id: like.id.to_s }
render :json=> { success: true, id: like.id.to_s, count: post.liked_count }
else
render :json=> { success: false }
render :json=> { success: false, count: post.liked_count }
end
end

def destroy
post = Post.find( params[:blog_id] )
like = post.likes.find(params[:id])
if like.destroy
render :json=> { success: true }
render :json=> { success: true, count: post.liked_count }
else
render :json=> { success: false }
render :json=> { success: false, count: post.liked_count }
end
end
end
4 changes: 4 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ def sub_content
def labels_content
self.labels.collect { |label| label.name }.join(", ")
end

def liked_count
self.likes.size
end
end

0 comments on commit bbb3f21

Please sign in to comment.