use .js response convention for remote likeke behavior

This commit is contained in:
danielgrippi 2011-05-06 15:59:21 -07:00 committed by Raphael Sofaer
parent eadede966f
commit dfeb4f3a13
14 changed files with 60 additions and 85 deletions

View file

@ -1,9 +1,10 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
class LikesController < ApplicationController class LikesController < ApplicationController
include ApplicationHelper include ApplicationHelper
helper :likes
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html, :mobile, :json respond_to :html, :mobile, :json
@ -19,18 +20,7 @@ class LikesController < ApplicationController
Postzord::Dispatch.new(current_user, @like).post Postzord::Dispatch.new(current_user, @like).post
respond_to do |format| respond_to do |format|
format.js { format.js { render :status => 201 }
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.html { render :nothing => true, :status => 201 } format.html { render :nothing => true, :status => 201 }
format.mobile { redirect_to status_message_path(@like.post_id) } format.mobile { redirect_to status_message_path(@like.post_id) }
end end
@ -45,10 +35,6 @@ class LikesController < ApplicationController
def destroy def destroy
if @like = Like.where(:post_id => params[:post_id], :author_id => current_user.person.id).first if @like = Like.where(:post_id => params[:post_id], :author_id => current_user.person.id).first
current_user.retract(@like) current_user.retract(@like)
respond_to do |format|
format.mobile{ redirect_to @like.post }
format.js {render :nothing => true, :status => 204}
end
else else
respond_to do |format| respond_to do |format|
format.mobile {redirect_to :back} format.mobile {redirect_to :back}
@ -56,5 +42,4 @@ class LikesController < ApplicationController
end end
end end
end end
end end

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class PeopleController < ApplicationController class PeopleController < ApplicationController
helper :comments helper :comments, :likes
before_filter :authenticate_user!, :except => [:show] before_filter :authenticate_user!, :except => [:show]
respond_to :html respond_to :html

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class SocketsController < ApplicationController class SocketsController < ApplicationController
helper :comments helper :comments, :likes
include ApplicationHelper include ApplicationHelper
include SocketsHelper include SocketsHelper
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class StatusMessagesController < ApplicationController class StatusMessagesController < ApplicationController
helper :comments helper :comments, :likes
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html respond_to :html

View file

@ -3,8 +3,16 @@
# the COPYRIGHT file. # the COPYRIGHT file.
module LikesHelper 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 = likes.collect { |like| link_to "#{h(like.author.name.titlecase)}", person_path(like.author) }
links.join(", ").html_safe links.join(", ").html_safe
end 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 end

View file

@ -3,15 +3,10 @@
-# the COPYRIGHT file. -# the COPYRIGHT file.
- if likes.size > 0 - if likes.size > 0
.likes .likes_container
= image_tag('icons/happy_smiley.png') .likes
= link_to t('.people_like_this', :count => likes.length), "#", :class => "expand_likes" = image_tag('icons/happy_smiley.png')
%span.hidden.likes_list = link_to t('.people_like_this', :count => likes.length), "#", :class => "expand_likes"
= likes_list(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)

View 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)) %>");

View 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)) %>");

View file

@ -37,20 +37,14 @@
= link_to(how_long_ago(post), status_message_path(post)) = link_to(how_long_ago(post), status_message_path(post))
- unless (defined?(@commenting_disabled) && @commenting_disabled) - unless (defined?(@commenting_disabled) && @commenting_disabled)
|
%span.like_action
= like_action(post)
|
= link_to t('comments.new_comment.comment'), '#', :class => 'focus_comment_textarea' = link_to t('comments.new_comment.comment'), '#', :class => 'focus_comment_textarea'
- if defined?(current_user) .likes
%span.like_links - if post.likes.count > 0
| = render "likes/likes", :post_id => post.id, :likes => post.likes, :dislikes => post.dislikes, :current_user => current_user
- 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
= render "comments/comments", :post => post, :comments => post.comments, :current_user => current_user, :condensed => true, :commenting_disabled => (defined?(@commenting_disabled) && @commenting_disabled) = render "comments/comments", :post => post, :comments => post.comments, :current_user => current_user, :condensed => true, :commenting_disabled => (defined?(@commenting_disabled) && @commenting_disabled)

View file

@ -161,7 +161,7 @@ en:
many: "%{count} comments" many: "%{count} comments"
other: "%{count} comments" other: "%{count} comments"
new_comment: new_comment:
comment: "comment" comment: "Comment"
commenting: "Commenting..." commenting: "Commenting..."
contacts: contacts:

View file

@ -92,12 +92,6 @@ var Stream = {
$(this).parent().fadeOut('fast'); $(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) { likes.live('ajax:failure', function(data, html, xhr) {
Diaspora.widgets.alert.alert('Failed to like/dislike!'); Diaspora.widgets.alert.alert('Failed to like/dislike!');
$(this).parent().fadeIn('fast'); $(this).parent().fadeIn('fast');

View file

@ -123,7 +123,7 @@ var WebSocketReceiver = {
processLike: function(postId, html) { processLike: function(postId, html) {
var post = $("*[data-guid='"+postId+"']"); var post = $("*[data-guid='"+postId+"']");
$(".likes_container", post).fadeOut('fast').html(html).fadeIn('fast'); $('.likes', post).html(html);
}, },
processPost: function(className, postId, html, aspectIds) { processPost: function(className, postId, html, aspectIds) {

View file

@ -571,8 +571,7 @@ header
ul.comments, ul.comments,
ul.show_comments, ul.show_comments,
div.likes, .likes_container
div.dislikes
:margin 0 :margin 0
:top 0.5em :top 0.5em
:padding 0 :padding 0
@ -2280,22 +2279,31 @@ h3,h4
:position relative :position relative
:z-index 10 :z-index 10
ul.show_comments,
div.likes, ul.show_comments
div.dislikes
:margin :margin
:bottom -0.5em :bottom -0.5em
> li
.likes_container
:margin
:bottom -4px
:padding 4px
ul.show_comments,
.likes_container
> *
:font :font
:size smaller :size smaller
img :weight bold
:position relative
:top 3px img
:height 12px :position relative
:width 12px :top 3px
:margin :height 12px
:left 0.5em :width 12px
:right 0.5em :margin
:left 0.5em
.mark_all_read .mark_all_read
:position relative :position relative
@ -2821,22 +2829,6 @@ h1.tag
:background :background
:color rgb(245,245,245) :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 #contacts_of_contact
.section .section
:margin :margin

View file

@ -83,7 +83,7 @@ describe LikesController do
it 'lets a user destroy their like' do it 'lets a user destroy their like' do
alice.should_receive(:retract).with(@like) alice.should_receive(:retract).with(@like)
delete :destroy, :format => "js", :post_id => @like.post_id, :id => @like.id delete :destroy, :format => "js", :post_id => @like.post_id, :id => @like.id
response.status.should == 204 response.should be_success
end end
it 'does not let a user destroy other likes' do it 'does not let a user destroy other likes' do