From 797a6f5ca9cf18a5bff22cefe04eeb191d6a990e Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Fri, 8 Jul 2011 15:29:58 -0700 Subject: [PATCH] like comments js response wip --- app/controllers/likes_controller.rb | 11 +++++++---- app/models/comment.rb | 2 +- app/models/like.rb | 2 +- app/models/user.rb | 2 +- app/views/comments/_comment.html.haml | 17 +++++++++++++---- app/views/likes/_likes_container.haml | 13 ++++++------- app/views/likes/create.js.erb | 4 ---- app/views/likes/destroy.js.erb | 3 --- app/views/likes/update.js.erb | 2 ++ app/views/shared/_stream_element.html.haml | 7 +++---- public/javascripts/content-updater.js | 11 ++++------- public/javascripts/view.js | 2 +- public/javascripts/web-socket-receiver.js | 11 +++++------ public/stylesheets/sass/application.sass | 12 ++++-------- 14 files changed, 48 insertions(+), 51 deletions(-) delete mode 100644 app/views/likes/create.js.erb delete mode 100644 app/views/likes/destroy.js.erb create mode 100644 app/views/likes/update.js.erb diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 482eaaefe..41edf780b 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -24,7 +24,7 @@ class LikesController < ApplicationController Postzord::Dispatch.new(current_user, @like).post respond_to do |format| - format.js { render :status => 201 } + format.js { render 'likes/update', :status => 201 } format.html { render :nothing => true, :status => 201 } format.mobile { redirect_to post_path(@like.post_id) } end @@ -37,10 +37,13 @@ class LikesController < ApplicationController end def destroy - target_id = params[:post_id] || params[:comment_id] - - if @like = Like.where(:id => params[:id], :author_id => current_user.person.id, :target_id => target_id).first + if @like = Like.where(:id => params[:id], :author_id => current_user.person.id).first current_user.retract(@like) + pp @like + respond_to do |format| + format.all{} + format.js{ render 'likes/update' } + end else respond_to do |format| format.mobile {redirect_to :back} diff --git a/app/models/comment.rb b/app/models/comment.rb index 4fb96cb8f..81c4b9d0e 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -25,7 +25,7 @@ class Comment < ActiveRecord::Base belongs_to :post, :touch => true belongs_to :author, :class_name => 'Person' - has_many :likes, :foreign_key => :target_id, :conditions => {:positive => true}, :dependent => :delete_all + has_many :likes, :conditions => {:positive => true}, :dependent => :delete_all, :as => :target validates_presence_of :text, :post validates_length_of :text, :maximum => 2500 diff --git a/app/models/like.rb b/app/models/like.rb index 6f6c90268..f86c28dc6 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -20,7 +20,7 @@ class Like < ActiveRecord::Base belongs_to :target, :polymorphic => true #, :counter_cache => true belongs_to :author, :class_name => 'Person' - validates_uniqueness_of :target_id, :scope => :author_id + validates_uniqueness_of :target_id, :scope => [:target_type, :author_id] validates_presence_of :author, :target def diaspora_handle diff --git a/app/models/user.rb b/app/models/user.rb index 20b3bed24..4b24f4e26 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -202,7 +202,7 @@ class User < ActiveRecord::Base if target.likes.loaded? return target.likes.detect{ |like| like.author_id == self.person.id } else - return Like.where(:author_id => self.person.id, :target_id => target.id).first + return Like.where(:author_id => self.person.id, :target_type => target.class.base_class.to_s, :target_id => target.id).first end end diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index 8ec14dc19..923929ac2 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -2,7 +2,7 @@ -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. -%li.comment.posted{:data=>{:guid => comment.id}, :class => ("hidden" if(defined? hidden))} +%li.comment.posted{:id => comment.guid, :class => ("hidden" if(defined? hidden))} - if current_user && (current_user.owns?(comment) || current_user.owns?(post)) .right.controls = link_to image_tag('deletelabel.png'), post_comment_path(comment.post_id, comment), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete comment_delete", :title => t('delete') @@ -16,9 +16,18 @@ %span{:class => direction_for(comment.text)} = markdownify(Diaspora::Taggable.format_tags(comment.text), :youtube_maps => comment.youtube_titles) - %br + .comment_info %time.timeago{:datetime => comment.created_at} = comment.created_at ? timeago(comment.created_at) : timeago(Time.now) - - unless (defined?(@commenting_disabled) && @commenting_disabled) - = like_action(comment, current_user) + · + + .likes + = render "likes/likes_container", :target_id => comment.id, :likes_count => comment.likes.count + + - if comment.likes.count > 0 + · + + - unless (defined?(@commenting_disabled) && @commenting_disabled) + %span.like_action + = like_action(comment, current_user) diff --git a/app/views/likes/_likes_container.haml b/app/views/likes/_likes_container.haml index a2d07967d..bb43ed49d 100644 --- a/app/views/likes/_likes_container.haml +++ b/app/views/likes/_likes_container.haml @@ -2,11 +2,10 @@ -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. -- if likes_count > 0 - .likes_container - .likes - = image_tag('icons/heart.svg') - = link_to t('likes.likes.people_like_this', :count => likes_count), post_likes_path(target_id), :class => "expand_likes" - %span.hidden.likes_list - /= render 'likes/likes', :likes => likes +.likes_container + - if likes_count > 0 + = image_tag('icons/heart.svg') + = link_to t('likes.likes.people_like_this', :count => likes_count), post_likes_path(target_id), :class => "expand_likes" + %span.hidden.likes_list + /= render 'likes/likes', :likes => likes diff --git a/app/views/likes/create.js.erb b/app/views/likes/create.js.erb deleted file mode 100644 index 86a7ba9e6..000000000 --- a/app/views/likes/create.js.erb +++ /dev/null @@ -1,4 +0,0 @@ -$(".like_action", ".stream_element[data-guid=<%=@like.target_id%>]").html("<%= escape_javascript(like_action(@like.target))%>"); - -WebSocketReceiver.processLike("<%=@like.target_id%>", "<%= escape_javascript(render("likes/likes_container", :target_id => @like.target_id, :likes_count => @like.target.likes.count)) %>"); - diff --git a/app/views/likes/destroy.js.erb b/app/views/likes/destroy.js.erb deleted file mode 100644 index adea1fb67..000000000 --- a/app/views/likes/destroy.js.erb +++ /dev/null @@ -1,3 +0,0 @@ -$(".like_action", ".stream_element[data-guid=<%=@like.target_id%>]").html("<%= escape_javascript(like_action(@like.target))%>"); -WebSocketReceiver.processLike("<%=@like.target_id%>", "<%= escape_javascript(render("likes/likes_container", :target_id => @like.target_id, :likes_count => @like.target.likes.count)) %>"); - diff --git a/app/views/likes/update.js.erb b/app/views/likes/update.js.erb new file mode 100644 index 000000000..7a447796e --- /dev/null +++ b/app/views/likes/update.js.erb @@ -0,0 +1,2 @@ +$(".like_action", "#<%=@like.target.guid%>").first().html("<%= escape_javascript(like_action(@like.target))%>"); +WebSocketReceiver.processLike("<%=@like.target.guid%>", "<%= escape_javascript(render("likes/likes_container", :target_id => @like.target_id, :likes_count => @like.target.likes.count)) %>"); diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index 69727b49b..d8fb9b2f1 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -2,7 +2,7 @@ -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. -.stream_element{:data=>{:guid=>post.id}} +.stream_element{:id => post.guid} - if current_user && post.author.owner_id == current_user.id .right.controls = link_to image_tag('deletelabel.png'), post_path(post), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete stream_element_delete", :title => t('delete') @@ -55,8 +55,7 @@ · = link_to t('comments.new_comment.comment'), '#', :class => 'focus_comment_textarea' - .likes - - if post.likes.count > 0 - = render "likes/likes_container", :target_id => post.id, :likes_count => post.likes.count, :current_user => current_user + .likes + = render "likes/likes_container", :target_id => post.id, :likes_count => post.likes.count, :current_user => current_user = render "comments/comments", :post => post, :current_user => current_user, :commenting_disabled => (defined?(@commenting_disabled) && @commenting_disabled) diff --git a/public/javascripts/content-updater.js b/public/javascripts/content-updater.js index 0f1e115c7..c9b074cc8 100644 --- a/public/javascripts/content-updater.js +++ b/public/javascripts/content-updater.js @@ -4,14 +4,11 @@ */ var ContentUpdater = { - elementWithGuid: function(selector, guid) { - return $(selector + "[data-guid='" + guid + "']"); - }, addPostToStream: function(html) { var streamElement = $(html); - var postId = streamElement.attr("data-guid"); + var postId = streamElement.id; - if($(".stream_element[data-guid='" + postId + "']").length === 0) { + if($(".stream_element#" + postId).length === 0) { if($("#no_posts").length) { $("#no_posts").detach(); } @@ -27,7 +24,7 @@ var ContentUpdater = { }, addLikesToPost: function(postId, html) { - var post = ContentUpdater.elementWithGuid("div", postId); + var post = $("div#" + postId); $(".likes_container", post) .fadeOut("fast") @@ -35,4 +32,4 @@ var ContentUpdater = { .fadeIn("fast"); } -}; \ No newline at end of file +}; diff --git a/public/javascripts/view.js b/public/javascripts/view.js index 4ae588bb6..90b7d500e 100644 --- a/public/javascripts/view.js +++ b/public/javascripts/view.js @@ -79,7 +79,7 @@ var View = { /* notification routing */ $("#notification").delegate('.hard_object_link', 'click', function(evt){ - var post = $("*[data-guid='"+ $(this).attr('data-ref') +"']"), + var post = $("#"+ $(this).attr('data-ref')), lastComment = post.find('.comment.posted').last(); if(post.length > 0){ diff --git a/public/javascripts/web-socket-receiver.js b/public/javascripts/web-socket-receiver.js index ddd36e354..17dd6e62b 100644 --- a/public/javascripts/web-socket-receiver.js +++ b/public/javascripts/web-socket-receiver.js @@ -77,7 +77,7 @@ var WebSocketReceiver = { }, processRetraction: function(post_id){ - $("*[data-guid='" + post_id + "']").fadeOut(400, function() { + $("#" + post_id).fadeOut(400, function() { $(this).remove(); }); if($("#main_stream")[0].childElementCount === 0) { @@ -87,9 +87,9 @@ var WebSocketReceiver = { processComment: function(postId, commentId, html, opts) { - if( $(".comment[data-guid='"+commentId+"']").length === 0 ) { + if( $("#"+commentId).length === 0 ) { - var post = $("*[data-guid='"+postId+"']'"), + var post = $("#"+postId), prevComments = $('.comment.posted', post); if(prevComments.length > 0) { @@ -123,9 +123,8 @@ var WebSocketReceiver = { Diaspora.widgets.directionDetector.updateBinds(); }, - processLike: function(postId, html) { - var post = $("*[data-guid='"+postId+"']"); - $('.likes', post).html(html); + processLike: function(targetGUID, html) { + $('.likes', "#" + targetGUID).first().html(html); }, processPost: function(className, postId, html, aspectIds) { diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index 8e318cf08..b9892254a 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -302,10 +302,10 @@ ul.as-selections :height 370px :width 500px - time - :font - :weight normal - :size smaller + .comment + .comment_info + :font + :size smaller .from a @@ -667,10 +667,6 @@ form.new_comment :display inline-block -.comments - .timeago - :color #999 - .stream.show ul.comments li