lib/nanoc/helpers/blogging.rb in nanoc-3.7.5 vs lib/nanoc/helpers/blogging.rb in nanoc-3.8.0
- old
+ new
@@ -54,10 +54,11 @@
attr_accessor :site
attr_accessor :limit
attr_accessor :relevant_articles
+ attr_accessor :preserve_order
attr_accessor :content_proc
attr_accessor :excerpt_proc
attr_accessor :title
attr_accessor :author_name
attr_accessor :author_uri
@@ -83,13 +84,17 @@
end
protected
def sorted_relevant_articles
- relevant_articles.sort_by do |a|
- attribute_to_time(a[:created_at])
- end.reverse.first(limit)
+ all = relevant_articles
+
+ unless @preserve_order
+ all = all.sort_by { |a| attribute_to_time(a[:created_at]) }
+ end
+
+ all.reverse.first(limit)
end
def last_article
sorted_relevant_articles.first
end
@@ -170,11 +175,11 @@
# Add specific author information
if a[:author_name] || a[:author_uri]
xml.author do
xml.name a[:author_name] || author_name
- xml.uri a[:author_uri] || author_uri
+ xml.uri a[:author_uri] || author_uri
end
end
# Add link
xml.link(rel: 'alternate', href: url)
@@ -266,13 +271,18 @@
# <%= atom_feed :limit => 5 %>
#
# @option params [Number] :limit (5) The maximum number of articles to
# show
#
- # @option params [Array] :articles (sorted_articles) A list of articles to
- # include in the feed
+ # @option params [Array] :articles (articles) A list of articles to include
+ # in the feed
#
+ # @option params [Boolean] :preserve_order (false) Whether or not the
+ # ordering of the list of articles should be preserved. If false, the
+ # articles will be sorted by `created_at`. If true, the list of articles
+ # will be used as-is, and should have the most recent articles last.
+ #
# @option params [Proc] :content_proc (->{ |article|
# article.compiled_content(:snapshot => :pre) }) A proc that returns the
# content of the given article, which is passed as a parameter. This
# function may not return nil.
#
@@ -301,9 +311,10 @@
builder = AtomFeedBuilder.new(@site, @item)
# Fill builder
builder.limit = params[:limit] || 5
builder.relevant_articles = params[:articles] || articles || []
+ builder.preserve_order = params.fetch(:preserve_order, false)
builder.content_proc = params[:content_proc] || ->(a) { a.compiled_content(snapshot: :pre) }
builder.excerpt_proc = params[:excerpt_proc] || ->(a) { a[:excerpt] }
builder.title = params[:title] || @item[:title] || @site.config[:title]
builder.author_name = params[:author_name] || @item[:author_name] || @site.config[:author_name]
builder.author_uri = params[:author_uri] || @item[:author_uri] || @site.config[:author_uri]