diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 766150b8a..071992f39 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -32,8 +32,7 @@ class LikesController < ApplicationController if @like current_user.retract(@like) respond_to do |format| - format.any { } - format.json{ render :json => @like.parent.as_api_response(:backbone), :status => 202 } + format.json { render :json => PostPresenter.new(@like.parent, current_user).to_json, :status => 202 } end else respond_to do |format| diff --git a/app/controllers/participations_controller.rb b/app/controllers/participations_controller.rb index a8b64d244..910fe7d0b 100644 --- a/app/controllers/participations_controller.rb +++ b/app/controllers/participations_controller.rb @@ -30,8 +30,7 @@ class ParticipationsController < ApplicationController if @participation current_user.retract(@participation) respond_to do |format| - format.any { } - format.json{ render :json => @participation.parent.as_api_response(:backbone), :status => 202 } + format.json { render :json => PostPresenter.new(@participation.parent, current_user).to_json, :status => 202 } end else respond_to do |format| diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 21bfdeee8..722ba5854 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -14,6 +14,9 @@ class PostPresenter { :user_like => self.user_like, :user_participation => self.user_participation, + :likes_count => self.post.likes.count, + :participations_count => self.post.participations.count, + :reshares_count => self.post.reshares.count, :user_reshare => self.user_reshare, :next_post => self.next_post_url, :previous_post => self.previous_post_url diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index 24abb9c2e..d2dd9b6a6 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -46,13 +46,14 @@ app.models.Post = Backbone.Model.extend({ }, unfollow : function() { + var self = this; var participationModel = new app.models.Participation(this.get("user_participation")); participationModel.url = this.participations.url + "/" + participationModel.id; - participationModel.destroy(); - this.set({ user_participation : null }); - - this.trigger('feedback', this) + participationModel.destroy({success : function(model, resp){ + self.set(resp.post); + self.trigger('feedback', this) + }}); }, toggleLike : function() { @@ -67,17 +68,19 @@ app.models.Post = Backbone.Model.extend({ like : function() { var self = this; this.likes.create({}, {success : function(resp){ - self.set(resp.attributes.post) + self.set(resp.get("post")) self.trigger('feedback', self) }}); }, unlike : function() { + var self = this; var likeModel = new app.models.Like(this.get("user_like")); likeModel.url = this.likes.url + "/" + likeModel.id; - likeModel.destroy(); - this.set({ user_like : null }); - this.trigger('feedback', this) + likeModel.destroy({success : function(model, resp) { + self.set(resp.post); + self.trigger('feedback', this) + }}); } }); diff --git a/public/javascripts/app/templates/post-viewer/feedback.handlebars b/public/javascripts/app/templates/post-viewer/feedback.handlebars index 5c7548e13..68325ae96 100644 --- a/public/javascripts/app/templates/post-viewer/feedback.handlebars +++ b/public/javascripts/app/templates/post-viewer/feedback.handlebars @@ -10,6 +10,7 @@ {{else}} {{/if}} + {{likes_count}} @@ -18,6 +19,7 @@ {{else}} {{/if}} + {{participations_count}} {{#if userCanReshare}} @@ -27,9 +29,11 @@ {{else}} {{/if}} + {{reshares_count}} {{/if}} + {{comments_count}} diff --git a/spec/controllers/likes_controller_spec.rb b/spec/controllers/likes_controller_spec.rb index 5dc2f79af..7680391ef 100644 --- a/spec/controllers/likes_controller_spec.rb +++ b/spec/controllers/likes_controller_spec.rb @@ -126,6 +126,11 @@ describe LikesController do response.status.should == 403 end + + it 'returns the parent post presenter' do + delete :destroy, :format => :json, id_field => @like.target_id, :id => @like.id + response.body.should == PostPresenter.new(@like.parent, alice).to_json.to_s + end end end end diff --git a/spec/controllers/participations_controller_spec.rb b/spec/controllers/participations_controller_spec.rb index fe2abf7ab..134a003e1 100644 --- a/spec/controllers/participations_controller_spec.rb +++ b/spec/controllers/participations_controller_spec.rb @@ -118,6 +118,11 @@ describe ParticipationsController do response.status.should == 403 end + + it 'returns the parent post presenter' do + delete :destroy, :format => :json, id_field => @participation.target_id, :id => @participation.id + response.body.should == PostPresenter.new(@participation.parent, alice).to_json.to_s + end end end end