Skip to content

Commit

Permalink
Provide overrideable attr methods in blogging helper
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 12, 2016
1 parent 6831551 commit 9082362
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/nanoc/helpers/blogging.rb
@@ -1,9 +1,21 @@
module Nanoc::Helpers
# @see http://nanoc.ws/doc/reference/helpers/#blogging
module Blogging
# @param item
#
# @return [Boolean]
def article?(item)
item[:kind] == 'article'
end

# TODO document
def article_created_at(item)
attribute_to_time(item[:created_at])
end

# @return [Array]
def articles
blk = -> { @items.select { |item| item[:kind] == 'article' } }
blk = -> { @items.select { |item| article?(item) } }
if @items.frozen?
@article_items ||= blk.call
else
Expand All @@ -13,7 +25,7 @@ def articles

# @return [Array]
def sorted_articles
blk = -> { articles.sort_by { |a| attribute_to_time(a[:created_at]) }.reverse }
blk = -> { articles.sort_by { |a| article_created_at(a) }.reverse }

if @items.frozen?
@sorted_article_items ||= blk.call
Expand Down Expand Up @@ -62,7 +74,7 @@ def sorted_relevant_articles
all = relevant_articles

unless @preserve_order
all = all.sort_by { |a| attribute_to_time(a[:created_at]) }
all = all.sort_by { |a| article_created_at(a) }
end

all.reverse.first(limit)
Expand Down Expand Up @@ -94,7 +106,7 @@ def validate_articles
if relevant_articles.empty?
raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no articles')
end
if relevant_articles.any? { |a| a[:created_at].nil? }
if relevant_articles.any? { |a| article_created_at(a).nil? }
raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: one or more articles lack created_at')
end
end
Expand All @@ -109,7 +121,7 @@ def build_for_feed(xml)
xml.title title

# Add date
xml.updated(attribute_to_time(last_article[:created_at]).__nanoc_to_iso8601_time)
xml.updated(article_created_at(last_article).__nanoc_to_iso8601_time)

# Add links
xml.link(rel: 'alternate', href: root_url)
Expand Down

0 comments on commit 9082362

Please sign in to comment.