Skip to content

Instantly share code, notes, and snippets.

@bitben
Created March 24, 2014 08:08
Show Gist options
  • Save bitben/9736126 to your computer and use it in GitHub Desktop.
Save bitben/9736126 to your computer and use it in GitHub Desktop.
module CategoriesTags
extend Nanoc::Memoization
def all_categories
categories = []
posts.each do |post|
next if post[:categories].nil?
categories << post[:categories]
end
categories.uniq
end
memoize :all_categories
def has_category?(category, post)
if post[:categories].nil?
false
else
post[:categories].include?(category)
end
end
def news_with_category(category, news=sorted_posts)
news.select { |post| has_category?(category, post) }
end
memoize :news_with_category
def news_by_category(news=posts)
categories = []
all_categories.each do |category|
categories << [category, news_with_category(category)]
end
categories
end
memoize :news_by_category
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment