Peel me a Grape :: We make things work

We Make Things Work :: Blog

The Atom spec mandates the use of link elements with rel=next|prev to support paginating collections of entries. The Atom Wiki has a page explaining the meaning of various link tags. A developerWorks article on Paging Atom suggests rel=first|last are also standard. last is useful to indicate how large the collection is, first is meaningful in the context of pagination – self has the same meaning – albeit somewhat ambiguous in this context (is it url for this page or url for the first page?).

Anyways…. I’d like to paginate my atom feeds with the same ease I paginate my html views with will_paginate

<%= will_paginate @articles %>
simplest way is with a helper method giving you something like
will_paginate_atom(@articles, builder)
with a helper like this:

def will_paginate_atom(collection, builder)
    total_pages = WillPaginate::ViewHelpers.total_pages_for_collection(collection)
    builder.link(:rel => 'next', :href => atom_url(:page => collection.current_page + 1)) unless collection.current_page.eql?(total_pages)
    builder.link(:rel => 'prev', :href => atom_url(:page => collection.current_page - 1)) unless collection.current_page.eql?(1)
    builder.link(:rel => 'last', :href => atom_url(:page => total_pages))
end

We could monkey patch atom_feed_helper to allow even nicer syntax. TODO.

atom_feed do |feed|
....
  feed.will_paginate(@articles)
.... 
end

Note: Feedburner doesn’t support paginating like this at the moment – so your next/prev/last links won’t be rewritten as feedburner.

Happy paginating!

blog comments powered by Disqus