Friday, December 7th, 2007...9:26 am

Small Tweak to atom_feed_helper Plugin

Jump to Comments

I ran into an interesting “issue” this past week while using the atom_feed_helper plugin for Rails.

The plugin is designed to be a quick, easy-to-use way to generate ATOM feeds for your controllers. An interesting issue with the plugin is that it doesn’t allow you to manually set each ATOM entries link URL. The plugin sets the link field manually using the _polymorphic_url()_ method.


@xml.link(:rel => 'alternate', :type => 'text/html', :href => @view.polymorphic_url(record))

My problem with this arose when trying to generate a feed for a nested resource. I wanted to generate an ATOM feed for all Comments left on a certain Question in my Rails application. Comments are not viewed individually, but rather as a threaded list in the Show method of the Question to which they belong. The URL generated by the _polymorphic_url()_ call didn’t quite handle this properly.

The more I thought about this, I can see other instances as well where you might want to manually specify a certain URL for each entry link. So I went about making the following change to the plugin and am going to be submitting the patch to the Rails team as an enhancement.


def entry(record, link = nil)
  @xml.entry do
    @xml.id("tag:#{@view.request.host_with_port},2007:#{record.class}#{record.id}")
    @xml.published(record.created_at.xmlschema) if record.respond_to?(:created_at)
    @xml.updated(record.updated_at.xmlschema) if record.respond_to?(:updated_at)

    yield @xml

    entry_link = link.nil? ? @view.polymorphic_url(record) : link
    @xml.link(:rel => 'alternate', :type => 'text/html', :href => entry_link)
  end
end

Leave a Reply