"Referred article" functionality

This commit is contained in:
Stefan Schlott 2013-08-24 13:50:40 +02:00
parent 607b76daab
commit 29a963733d
3 changed files with 29 additions and 21 deletions

View file

@ -19,6 +19,16 @@ def pathname_of_article(item)
time.strftime('%Y-%m-%d') + '-' + slug
end
def sanitize_path(path)
result = path
if (!path.start_with?('/'))
result = '/' + result
end
if (!path.end_with?('/'))
result = result + '/'
end
result
end
def latest_articles(max=nil)
@cache_latest_art ||= @site.items.select do |p|
@ -29,3 +39,12 @@ def latest_articles(max=nil)
@cache_latest_art[0..(max ? max-1 : @cache_latest_art.length-1)]
end
def latest_articles_referred_to(refers_to, max=nil)
refers_to = sanitize_path(refers_to)
@site.items.select do |p|
p.attributes[:kind]=='article' && p.attributes[:refers_to] && sanitize_path(p.attributes[:refers_to])==refers_to
end.sort do |a, b|
a.attributes[:created_at] <=> b.attributes[:created_at]
end.reverse[0..(max ? max-1 : @cache_latest_art.length-1)]
end