/
chat_rooms_controller.rb
106 lines (91 loc) · 2.58 KB
/
chat_rooms_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class ChatRoomsController < ApplicationController
before_filter :is_logged_in?
# GET /chat_rooms
# GET /chat_rooms.xml
def index
@chat_rooms = ChatRoom.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @chat_rooms }
end
end
# GET /chat_rooms/1
# GET /chat_rooms/1.xml
def show
@chat_room = ChatRoom.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @chat_room }
end
end
# GET /chat_rooms/new
# GET /chat_rooms/new.xml
def new
@chat_room = ChatRoom.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @chat_room }
end
end
# GET /chat_rooms/1/edit
def edit
@chat_room = ChatRoom.find(params[:id])
end
# POST /chat_rooms
# POST /chat_rooms.xml
def create
@chat_room = ChatRoom.new(params[:chat_room])
respond_to do |format|
if @chat_room.save
flash[:notice] = 'ChatRoom was successfully created.'
format.html { redirect_to(@chat_room) }
format.xml { render :xml => @chat_room, :status => :created, :location => @chat_room }
else
format.html { render :action => "new" }
format.xml { render :xml => @chat_room.errors, :status => :unprocessable_entity }
end
end
end
# PUT /chat_rooms/1
# PUT /chat_rooms/1.xml
def update
@chat_room = ChatRoom.find(params[:id])
respond_to do |format|
if @chat_room.update_attributes(params[:chat_room])
flash[:notice] = 'ChatRoom was successfully updated.'
format.html { redirect_to(@chat_room) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @chat_room.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /chat_rooms/1
# DELETE /chat_rooms/1.xml
def destroy
@chat_room = ChatRoom.find(params[:id])
@chat_room.destroy
respond_to do |format|
format.html { redirect_to(chat_rooms_url) }
format.xml { head :ok }
end
end
def send_data
render :juggernaut => {:type => :send_to_channels, :channels => [params[:id].to_i] } do |page|
page.insert_html :bottom, 'chat_room', "<p>#{current_user.login}: #{h params[:chat_input]}</p>"
page.call :scrollChatPanel, 'chat_room'
end
render :nothing => true
end
private
def is_logged_in?
if logged_in?
true
else
flash[:error] = "You must be logged in to chat"
redirect_to("/login")
false
end
end
end