Juggernaut on Rails - Sending Private Messages

How to send Private Messages using Juggernaut.

taelor
8/12/2008

Chat_Sandbox now on GitHub!

I figured the best (and potentialy coolest) way to share this code was to just post it on GitHub. So now you can find the juggernaut chat_sandbox code on github. Please fork it off and fool with it. I have kept it bare bones, but if you wanna tweak it out some to show off, feel free. I’ll merge it back in.

Private Messaging Code Highlights

When you git the source, you will see where I have added a new controller called messages, which should have probably been called private_messages, since it only deals with just the private messages. Its not restful, but I was just going for something really quick. when a user clicks a username in the list, a chat box will open up. He is the helper to create that box.


 1 module MessagesHelper
2 def create_private_message(client_ids)
3 users = User.find(client_ids).collect{|user| user.login}.to_sentence
4 private_chat_room_id = private_chat_room_client_ids.join(_)
5 private_chat_input_id = private_chat_input_
client_ids.join(_)
6
7 top ="<div class=‘private_message’><h3>#{users}</h3><div id=‘#{private_chat_room_id}’ class=‘private_chat_room scroller’></div><div id=‘chat_form’>"
8
9 form = form_remote_tag(:url => { :action => :send_private_message, :client_ids => client_ids }, :complete => "$(‘#{private_chat_input_id}’).value = ‘’" )
10
11 text = text_field_tag( private_chat_input_id, , { :size => 50, :id => private_chat_input_id, :class=> private_chat_input} )
12
13 submit = submit_tag "Chat"
14
15 bottom ="</form></div><div"
16
17 top+form+text+submit+bottom
18 end
19 end



Now when someone chats something into this box, it will be pushed to the other user. But what if they don’t have that box created? Create it for them. This of course is the thought process for the javascript that gets pushed out. Here’s the code. Notice the part where you check if private_chat_room_id is null, it is a Javascript conditional, not ruby.


 1 def send_private_message
2 client_ids</span> = params[<span style="color:#A60">:client_ids</span>].collect{|client_id| client_id.to_i } <span style="color:#080;font-weight:bold">if</span> params[<span style="color:#A60">:client_ids</span>] <span class="no"> 3</span> <span class="no"> 4</span> private_chat_room_id = <span style="background-color:#fff0f0;color:#D20"><span style="color:#710">'</span><span style="">private_chat_room_</span><span style="color:#710">'</span></span>+<span style="color:#33B">client_ids.join(_)
5 private_chat_input_id = private_chat_input_+client_ids</span>.join(<span style="background-color:#fff0f0;color:#D20"><span style="color:#710">'</span><span style="">_</span><span style="color:#710">'</span></span>) <span class="no"> 6</span> <span class="no"> 7</span> render <span style="color:#A60">:juggernaut</span> =&gt; {<span style="color:#A60">:type</span> =&gt; <span style="color:#A60">:send_to_clients</span>, <span style="color:#A60">:client_ids</span> =&gt; <span style="color:#33B">client_ids } do |page|
8
9 page << "if ( $(‘#{private_chat_room_id}’) == null) {"
10 page.insert_html :bottom, private_messages, create_private_message(@client_ids)
11 page << "}"
12
13 page.insert_html :bottom, private_chat_room_id, "<p>#{current_user.login}: #{h params[private_chat_input_id]}</p>"
14 page.call :scrollChatPanel, private_chat_room_id
15 end
16
17 render :nothing => true
18 end



Fork the code and mess around with it. Have some fun. Help me learn something.

Next Post? Draggables? JQuery?

I looked on github for some Juggernaut related things and found a jQuery fork for juggernaut. I was going to maybe use that and jRails to port everything over. If not that, I could do Prototype Draggables for the private messages. What do you suggest? Leave a comment.

 
2/18/2009
 

Comments


781bda58d641b468092d36f4abda0170 Glenn said...
Aug 26, 2008

It looks like you've not closed the > bracket on the closing tag for chat_form DIV

Be2ea3416aaf1e1e4c62f3c9a8d7b3b6 Taylor said...
Aug 26, 2008

thanks for the heads up. fixed it on github. I love there new straight from website editing tool!

New Comment