This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
Original Post: What's New in Edge Rails: Get Your RSS & Atom Feeds for Free
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Rails is all about some open-ness and interopability and it has become incrementally easier to achieve this with the newResource Feeder plugin. This plugin gives you an easy way to create RSS and Atom feeds in your controllers from a collection of model objects with the use of the rss_feed_for and atom_feed_for methods:
class PostsController < ActionController
# Build an rss feed
def rss
rss_feed_for Post.find(:all, :order => 'created_at',
:limit => 10)
end
# Build an atom feed
def atom
atom_feed_for Post.find(:all, :order => 'created_at',
:limit => 10)
end
end
So how do these feed methods know how to pull information from each individual resource of the collection? It uses a combination of options passed into the feed methods and specific properties of the resource. If you don’t pass any options into these feed methods your model objects should have title, description and created_at reader methods (and updated_at for atom).
Options
There are several options you can pass into these nice little feed methods to customize the title of the feed and most of the other feed elements. Here’s my attempt at enumerating all of them for RSS:
Feed options (w/ defaults):
options[:feed][:title] ; Pluralization of the model class, i.e. “Posts”
options[:feed][:link] ; Url to this record
options[:feed][:description] ; omitted if not specified
options[:feed][:language] ; “en-us”
options[:feed][:ttl] ; 40
Individual item options (if specified will be used for all items)
options[:item][:title]
options[:item][:description]
options[:item][:pub_date]
General options:
options[:url_writer] ; an instance of UrlWriter for your app (I think? Help me out here!)
Just pass in these options to your feed methods as needed:
class PostsController < ActionController
# Build an rss feed
def rss
rss_feed_for Post.find(:all, :order => 'created_at',
:limit => 10),
{ :feed => {:title => "All posts"},
:item => {:pub_date => Time.now} }
end
end
class PostsController < ActionController
# View a collection of posts. If "format" is specified
# will return view of that type (of either rss or atom format)
# /posts/list?format=rss => RSS
# /posts/list?format=atom => Atom
def list
@posts = Post.find(:all, :order => 'created_at',
:limit => 10)
options = { :feed => {:title => "All posts"},
:item => {:pub_date => Time.now} }
respond_to do |wants|
wants.html
wants.rss { rss_feed_for @posts, options }
wants.atom { atom_feed_for @posts, options }
end
end
end