display proper counts after feedback actions; modified destroy responses to return PostPresenter objects

This commit is contained in:
danielgrippi 2012-02-21 21:44:44 -08:00
parent 37981b71fe
commit 899136d0e5
7 changed files with 30 additions and 12 deletions

View file

@ -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|

View file

@ -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|

View file

@ -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

View file

@ -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)
}});
}
});

View file

@ -10,6 +10,7 @@
{{else}}
<i class="icon-heart icon-white"></i>
{{/if}}
{{likes_count}}
</a>
<a href="#" class="label follow" title="{{#if user_participation}} Stop Following {{else}} Follow {{/if}} Post">
@ -18,6 +19,7 @@
{{else}}
<i class="icon-plus icon-white"></i>
{{/if}}
{{participations_count}}
</a>
{{#if userCanReshare}}
@ -27,9 +29,11 @@
{{else}}
<i class="icon-retweet icon-white"></i>
{{/if}}
{{reshares_count}}
</a>
{{/if}}
<a href="#" class="label comment" title="Comment">
<i class="icon-comment icon-white"></i>
{{comments_count}}
</a>

View file

@ -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

View file

@ -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