This repository has been archived by the owner on Mar 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
blog.rb
95 lines (81 loc) · 2.45 KB
/
blog.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
module SchnitzelPress
module Actions
module Blog
extend ActiveSupport::Concern
included do
get '/' do
@show_description = true
if @post = Post.published.pages.where(:slugs => 'home').first
render_post
else
render_blog
end
end
get '/blog/?' do
@show_description = true
render_blog
end
def render_blog
total_count = Post.latest.count
skipped_count = params[:page].to_i * 10
@posts = Post.latest.skip(skipped_count).limit(10)
displayed_count = @posts.count(true)
@show_previous_posts_button = total_count > skipped_count + displayed_count
haml :index
end
get '/blog.css' do
cache_for 1.hour
scss :blog
end
get '/posts.atom' do
cache_for 3.minutes
@posts = Post.latest.limit(10)
content_type 'application/atom+xml; charset=utf-8'
haml :atom, :format => :xhtml, :layout => false
end
get '/feed/?' do
redirect settings.feed_url
end
get %r{^/(\d{4})/(\d{1,2})/(\d{1,2})/?$} do
year, month, day = params[:captures]
@posts = Post.latest.for_day(year.to_i, month.to_i, day.to_i)
haml :index
end
get %r{^/(\d{4})/(\d{1,2})/?$} do
year, month = params[:captures]
@posts = Post.latest.for_month(year.to_i, month.to_i)
haml :index
end
get %r{^/(\d{4})/?$} do
year = params[:captures].first
@posts = Post.latest.for_year(year.to_i)
haml :index
end
get '/:year/:month/:day/:slug/?' do |year, month, day, slug|
@post = Post.
for_day(year.to_i, month.to_i, day.to_i).
where(:slugs => slug).first
render_post
end
get '/*/?' do
slug = params[:splat].first
@post = Post.where(:slugs => slug).first
render_post
end
def render_post(enforce_canonical_url = true)
if @post
# enforce canonical URL
if enforce_canonical_url && request.path != url_for(@post)
redirect url_for(@post)
else
@show_description = (@post.slug == 'home')
haml :post
end
else
halt 404
end
end
end
end
end
end