From 96319775977a27a6550fc6c646976242bfc5a1aa Mon Sep 17 00:00:00 2001 From: maxwell Date: Wed, 30 Jun 2010 17:29:32 -0700 Subject: [PATCH] RS MS comments propagate, and show up IN REAL TIME --- app/models/comment.rb | 8 +++++++- app/views/comments/_comment.html.haml | 8 ++++---- app/views/comments/_comments.html.haml | 6 +++--- app/views/js/_websocket_js.haml | 10 ++++++++-- app/views/layouts/application.html.haml | 4 ++-- lib/socket_render.rb | 13 ++++++++++++- 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 6aa5049ba..98b0300fc 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,7 +9,6 @@ class Comment xml_accessor :post_id key :text, String - key :target, String timestamps! key :post_id, ObjectId @@ -19,6 +18,8 @@ class Comment belongs_to :person, :class_name => "Person" after_save :send_friends_comments_on_my_posts + after_save :send_to_view + def ==(other) (self.message == other.message) && (self.person.email == other.person.email) @@ -32,4 +33,9 @@ class Comment self.push_to(self.post.friends_with_permissions) end end + + + def send_to_view + WebSocket.update_clients(self) + end end \ No newline at end of file diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index eb97c4123..6ae6e41a4 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -1,6 +1,6 @@ -%li.comment{:id => comment.id} +%li.comment{:id => post.id} %span.from - = link_to_person comment.person - = auto_link comment.text + = link_to_person post.person + = auto_link post.text %div.time - = "#{time_ago_in_words(comment.updated_at)} ago" + = "#{time_ago_in_words(post.updated_at)} ago" diff --git a/app/views/comments/_comments.html.haml b/app/views/comments/_comments.html.haml index 89237d1d7..9bcef1291 100644 --- a/app/views/comments/_comments.html.haml +++ b/app/views/comments/_comments.html.haml @@ -1,7 +1,7 @@ %div.comments - %ul.comment_set + %ul.comment_set{:id => post.id} - for comment in post.comments - = render "comments/comment", :comment => comment + = render 'comments/comment', :post => comment %li.comment - = render "comments/new_comment", :post => post + = render 'comments/new_comment', :post => post diff --git a/app/views/js/_websocket_js.haml b/app/views/js/_websocket_js.haml index 00d130029..1fc933a25 100644 --- a/app/views/js/_websocket_js.haml +++ b/app/views/js/_websocket_js.haml @@ -11,7 +11,13 @@ ws.onmessage = function(evt) { var obj = jQuery.parseJSON(evt.data); debug("got a " + obj['class']); - if((location.href.indexOf(obj['class']) != -1 ) || (location.pathname == '/')) { + + if (obj['class']=="comments"){ + var post_id = obj['post_id'] + $('#'+ obj['post_id'] + ' .comment_set li:last' ).before( + $(obj['html']).fadeIn("fast", function(){}) + ) + }else if((location.href.indexOf(obj['class']) != -1 ) || (location.pathname == '/')) { $("#stream").prepend( $(obj['html']).fadeIn("fast", function(){ $("#stream label:first").inFieldLabels(); @@ -24,4 +30,4 @@ ws.onopen = function() { ws.send(location.pathname); debug("connected..."); - };}); \ No newline at end of file + };}); diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 94b5b87dc..0f604ef70 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -12,8 +12,8 @@ /= javascript_include_tag"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" = javascript_include_tag 'jquery142', 'rails', 'view', 'publisher' = javascript_include_tag 'tiny_mce/tiny_mce.js','jquery.infieldlabel' - = render "js/google_a_js" - = render "js/websocket_js" + = render 'js/google_a_js' + = render 'js/websocket_js' = csrf_meta_tag = yield(:head) diff --git a/lib/socket_render.rb b/lib/socket_render.rb index 0f3c8dd8b..eb88613b7 100644 --- a/lib/socket_render.rb +++ b/lib/socket_render.rb @@ -22,11 +22,22 @@ module SocketRenderer puts e.message raise e end - {:class =>object.class.to_s.underscore.pluralize, :html => v} + + {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)} end def self.view_for(object) + puts object.inspect + puts @view.type_partial(object) + @view.render @view.type_partial(object), :post => object end + def self.obj_id(object) + if object.is_a? Post + object.id + else + object.post.id + end + end end