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
|
respond_to :html, :mobile, :json
|
||||||
|
|
||||||
def create
|
def create
|
||||||
positive = (params[:positive] == 'true') ? true : false
|
|
||||||
if target
|
if target
|
||||||
@like = current_user.build_like(:positive => positive, :target => target)
|
@like = current_user.build_like(:target => target)
|
||||||
|
|
||||||
if @like.save
|
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
|
Postzord::Dispatcher.build(current_user, @like).post
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.js { render 'likes/update', :status => 201 }
|
format.js { render 'likes/update', :status => 201 }
|
||||||
format.html { render :nothing => true, :status => 201 }
|
format.html { render :nothing => true, :status => 201 }
|
||||||
format.mobile { redirect_to post_path(@like.post_id) }
|
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
|
end
|
||||||
else
|
else
|
||||||
render :nothing => true, :status => 422
|
render :nothing => true, :status => 422
|
||||||
|
|
@ -52,7 +51,11 @@ class LikesController < ApplicationController
|
||||||
if target
|
if target
|
||||||
@likes = target.likes.includes(:author => :profile)
|
@likes = target.likes.includes(:author => :profile)
|
||||||
@people = @likes.map{|x| x.author}
|
@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
|
else
|
||||||
render :nothing => true, :status => 404
|
render :nothing => true, :status => 404
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ class Comment < ActiveRecord::Base
|
||||||
t.add :created_at
|
t.add :created_at
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
xml_attr :text
|
xml_attr :text
|
||||||
xml_attr :diaspora_handle
|
xml_attr :diaspora_handle
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,15 @@ class Like < ActiveRecord::Base
|
||||||
xml_attr :target_type
|
xml_attr :target_type
|
||||||
include Diaspora::Relayable
|
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 :positive
|
||||||
xml_attr :diaspora_handle
|
xml_attr :diaspora_handle
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,11 +67,11 @@
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<% if(user_like) { %>
|
<% 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
|
Unlike
|
||||||
</a>
|
</a>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<a href="/posts/<%= id %>/likes?positive=true" class="like_action like" rel='nofollow'>
|
<a href="#" class="like_action like" rel='nofollow'>
|
||||||
Like
|
Like
|
||||||
</a>
|
</a>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
App.Models.Like = Backbone.Model.extend({
|
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({
|
App.Models.Post = Backbone.Model.extend({
|
||||||
url: function(){
|
url: function(){
|
||||||
return "/posts/" + this.get("id");
|
return "/posts/" + this.id;
|
||||||
},
|
},
|
||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.comments = new App.Collections.Comments(this.get("last_three_comments"));
|
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(){
|
createdAt: function(){
|
||||||
|
|
|
||||||
|
|
@ -68,20 +68,9 @@ App.Views.Post = App.Views.StreamObject.extend({
|
||||||
var link = $(evt.target);
|
var link = $(evt.target);
|
||||||
|
|
||||||
if(link.hasClass('like')) {
|
if(link.hasClass('like')) {
|
||||||
this.model.likes.create({
|
this.model.likes.create();
|
||||||
target_id: this.model.get("id"),
|
} else {
|
||||||
target_type: "post",
|
this.model.likes.get(link.data("id")).destroy();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue