# ActsAsChattable # =============== # # ActsAsChattable is a plugin with make web chatting easy. # # With this plugin, you can add a user-to-user chatting system into your Rails app # within 3-minutes. # # It uses some codes from Chatten project (http://rubyforge.org/projects/chatten/), # So let's give our thanks to the author of Chatten: Jacob Pedersen. # # Version: 0.1.0 - beta # Author: Yuanyi Zhang -- http://elctech.com # Contributors: # License: Ruby MIT # # ==Installation # # ./script/plugin install https://svn.elctech.com/svn/public/plugins/acts_as_chattable # # ==Demo # # svn co https://svn.elctech.com/svn/public/demo/acts_as_chattable # # ==Usage # # * At first, I assume you have a model named User and a controller named UsersController, # I also assume you have a helper named 'current_user' which will return the current # user (it should be there if you use restful_auth). If you don't have these yet, # create them and let's go! # # * create chat_message model and migration: # # script/generate chattable # # * add acts_as_chattable to user model # # class User # acts_as_chattable # ... # # def name # ... # end # end # # You'd better to define name method for User, or all users' name will be displayed as 'Unknown'. # # * include chat system in users controller # # class UsersController # include ChatSystem # ... # end # # * render chat in views # # <%= render_chat(friends, use_yui) %> # <%= buddy_list(friends, use_yui) %> # # friends should be an array of user with presents current user's friends. # use_yui indicate if YUI should be used to get a better look, default is true. # # You can get more info about these two helpers with rdoc. # # * It's done, open your browser and start to enjoy the chatting. # # =================================== # For more detailed examples with sample images visit http://elctech.com/blog # All Feedbacks/Comments/Issues are welcome. module ActsAsChattable # :nodoc module ViewHelpers # :nodoc # This helper will render chat views for specific page. # # friends is an array of user which should presents current user's friends.
# use_yui indicate if YUI should be used to get a better look, default is true. # # *Examples* # # # users_controller.rb # def index # @friends = current_user.friends # end # # # index.html.erb # <%= render_chat @friends %> # def render_chat(user, friends) chat = <<-JS JS chat += <<-JS
JS chat += <<-CSS CSS friends.each do |friend| chat += "
" chat += <<-HANDLE
Chat with #{friend.name}:
HANDLE chat += <<-CHATWINDOW
CHATWINDOW chat += form_remote_tag(:url => { :controller => 'chat', :action => :send_data, :from => user.id, :to => friend.id }, :complete => "$('chat_input#{friend.id}').value = ''" ) chat += text_field_tag "chat_input#{friend.id}" chat += submit_tag 'Send', :id => 'commit' chat += '' chat += "
" chat += <<-JS JS end chat end # This helper will generate a buddy list for you, when user click # the friend's name in buddy list, a chatting window will be open to allow # user chatting with this friend. # # *Example* # # <%= buddy_list(current_user.friends) %> # # If you want to customize the buddy list, use chat_link which will # provide a link to open the chatting window. # def buddy_list(friends) chat = '' end # Link which will open a chatting window to chat with this user. # # if you didn't use yui to render the chatting window, then # use_yui should be set to false to show a non-yui chatting # window. # # *Example* # # To generate a buddy list: # # <% user.firends.each do |friend| %> # <%= link_to friend.name, friend.chat_link %> # <% end %> # # It's equal to buddy_list helper. # def chat_link(friend) "$('chat#{friend.id}').show(); $('chat_input#{friend.id}').focus()" end end end