Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/plugins/atom_feed_helper/lib/atom_feed_helper.rb

Revision 8530, 4.3 kB (checked in by rick, 2 years ago)

Migrate atom_feed_helper changes to a plugin

Line 
1 # Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERb or any other
2 # template languages).
3 module AtomFeedHelper
4   # Full usage example:
5   #
6   #   config/routes.rb:
7   #     ActionController::Routing::Routes.draw do |map|
8   #       map.resources :posts
9   #       map.root :controller => "posts"
10   #     end
11   #
12   #   app/controllers/posts_controller.rb:
13   #     class PostsController < ApplicationController::Base
14   #       # GET /posts.html
15   #       # GET /posts.atom
16   #       def index
17   #         @posts = Post.find(:all)
18   #         
19   #         respond_to do |format|
20   #           format.html
21   #           format.atom
22   #         end
23   #       end
24   #     end
25   #
26   #   app/views/posts/index.atom.builder:
27   #     atom_feed(:tag_uri => "2008") do |feed|
28   #       feed.title("My great blog!")
29   #       feed.updated((@posts.first.created_at))
30   #     
31   #       for post in @posts
32   #         feed.entry(post) do |entry|
33   #           entry.title(post.title)
34   #           entry.content(post.body, :type => 'html')
35   #     
36   #           entry.author do |author|
37   #             author.name("DHH")
38   #           end
39   #         end
40   #       end
41   #     end
42   #
43   # The options are for atom_feed are:
44   #
45   # * <tt>:schema_date</tt>: Required. The date at which the tag scheme for the feed was first used. A good default is the year you created the feed. See http://feedvalidator.org/docs/error/InvalidTAG.html for more information.
46   # * <tt>:language</tt>: Defaults to "en-US".
47   # * <tt>:root_url</tt>: The HTML alternative that this feed is doubling for. Defaults to / on the current host.
48   # * <tt>:url</tt>: The URL for this feed. Defaults to the current URL.
49   #
50   # atom_feed yields a AtomFeedBuilder instance.
51   def atom_feed(options = {}, &block)
52     if options[:schema_date].blank?
53       logger.warn("You must provide the :schema_date option to atom_feed for your feed to be valid. A good default is the year you first created this feed.") unless logger.nil?
54     else
55       options[:schema_date] = options[:schema_date].strftime("%Y-%m-%d") if options[:schema_date].respond_to?(:strftime)
56     end
57    
58     xml = options[:xml] || eval("xml", block.binding)
59     xml.instruct!
60
61     xml.feed "xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do
62       xml.id("tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}")     
63       xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port))
64       xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url)
65      
66       yield AtomFeedBuilder.new(xml, self, options)
67     end
68   end
69
70
71   class AtomFeedBuilder
72     def initialize(xml, view, feed_options = {})
73       @xml, @view, @feed_options = xml, view, feed_options
74     end
75    
76     # Accepts a Date or Time object and inserts it in the proper format. If nil is passed, current time in UTC is used.
77     def updated(date_or_time = nil)
78       @xml.updated((date_or_time || Time.now.utc).xmlschema)
79     end
80
81     # Creates an entry tag for a specific record and prefills the id using class and id.
82     #
83     # Options:
84     #
85     # * <tt>:updated</tt>: Time of update. Defaults to the created_at attribute on the record if one such exists.
86     # * <tt>:published</tt>: Time first published. Defaults to the updated_at attribute on the record if one such exists.
87     # * <tt>:url</tt>: The URL for this entry. Defaults to the polymorphic_url for the record.
88     def entry(record, options = {})
89       @xml.entry do
90         @xml.id("tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}")
91
92         if options[:published] || (record.respond_to?(:created_at) && record.created_at)
93           @xml.published((options[:published] || record.created_at).xmlschema)
94         end
95
96         if options[:updated] || (record.respond_to?(:updated_at) && record.updated_at)
97           @xml.updated((options[:updated] || record.updated_at).xmlschema)
98         end
99
100         @xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:url] || @view.polymorphic_url(record))
101
102         yield @xml
103       end
104     end
105
106     private
107       def method_missing(method, *arguments, &block)
108         @xml.__send__(method, *arguments, &block)
109       end
110   end
111 end
Note: See TracBrowser for help on using the browser.