-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
246 lines (199 loc) · 4.91 KB
/
app.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require 'rubygems'
require 'fileutils'
require 'sinatra'
require 'json'
require 'haml'
require 'config/database'
require 'helpers/sinatra'
## CONFIGURATION
enable :sessions
set :views, File.dirname(__FILE__) + '/views'
set :public, File.dirname(__FILE__) + '/public'
#@env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
mime :json, "application/json"
before do
if logged_in?
@new_tasks = Task.all(:tasked_id => logged_in_user.id).count(:status => 0)
@tasked = Task.all(:tasked_by_id => logged_in_user.id, :tasked_id.not => logged_in_user.id).count(:status => 0)
end
end
## ROUTING
enable :sessions
get '/' do
@u = session[:user]
if logged_in?
@tasks = Task.all(:tasked_id => logged_in_user.id, :status => 0)
@completed_tasks = Task.all(:tasked_id => logged_in_user.id, :status => 1)
@deleted_tasks = Task.all(:tasked_id => logged_in_user.id, :status => -1)
erb :'tasks/index'
else
erb :index
end
end
## start of new routing paradigm...
get '/:user' do
erb :index
end
## ...end of new routing paradigm
get '/user/login' do
erb :'users/login'
end
post '/user/login' do
if session[:user] = User.authenticate(params["email"], params["password"])
flash("Login successful")
redirect '/'
else
flash("Login failed - Try again")
redirect '/user/login'
end
end
get '/user/logout' do
session[:user] = nil
flash("Logout successful")
redirect '/'
end
get '/user/create' do
erb :'users/create'
end
post '/user/create' do
u = User.new
u.login = params["login"]
u.password = params["password"]
u.email = params["email"]
if u.save
flash("User created")
redirect '/users/login'
else
tmp = []
u.errors.each do |e|
tmp << (e.join("<br />"))
end
flash(tmp)
redirect '/user/create'
end
end
get '/user/account' do
if logged_in?
@user = logged_in_user
erb :'users/account'
else
flash("you need to log in to manage your account, existing or otherwise.")
redirect '/'
end
end
post '/user/account' do
if logged_in?
u = logged_in_user
u.password = params["password"]
if u.save
flash("Account updated")
redirect '/tasks'
else
tmp = []
u.errors.each do |e|
tmp << (e.join("<br />"))
end
flash(tmp)
redirect '/tasks'
end
else
flash("you need to log in to manage your account, existing or otherwise.")
redirect '/'
end
end
get '/users/list' do
@u = User.all
erb :'users/index'
end
# this is really the only search method we need...
# can probably package it up real tight, do some kind of hyper-indexing on this shit, and chunk it over to the JS
get '/users/search' do
if request.xhr?
mime :json, "application/json"
# loop through params (only search by email for now...)
# what kind of search we gonna use? what does git use?
# can probably get away with vanilla SQL for now...
else
# really shouldn't be...
end
end
# TASKS
get '/task/create' do
erb :'tasks/create'
end
post '/task/create' do
t = Task.new
@tasked_by = logged_in_user
@tasked = logged_in_user
if params["tasked_email"] && params["tasked_email"].length > 0
# you be taskin'
unless User.first(:email => params["tasked_email"])
flash("aint no user with that email address, son")
redirect '/task/create'
end
@tasked = User.first(:email => params["tasked_email"])
end
#
# begin
# @tasked = User.first(:email => params["tasked_email"])
# rescue
# flash("you can't task that person...")
# redirect '/task/create'
# end
# else
# @tasked = logged_in_user
# end
t.body = params["body"]
t.tasked_id = @tasked.id
t.tasked_by_id = @tasked_by.id
if t.save
#foo
redirect '/tasks'
else
flash("errors, byatch")
redirect '/task/create'
end
erb :'tasks'
end
get '/task/update' do
end
post '/task/update' do
# puts "status: #{json_params.to_s}"
task = Task.get(params[:id])
puts task.body
task.status = params[:status] if params[:status]
if task.save
if request.xhr?
mime :json, "application/json"
content_type :json
task.to_json
"{foo: 'bar'}"
else
# foo!
end
redirect '/' unless self.request.env['HTTP_X_REQUESTED_WITH'] and self.request.env['HTTP_X_REQUESTED_WITH'].scan(/XML/) # Don't redirect Ajax request...
else
flash("errors, byatch")
end
end
get '/tasks' do
if logged_in?
@tasks = Task.all(:tasked_id => logged_in_user.id, :status => 0)
@completed_tasks = Task.all(:tasked_id => logged_in_user.id, :status => 1)
@deleted_tasks = Task.all(:tasked_id => logged_in_user.id, :status => -1)
erb :'tasks/index'
else
redirect '/'
end
end
get '/tasked' do
if logged_in?
@tasks = Task.all(:tasked_by_id => logged_in_user.id, :tasked_id.not => logged_in_user.id, :status.gt => -1)
erb :'tasks/index'
else
redirect '/'
end
end
get '/task/:id' do
redirect '/'
end