-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
273 lines (236 loc) · 5.55 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
require "sinatra"
enable :sessions
enable :protection
set :protection, except: :session_hijacking
use Rack::Deflater
#Cats
get "/" do
if request.xhr?
erb :body
else
erb :index
end
end
#Many nodes
get "/many" do
if request.xhr?
erb :body_many_nodes
else
erb :index_many_nodes
end
end
#Todo
get "/todo" do
@todos = todos
if @todos.length == 0
erb :todo_index_empty
else
@completed_count = completed.length
@active_count = active.length
@all_completed = all_completed? @todos
@show_footer_and_toggle_all = todos.length > 0
erb :todo_index
end
end
get "/todo/?:route?/edit/:id" do
@edit_id = params[:id]
@route = params[:route]
@todos = todos_based_on_route @route
if request.xhr?
erb :todos
else
@completed_count = completed.length
@active_count = active.length
@all_completed = all_completed? @todos
@show_footer_and_toggle_all = todos.length > 0
erb :todo_index
end
end
get "/todo/?:route?/todos" do
@route = params[:route]
@todos = todos_based_on_route @route
erb :todos
end
get "/todo/?:route?/footer" do
@completed_count = completed.length
@active_count = active.length
@route = params[:route]
@show_footer_and_toggle_all = todos.length > 0
erb :footer
end
get "/todo/completed" do
@todos = completed
@route = "completed"
@active_count = active.length
@show_footer_and_toggle_all = todos.length > 0
@completed_count = completed.length
@all_completed = all_completed? todos
erb :todo_index
end
get "/todo/active" do
@todos = active
@route = "active"
@active_count = active.length
@show_footer_and_toggle_all = todos.length > 0
@completed_count = completed.length
@all_completed = all_completed? todos
erb :todo_index
end
post "/todo/?:route?/new_todo" do
add_todo({:text => params[:title], :completed => false, :id => rand(36**8).to_s(36)})
if request.xhr?
@route = params[:route]
@todos = todos_based_on_route @route
erb :todos
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
post "/todo/?:route?/destroy/:id" do
destroy_todo(params[:id])
if request.xhr?
@route = params[:route]
@todos = todos_based_on_route @route
erb :todos
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
get "/todo/toggle_all" do
@todos = todos
@all_completed = all_completed? @todos
@show_footer_and_toggle_all = todos.length > 0
erb :toggle_all
end
post "/todo/?:route?/complete/:id" do
complete_todo params[:id]
if request.xhr?
@route = params[:route]
@todos = todos_based_on_route @route
erb :todos
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
post "/todo/?:route?/reactivate/:id" do
reactivate_todo params[:id]
if request.xhr?
@route = params[:route]
@todos = todos_based_on_route @route
erb :todos
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
post "/todo/?:route?/edit/:id" do
edit_todo params[:id], params[:text]
if request.xhr?
@route = params[:route]
@todos = todos_based_on_route @route
erb :todos
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
post "/todo/?:route?/complete_all" do
complete_all_todos
if request.xhr?
@route = params[:route]
@all_completed = all_completed? todos
@show_footer_and_toggle_all = todos.length > 0
erb :toggle_all
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
post "/todo/?:route?/reactivate_all" do
reactivate_all_todos
if request.xhr?
@route = params[:route]
@all_completed = all_completed? todos
@show_footer_and_toggle_all = todos.length > 0
erb :toggle_all
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
post "/todo/?:route?/clear_completed" do
clear_completed_todos
if request.xhr?
@completed_count = completed.length
@active_count = active.length
@route = params[:route]
@todos = todos_based_on_route @route
@show_footer_and_toggle_all = todos.length > 0
erb :footer
else
redirect "/todo/#{params[:route]}" if params[:route]
redirect "/todo"
end
end
private
def no_todos
[]
end
def completed
todos.select { |todo| todo[:completed] }
end
def active
todos.select { |todo| !todo[:completed] }
end
def todos
return session[:todos] unless session[:todos] == nil
return []
#[{:text => "not completed", :completed => false}, {:text => "completed", :completed => true}]
end
def todos_based_on_route route
if route == "completed"
return completed
elsif route == "active"
return active
else
return todos
end
end
def add_todo todo
session[:todos] = [] if session[:todos] == nil
session[:todos] << todo
end
def destroy_todo id
session[:todos].delete_if { |todo| todo[:id] == id }
end
def complete_todo id
todo = find_todo_by_id id
todo[:completed] = true
end
def reactivate_todo id
todo = find_todo_by_id id
todo[:completed] = false
end
def edit_todo id, text
todo = find_todo_by_id id
todo[:text] = text
end
def find_todo_by_id id
session[:todos].select { |todo| todo[:id] == id }.first
end
def all_completed? ts
return true if ts.select { |todo| todo[:completed] == false }.empty?
false
end
def complete_all_todos
todos.map { |todo| todo[:completed] = true }
end
def reactivate_all_todos
todos.map { |todo| todo[:completed] = false }
end
def clear_completed_todos
todos.delete_if { |todo| todo[:completed] == true }
end