use .js response convention for remote likeke behavior
This commit is contained in:
parent
eadede966f
commit
dfeb4f3a13
14 changed files with 60 additions and 85 deletions
|
|
@ -1,9 +1,10 @@
|
|||
|
||||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
class LikesController < ApplicationController
|
||||
include ApplicationHelper
|
||||
helper :likes
|
||||
before_filter :authenticate_user!
|
||||
|
||||
respond_to :html, :mobile, :json
|
||||
|
|
@ -19,18 +20,7 @@ class LikesController < ApplicationController
|
|||
Postzord::Dispatch.new(current_user, @like).post
|
||||
|
||||
respond_to do |format|
|
||||
format.js {
|
||||
json = { :post_id => @like.post_id,
|
||||
:html => render_to_string(
|
||||
:partial => 'likes/likes',
|
||||
:locals => {
|
||||
:likes => @like.post.likes,
|
||||
:dislikes => @like.post.dislikes
|
||||
}
|
||||
)
|
||||
}
|
||||
render(:json => json, :status => 201)
|
||||
}
|
||||
format.js { render :status => 201 }
|
||||
format.html { render :nothing => true, :status => 201 }
|
||||
format.mobile { redirect_to status_message_path(@like.post_id) }
|
||||
end
|
||||
|
|
@ -45,10 +35,6 @@ class LikesController < ApplicationController
|
|||
def destroy
|
||||
if @like = Like.where(:post_id => params[:post_id], :author_id => current_user.person.id).first
|
||||
current_user.retract(@like)
|
||||
respond_to do |format|
|
||||
format.mobile{ redirect_to @like.post }
|
||||
format.js {render :nothing => true, :status => 204}
|
||||
end
|
||||
else
|
||||
respond_to do |format|
|
||||
format.mobile {redirect_to :back}
|
||||
|
|
@ -56,5 +42,4 @@ class LikesController < ApplicationController
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
class PeopleController < ApplicationController
|
||||
helper :comments
|
||||
helper :comments, :likes
|
||||
before_filter :authenticate_user!, :except => [:show]
|
||||
|
||||
respond_to :html
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
class SocketsController < ApplicationController
|
||||
helper :comments
|
||||
helper :comments, :likes
|
||||
include ApplicationHelper
|
||||
include SocketsHelper
|
||||
include Rails.application.routes.url_helpers
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
class StatusMessagesController < ApplicationController
|
||||
helper :comments
|
||||
helper :comments, :likes
|
||||
before_filter :authenticate_user!
|
||||
|
||||
respond_to :html
|
||||
|
|
|
|||
|
|
@ -3,8 +3,16 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
module LikesHelper
|
||||
def likes_list likes
|
||||
def likes_list(likes)
|
||||
links = likes.collect { |like| link_to "#{h(like.author.name.titlecase)}", person_path(like.author) }
|
||||
links.join(", ").html_safe
|
||||
end
|
||||
|
||||
def like_action(post)
|
||||
if current_user.liked?(post)
|
||||
link_to t('shared.stream_element.unlike'), like_path(:post_id => post.id, :id => 'xxx'), :method => :delete, :class => 'unlike', :remote => true
|
||||
else
|
||||
link_to t('shared.stream_element.like'), likes_path(:positive => 'true', :post_id => post.id ), :method => :post, :class => 'like', :remote => true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,15 +3,10 @@
|
|||
-# the COPYRIGHT file.
|
||||
|
||||
- if likes.size > 0
|
||||
.likes
|
||||
= image_tag('icons/happy_smiley.png')
|
||||
= link_to t('.people_like_this', :count => likes.length), "#", :class => "expand_likes"
|
||||
%span.hidden.likes_list
|
||||
= likes_list(likes)
|
||||
.likes_container
|
||||
.likes
|
||||
= image_tag('icons/happy_smiley.png')
|
||||
= link_to t('.people_like_this', :count => likes.length), "#", :class => "expand_likes"
|
||||
%span.hidden.likes_list
|
||||
= likes_list(likes)
|
||||
|
||||
/- if dislikes.length > 0
|
||||
/ .dislikes
|
||||
/ = image_tag('icons/sad_smiley.png')
|
||||
/ = link_to t('.people_dislike_this', :count => dislikes.length), "#", :class => "expand_dislikes"
|
||||
/ %span.hidden.dislikes_list
|
||||
/ = likes_list(dislikes)
|
||||
|
|
|
|||
4
app/views/likes/create.js.erb
Normal file
4
app/views/likes/create.js.erb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$(".like_action", ".stream_element[data-guid=<%=@like.post_id%>]").html("<%= escape_javascript(like_action(@like.post))%>");
|
||||
|
||||
WebSocketReceiver.processLike("<%=@like.post_id%>", "<%= escape_javascript(render("likes/likes", :post_id => @like.post_id, :likes => @like.post.likes, :dislikes => @like.post.dislikes)) %>");
|
||||
|
||||
3
app/views/likes/destroy.js.erb
Normal file
3
app/views/likes/destroy.js.erb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$(".like_action", ".stream_element[data-guid=<%=@like.post_id%>]").html("<%= escape_javascript(like_action(@like.post))%>");
|
||||
WebSocketReceiver.processLike("<%=@like.post_id%>", "<%= escape_javascript(render("likes/likes", :post_id => @like.post_id, :likes => @like.post.likes, :dislikes => @like.post.dislikes)) %>");
|
||||
|
||||
|
|
@ -37,20 +37,14 @@
|
|||
= link_to(how_long_ago(post), status_message_path(post))
|
||||
|
||||
- unless (defined?(@commenting_disabled) && @commenting_disabled)
|
||||
|
|
||||
%span.like_action
|
||||
= like_action(post)
|
||||
|
|
||||
= link_to t('comments.new_comment.comment'), '#', :class => 'focus_comment_textarea'
|
||||
|
||||
- if defined?(current_user)
|
||||
%span.like_links
|
||||
|
|
||||
- if !current_user.liked?(post)
|
||||
= link_to t('.like'), likes_path(:positive => 'true', :post_id => post.id ), :method => :post, :class => "like_it", :remote => true
|
||||
- else
|
||||
= link_to t('.unlike'), like_path(:post_id => post.id, :id => 'xxx'), :method => :delete, :class => "like_it", :remote => true
|
||||
|
||||
/|
|
||||
/= link_to t('.dislike'), like_path(:positive => 'false', :post_id => post.id), :method => :post, :class => "dislike_it", :remote => true
|
||||
|
||||
.likes_container
|
||||
= render "likes/likes", :post_id => post.id, :likes => post.likes, :dislikes => post.dislikes, :current_user => current_user
|
||||
.likes
|
||||
- if post.likes.count > 0
|
||||
= render "likes/likes", :post_id => post.id, :likes => post.likes, :dislikes => post.dislikes, :current_user => current_user
|
||||
|
||||
= render "comments/comments", :post => post, :comments => post.comments, :current_user => current_user, :condensed => true, :commenting_disabled => (defined?(@commenting_disabled) && @commenting_disabled)
|
||||
|
|
|
|||
|
|
@ -161,7 +161,7 @@ en:
|
|||
many: "%{count} comments"
|
||||
other: "%{count} comments"
|
||||
new_comment:
|
||||
comment: "comment"
|
||||
comment: "Comment"
|
||||
commenting: "Commenting..."
|
||||
|
||||
contacts:
|
||||
|
|
|
|||
|
|
@ -92,12 +92,6 @@ var Stream = {
|
|||
$(this).parent().fadeOut('fast');
|
||||
});
|
||||
|
||||
likes.live('ajax:success', function(data, json, xhr) {
|
||||
$(this).parent().detach();
|
||||
json = $.parseJSON(json);
|
||||
WebSocketReceiver.processLike(json.post_id, json.html);
|
||||
});
|
||||
|
||||
likes.live('ajax:failure', function(data, html, xhr) {
|
||||
Diaspora.widgets.alert.alert('Failed to like/dislike!');
|
||||
$(this).parent().fadeIn('fast');
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ var WebSocketReceiver = {
|
|||
|
||||
processLike: function(postId, html) {
|
||||
var post = $("*[data-guid='"+postId+"']");
|
||||
$(".likes_container", post).fadeOut('fast').html(html).fadeIn('fast');
|
||||
$('.likes', post).html(html);
|
||||
},
|
||||
|
||||
processPost: function(className, postId, html, aspectIds) {
|
||||
|
|
|
|||
|
|
@ -571,8 +571,7 @@ header
|
|||
|
||||
ul.comments,
|
||||
ul.show_comments,
|
||||
div.likes,
|
||||
div.dislikes
|
||||
.likes_container
|
||||
:margin 0
|
||||
:top 0.5em
|
||||
:padding 0
|
||||
|
|
@ -2280,22 +2279,31 @@ h3,h4
|
|||
:position relative
|
||||
:z-index 10
|
||||
|
||||
ul.show_comments,
|
||||
div.likes,
|
||||
div.dislikes
|
||||
|
||||
ul.show_comments
|
||||
:margin
|
||||
:bottom -0.5em
|
||||
> li
|
||||
|
||||
.likes_container
|
||||
:margin
|
||||
:bottom -4px
|
||||
:padding 4px
|
||||
|
||||
|
||||
ul.show_comments,
|
||||
.likes_container
|
||||
> *
|
||||
:font
|
||||
:size smaller
|
||||
img
|
||||
:position relative
|
||||
:top 3px
|
||||
:height 12px
|
||||
:width 12px
|
||||
:margin
|
||||
:left 0.5em
|
||||
:right 0.5em
|
||||
:weight bold
|
||||
|
||||
img
|
||||
:position relative
|
||||
:top 3px
|
||||
:height 12px
|
||||
:width 12px
|
||||
:margin
|
||||
:left 0.5em
|
||||
|
||||
.mark_all_read
|
||||
:position relative
|
||||
|
|
@ -2821,22 +2829,6 @@ h1.tag
|
|||
:background
|
||||
:color rgb(245,245,245)
|
||||
|
||||
.likes_container
|
||||
.likes,
|
||||
.dislikes
|
||||
:border-bottom 1px solid white
|
||||
a
|
||||
:padding 1px
|
||||
:vertical-align middle
|
||||
:font-size 11px
|
||||
img
|
||||
:position relative
|
||||
:width 14px
|
||||
:height 14px
|
||||
:margin-left 5px
|
||||
:top 2px
|
||||
|
||||
|
||||
#contacts_of_contact
|
||||
.section
|
||||
:margin
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ describe LikesController do
|
|||
it 'lets a user destroy their like' do
|
||||
alice.should_receive(:retract).with(@like)
|
||||
delete :destroy, :format => "js", :post_id => @like.post_id, :id => @like.id
|
||||
response.status.should == 204
|
||||
response.should be_success
|
||||
end
|
||||
|
||||
it 'does not let a user destroy other likes' do
|
||||
|
|
|
|||
Loading…
Reference in a new issue