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

Changeset 8530

Show
Ignore:
Timestamp:
01/02/08 19:47:39 (5 months ago)
Author:
rick
Message:

Migrate atom_feed_helper changes to a plugin

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/atom_feed_helper/lib/atom_feed_helper.rb

    r8517 r8530  
    22# template languages). 
    33module AtomFeedHelper 
    4   # Available Options
     4  # Full usage example
    55  # 
    6   #   :root_url - Adds a <link rel="alternate" type="text/html" ... /> tag for the feed. 
    7   #   :url - Adds a <link rel="self" type="application/atom+xml" ... /> tag for the feed. 
     6  #   config/routes.rb: 
     7  #     ActionController::Routing::Routes.draw do |map| 
     8  #       map.resources :posts 
     9  #       map.root :controller => "posts" 
     10  #     end 
    811  # 
     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. 
    951  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     
    1058    xml = options[:xml] || eval("xml", block.binding) 
    1159    xml.instruct! 
    1260 
    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) 
    1565       
    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) 
    2567    end 
    2668  end 
    2769 
    2870 
    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 
    33103      end 
     104    end 
    34105 
    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) 
    51109      end 
    52  
    53       private 
    54         def method_missing(method, *arguments, &block) 
    55           @xml.__send__(method, *arguments, &block) 
    56         end 
    57     end 
     110  end 
    58111end