-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.rb
More file actions
86 lines (64 loc) · 1.82 KB
/
models.rb
File metadata and controls
86 lines (64 loc) · 1.82 KB
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
class Post < Maneki
path 'posts'
# Grab a handful of posts
def self.page (n, per_page = 5)
n = 1 unless n
start = (n.to_i - 1) * per_page
return [] if start >= all.size
finish = start + per_page
finish = all.size if finish > all.size
all.sort[start...finish]
end
# Archive posts
def self.archive
years_months_posts = all.inject({}) do |archive, post|
year_month = Time.local(post.published_at.year, post.published_at.month)
archive[year_month] ||= []
archive[year_month] << post
archive
end
years_months_posts.sort.reverse
end
# Search for some posts
def self.search (keywords)
found = {}
keywords.split(' ').each do |keyword|
posts = find :match => :any, :title => keyword, :body => keyword, :headers => { :tags => keyword }
# First lot of results is our base
if found.empty?
found = posts
end
# Then remove anything that doesn't match all keywords
found = found.select { |post| posts.include? post }
end
found.sort
end
# Find posts that are tagged with a given tag
def self.find_tagged_with (tag)
posts = Post.find :headers => { :tags => tag }
posts.sort
end
# This post's tags
def tags
@headers[:tags]
end
# When a post was published
def published_at
Chronic.parse(@headers[:published]) if @headers[:published]
end
# Some posts are just links
def link
@headers[:link]
end
# Check if this post is valid
def valid?
return false if @headers.nil?
return false if published_at.nil? or published_at > Time.now.getlocal
return false if tags.nil? or tags.empty?
true
end
# Sort by publish date in reverse order
def <=> (rhs)
rhs.published_at <=> published_at
end
end