| | 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. |
|---|
| 13 | | xml.feed "xml:lang" => "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do |
|---|
| 14 | | xml.id("tag:#{request.host},#{Time.now.utc.year}:#{request.request_uri.split(".")[0].gsub("/", "")}") |
|---|
| | 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) |
|---|
| 16 | | if options[:root_url] || respond_to?(:root_url) |
|---|
| 17 | | xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || root_url) |
|---|
| 18 | | end |
|---|
| 19 | | |
|---|
| 20 | | if options[:url] |
|---|
| 21 | | xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url]) |
|---|
| 22 | | end |
|---|
| 23 | | |
|---|
| 24 | | yield AtomFeedBuilder.new(xml, self) |
|---|
| | 66 | yield AtomFeedBuilder.new(xml, self, options) |
|---|
| 29 | | protected |
|---|
| 30 | | class AtomFeedBuilder |
|---|
| 31 | | def initialize(xml, view) |
|---|
| 32 | | @xml, @view = xml, view |
|---|
| | 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 |
|---|
| 35 | | # Available Options: |
|---|
| 36 | | # |
|---|
| 37 | | # :url - Adds a <link rel="alternate" type="text/html" ... /> tag for the given record. |
|---|
| 38 | | # :published - Uses the given field to set the <published> tag value. Defaults to #created_at. |
|---|
| 39 | | def entry(record, options = {}) |
|---|
| 40 | | published = record.created_at if record.respond_to?(:created_at) |
|---|
| 41 | | |
|---|
| 42 | | @xml.entry do |
|---|
| 43 | | @xml.id("tag:#{@view.request.host_with_port},#{(published || Time.now.utc).year}:#{record.class}#{record.id}") |
|---|
| 44 | | @xml.published(published.xmlschema) if published |
|---|
| 45 | | @xml.updated(record.updated_at.xmlschema) if record.respond_to?(:updated_at) |
|---|
| 46 | | |
|---|
| 47 | | yield @xml |
|---|
| 48 | | |
|---|
| 49 | | @xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:url] || @view.polymorphic_url(record)) |
|---|
| 50 | | end |
|---|
| | 106 | private |
|---|
| | 107 | def method_missing(method, *arguments, &block) |
|---|
| | 108 | @xml.__send__(method, *arguments, &block) |
|---|