added api response for likes; cleaned up view logic; specify like collection url from parent model (Post)
This commit is contained in:
parent
ae6fa5bebb
commit
d9d878f11f
7 changed files with 27 additions and 33 deletions
|
|
@ -9,19 +9,18 @@ class LikesController < ApplicationController
|
|||
respond_to :html, :mobile, :json
|
||||
|
||||
def create
|
||||
positive = (params[:positive] == 'true') ? true : false
|
||||
if target
|
||||
@like = current_user.build_like(:positive => positive, :target => target)
|
||||
@like = current_user.build_like(:target => target)
|
||||
|
||||
if @like.save
|
||||
Rails.logger.info("event=create type=like user=#{current_user.diaspora_handle} status=success like=#{@like.id} positive=#{positive}")
|
||||
Rails.logger.info("event=create type=like user=#{current_user.diaspora_handle} status=success like=#{@like.id}")
|
||||
Postzord::Dispatcher.build(current_user, @like).post
|
||||
|
||||
respond_to do |format|
|
||||
format.js { render 'likes/update', :status => 201 }
|
||||
format.html { render :nothing => true, :status => 201 }
|
||||
format.mobile { redirect_to post_path(@like.post_id) }
|
||||
format.json { render :json => {"id" => @like.id}, :status => 201 }
|
||||
format.json{ render :json => @like.as_api_response(:backbone), :status => 201 }
|
||||
end
|
||||
else
|
||||
render :nothing => true, :status => 422
|
||||
|
|
@ -52,7 +51,11 @@ class LikesController < ApplicationController
|
|||
if target
|
||||
@likes = target.likes.includes(:author => :profile)
|
||||
@people = @likes.map{|x| x.author}
|
||||
render :layout => false
|
||||
|
||||
respond_to do |format|
|
||||
format.all{ render :layout => false }
|
||||
format.json{ render :json => @likes.as_api_response(:backbone) }
|
||||
end
|
||||
else
|
||||
render :nothing => true, :status => 404
|
||||
end
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ class Comment < ActiveRecord::Base
|
|||
t.add :created_at
|
||||
end
|
||||
|
||||
|
||||
xml_attr :text
|
||||
xml_attr :diaspora_handle
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,15 @@ class Like < ActiveRecord::Base
|
|||
xml_attr :target_type
|
||||
include Diaspora::Relayable
|
||||
|
||||
# NOTE API V1 to be extracted
|
||||
acts_as_api
|
||||
api_accessible :backbone do |t|
|
||||
t.add :id
|
||||
t.add :guid
|
||||
t.add :author
|
||||
t.add :created_at
|
||||
end
|
||||
|
||||
xml_attr :positive
|
||||
xml_attr :diaspora_handle
|
||||
|
||||
|
|
|
|||
|
|
@ -67,11 +67,11 @@
|
|||
</span>
|
||||
|
||||
<% if(user_like) { %>
|
||||
<a href="/posts/<%= id %>/likes/<%= user_like.id %>" class="like_action unlike" rel='nofollow'>
|
||||
<a href="#" class="like_action unlike" data-id="<%= user_like.id %>" rel='nofollow'>
|
||||
Unlike
|
||||
</a>
|
||||
<% } else { %>
|
||||
<a href="/posts/<%= id %>/likes?positive=true" class="like_action like" rel='nofollow'>
|
||||
<a href="#" class="like_action like" rel='nofollow'>
|
||||
Like
|
||||
</a>
|
||||
<% } %>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,2 @@
|
|||
App.Models.Like = Backbone.Model.extend({
|
||||
url: function(){
|
||||
if(this.get("id")) {
|
||||
return "/" + this.get("target_type") + "s/" + this.get("target_id") + "/likes/" + this.get("id");
|
||||
}
|
||||
else {
|
||||
return "/posts/" + this.get("target_id") + "/likes";
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
App.Models.Post = Backbone.Model.extend({
|
||||
url: function(){
|
||||
return "/posts/" + this.get("id");
|
||||
return "/posts/" + this.id;
|
||||
},
|
||||
|
||||
initialize: function() {
|
||||
this.comments = new App.Collections.Comments(this.get("last_three_comments"));
|
||||
this.likes = new App.Collections.Likes();
|
||||
|
||||
this.likes = new App.Collections.Likes(this.get("user_like")); // load in the user like initially
|
||||
this.likes.url = '/posts/' + this.id + '/likes';
|
||||
},
|
||||
|
||||
createdAt: function(){
|
||||
|
|
|
|||
|
|
@ -67,21 +67,10 @@ App.Views.Post = App.Views.StreamObject.extend({
|
|||
|
||||
var link = $(evt.target);
|
||||
|
||||
if(link.hasClass('like')){
|
||||
this.model.likes.create({
|
||||
target_id: this.model.get("id"),
|
||||
target_type: "post",
|
||||
positive: "true"
|
||||
});
|
||||
}
|
||||
else {
|
||||
var like = new App.Models.Like({
|
||||
"id": this.model.get("user_like")["posts"]["id"],
|
||||
target_type: "post",
|
||||
target_id: this.model.get("id")
|
||||
});
|
||||
|
||||
like.destroy();
|
||||
if(link.hasClass('like')) {
|
||||
this.model.likes.create();
|
||||
} else {
|
||||
this.model.likes.get(link.data("id")).destroy();
|
||||
}
|
||||
|
||||
return this;
|
||||
|
|
|
|||
Loading…
Reference in a new issue