-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.rb
517 lines (452 loc) · 11.6 KB
/
views.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
require 'deklarativna'
class BaseView < BaseTemplate
def initialize request
@request = request
@title = "Deklarativna"
end
def _stylesheets
[
link("rel"=>"stylesheet",
"href"=>"/css/bootstrap.css",
"type"=>"text/css"),
link("rel"=>"stylesheet",
"href"=>"/css/styles.css",
"type"=>"text/css")
]
end
def _scripts
[
javascript("src"=>"http://code.jquery.com/jquery-latest.js"),
]
end
def _head
[
title { @title }
] + _stylesheets + _scripts + _extra_head
end
def _extra_head
[]
end
def __nav_bar_item text, link, active_condition
li("class"=>("active" if active_condition)) {
a("href"=>link) { text }
}
end
def _nav_bar_items
[
(__nav_bar_item "Home", "/", (@request.path_info == "/")),
(__nav_bar_item "About", "/about/", (@request.path_info == "/about/")),
(__nav_bar_item "Blog", "/posts/", (/\/posts.*/.match @request.path_info))
]
end
def _nav_bar
ul("class"=>"nav", "style"=>"float:right;") { _nav_bar_items }
end
def _topbar
div("class"=>"topbar") {
div("class"=>"fill") {
div("class"=>"container") {[
a("class"=>"brand", "href"=>"/") { "Deklarativna" },
_nav_bar
]}
}
}
end
def _footer
footer("style"=>"text-align:center;") {
span { "© 2012 David Litvak Bruno" }
}
end
def _body
content + [_footer]
end
def content
[
_topbar,
div("class"=>"container") {
[
div("class"=>"content", "id"=>"content") {
_content
}
] + _extra_content
}
] + _extra_body
end
def _content
[]
end
def _extra_content
[]
end
def _extra_body
[]
end
def _input label_text, label_for, widget
div("class"=>"clearfix") {[
label("for"=>label_for) { label_text },
div("class"=>"input") { widget }
]}
end
def _submit text
div("class"=>"clearfix") {
div("class"=>"input") {
submit("value"=>text)
}
}
end
end
class IndexView < BaseView
def _title_block
div("class"=>"hero-unit") {[
h1 { "Deklarativna's Blog" },
p { "A blog about declarative programming" },
p {
a("href"=>"/posts/", "class"=>"btn primary large") {
"To the Posts! »"
}
}
]}
end
def _about_me
div("class"=>"span5") {[
h2 { "A little bit about me" },
p {
"I'm an enthusiastic programmer interested in
new ways to develop, always trying to expand and extend
the tools and help people to discover more and more..."
},
br,
p {
a("href"=>"/about/", "class"=>"btn") {
"About me »"
}
}
]}
end
def _about_deklarativna
div("class"=>"span6") {[
h2 { "About Deklarativna" },
p {
"Deklarativna is a templating framework for ruby. #{br}
The idea after it, was to allow developers to write
the html templates in ruby code, and to allow them to
have a fully integrated frontend development experience. #{br}
Also, it was created as teaching material for my students."
},
p {
a("href"=>"http://www.github.com/dlitvakb/deklarativna",
"class"=>"btn") {
"Find out more! »"
}
}
]}
end
def _other_resources
div("class"=>"span6") {[
h2 { "What other resources do I use?" },
p {
"As I intend to program this blog in a fully declarative way,
I've found that the best resources to fit my intentions were
#{a("href"=>"http://www.sinatrarb.com") {"Sinatra"}} and
#{a("href"=>"http://www.datamapper.org") {"DataMapper"}}. #{br}
As I've found #{a("href"=>"http://www.ruby-lang.org") {"Ruby"}}
to be an amazing programming language to write in a
declarative way, it has been, therefore my desicion to use it"
}
]}
end
def _content
[
_title_block,
div("class"=>"row") {[
_about_me,
_about_deklarativna,
_other_resources
]}
]
end
end
class AboutView < BaseView
def initialize request
super request
@title = "Deklarativna's Blog - About"
end
def _about_me
p { "My name is #{b { "David Litvak Bruno" }},
I'm a software developer from Argentina. #{br}
I'm a framework development devotee,
and my main programming languages are
#{i { "Python, Ruby and Java" }}. #{br}
I wrote Deklarativna as teaching material for
the Advanced Object Oriented Programming course I'm an
assistant teacher in. #{br}
It has been a great and rewarding experience and I hope
other people find also this piece of software usefull "
}
end
def _email
a("href"=>"mailto:[email protected]") {
"david (dot) litvakb (at) gmail (dot) com"
}
end
def _twitter
a("href"=>"http://www.twitter.com/dlitvakb") { "Twitter" }
end
def _github
a("href"=>"http://www.github.com/dlitvakb") { "GitHub" }
end
def _contact_info
[
p { "You can contact me at #{_email}" },
p { "You can also follow me at #{_github} or #{_twitter}." }
]
end
def _content
[
h2 { "About me..." },
div {
[
_about_me
] + _contact_info
}
]
end
end
class NotFoundView < BaseView
def _content
[
div("class"=>"hero-unit") {[
h1 { "404 - Not Found" },
p { "Sorry, the page you were looking for is not here" },
p {
a("href"=>"/posts/", "class"=>"btn primary large") {
"To the Posts! »"
}
}
]}
]
end
end
class PostsView < BaseView
def initialize request, displaying_posts, recent_posts=nil
super request
@displaying_posts = displaying_posts
@recent_posts = recent_posts
@title = "Deklarativna's Blog"
end
def _nav_bar_items
if (@request.user).nil?
return super << (__nav_bar_item "Login",
"/user/login/",
(@request.path_info == '/user/login/')
)
end
if @request.user.is_admin
return super << (__nav_bar_item "Create Post",
"/post/create/",
(@request.path_info == '/post/create/')
) << (__nav_bar_item "Logout",
"/user/logout/",
false)
end
return super
end
def _categories post
categories = []
post.categories.each do |category|
categories << a("href"=>"/posts/category/#{category.name}/") {
category.name
}
end
categories
end
def _sanitize_iso_timestamp timestamp
date_iso_splitted = timestamp.split("T")
date_iso_splitted[1] = date_iso_splitted[1].split("-")[0]
date_iso_splitted
end
def _posted_at post
datetime = (_sanitize_iso_timestamp post.created_at.to_s)
date_part = datetime[0].split("-")
year = date_part[0]
month = date_part[1]
day = date_part[2]
a("href"=>"/posts/#{year}/#{month}/#{day}/") {
datetime.join(" ")
}
end
def _comments_extra post
a("href"=>"/posts/#{post.id}/#comments", "class"=>"extra-comments") {
"Comments (#{post.comments.size})"
}
end
def _render_post post
[
div("class"=>"post") {[
h2 { a("href"=>"/posts/#{post.id}/") { post.title }},
p("class"=>"post-body") { post.body },
p("class"=>"categories") {
[ "Category: ", (_categories post).join(" ") ]
},
p("class"=>"posted-at") {
"Posted at #{i { _posted_at post }}" + (_comments_extra post)
}
]}
]
end
def _posts
@displaying_posts.map { |e| _render_post e }
end
def _recent_posts
@recent_posts.map { |e| p { a("href"=>"/posts/#{e.id}/") { e.title } } }
end
def _sidebar
[
h3 { a("href"=>"/") { "Deklarativna" }},
hr,
h4 { "Recent Posts" }
] + _recent_posts
end
def _content
[
div("class"=>"row") {[
div("id"=>"posts", "class"=>"span12") { _posts },
div("id"=>"sidebar", "class"=>"span4") { _sidebar }
]}
]
end
end
class PostDetailView < PostsView
def initialize request, post, recent_posts=nil
@request = request
@post = post
@recent_posts = recent_posts
@title = "Deklarativna's Blog - #{@post.title}"
end
def _comments_extra post
""
end
def _commentor comment
return (a("href"=>comment.url) { comment.posted_by }) if !comment.url.empty?
comment.posted_by
end
def _comment comment
datetime = (_sanitize_iso_timestamp comment.created_at.to_s)
div("class"=>"comment") {[
h3 { _commentor comment},
p { comment.body },
p("class"=>"posted-at") {
"#{_commentor comment} wrote this at #{datetime[0]} #{datetime[1]}"
}
]}
end
def _render_comments post
comments = []
post.comments.each { |e|
comments << (_comment e)
}
comments
end
def _comments post
div("class"=>"comments", "id"=>"comments") {[
h3 { "Comments (#{post.comments.size})" }
] + (_render_comments post)
}
end
def _new_comment_form
form("class"=>"comment-form",
"method"=>"post",
"action"=>"/posts/#{@post.id}/comments/create/") {[
h5 { "Add your comments:" },
(_input "Name", "name",
text("name"=>"posted_by",
"value"=>"#{@request.user.user_name if @request.user.respond_to? :user_name}"
)
),
(_input "Website (Optional)", "url", text("name"=>"url")),
(_input "Message", "message", textarea("name"=>"body", "rows"=>"5")),
_submit("Comment!")
]}
end
def _render_post post
(super post) << (_comments post) << (_new_comment_form)
end
def _posts
[
(_render_post @post)
]
end
end
class PostCreateView < PostsView
def initialize request
@request = request
@title = "Deklarativna's Blog - New Post"
end
def _form
form("method"=>"post","action"=>"/post/create/") {[
(_input "Title", "title", text("name"=>"title")),
(_input "Body", "body", textarea("name"=>"body", "rows"=>"10")),
(_input "Categories", "categories", text("name"=>"categories")),
_submit("Post!")
]}
end
def _content
[
h2 { "New Post..." },
_form
]
end
end
class UserCreateView < PostsView
def initialize request
@request = request
@title = "Deklarativna's Blog - New User"
end
def _form
form("method"=>"post","action"=>"/user/create/") {[
(_input "Name", "name", text("name"=>"name")),
(_input "Password", "password", password("name"=>"password")),
(_input "E-Mail", "email", text("name"=>"email")),
_submit("Create!")
]}
end
def _content
[
h2 { "New User..." },
_form
]
end
end
class UserLoginView < PostsView
def initialize request
@request = request
@title = "Deklarativna's Blog - Login"
end
def _form
form("method"=>"post","action"=>"/user/login/") {[
(_input "Name", "name", text("name"=>"name")),
(_input "Password", "password", password("name"=>"password")),
_submit("Login!")
]}
end
def _content
[
h2 { "Login ..." },
_form
]
end
end
class UserLoginRetryView < UserLoginView
def _error_message
div("class"=>"alert-message warning") {
p { "The username or password was incorrect" }
}
end
def _content
[
h2 { "Login ..." },
_error_message,
_form
]
end
end