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

Changeset 8529

Show
Ignore:
Timestamp:
01/02/08 19:45:53 (4 months ago)
Author:
rick
Message:

Fix atom_feed_helper to comply with the atom spec. Closes #10672 [xaviershay]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8527 r8529  
    11*SVN* 
     2 
     3* Fix atom_feed_helper to comply with the atom spec.  Closes #10672 [xaviershay] 
     4 
     5  * The tags created do not contain a date (http://feedvalidator.org/docs/error/InvalidTAG.html) 
     6  * IDs are not guaranteed unique 
     7  * A default self link was not provided, contrary to the documentation 
     8  * NOTE:  This changes tags for existing atom entries, but at least they validate now. 
    29 
    310* Correct indentation in tests.  Closes #10671 [l.guidi] 
  • trunk/actionpack/lib/action_view/helpers/atom_feed_helper.rb

    r7529 r8529  
    2727      # 
    2828      #   app/views/posts/index.atom.builder: 
    29       #     atom_feed do |feed| 
     29      #     atom_feed(:tag_uri => "2008") do |feed| 
    3030      #       feed.title("My great blog!") 
    3131      #       feed.updated((@posts.first.created_at)) 
     
    4545      # The options are for atom_feed are: 
    4646      # 
     47      # * <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. 
    4748      # * <tt>:language</tt>: Defaults to "en-US". 
    4849      # * <tt>:root_url</tt>: The HTML alternative that this feed is doubling for. Defaults to / on the current host. 
     
    5152      # atom_feed yields a AtomFeedBuilder instance. 
    5253      def atom_feed(options = {}, &block) 
     54        if options[:schema_date].blank? 
     55          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? 
     56        else 
     57          options[:schema_date] = options[:schema_date].strftime("%Y-%m-%d") if options[:schema_date].respond_to?(:strftime) 
     58        end 
     59         
    5360        xml = options[:xml] || eval("xml", block.binding) 
    5461        xml.instruct! 
    5562 
    5663        xml.feed "xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do 
    57           xml.id("tag:#{request.host}:#{request.request_uri.split(".")[0].gsub("/", "")}")       
     64          xml.id("tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}")       
    5865          xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port)) 
    59  
    60           if options[:url] 
    61             xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url) 
    62           end 
    63  
    64           yield AtomFeedBuilder.new(xml, self) 
     66          xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url) 
     67           
     68          yield AtomFeedBuilder.new(xml, self, options) 
    6569        end 
    6670      end 
     
    6872 
    6973      class AtomFeedBuilder 
    70         def initialize(xml, view
    71           @xml, @view = xml, view 
     74        def initialize(xml, view, feed_options = {}
     75          @xml, @view, @feed_options = xml, view, feed_options 
    7276        end 
    7377         
     
    8690        def entry(record, options = {}) 
    8791          @xml.entry do  
    88             @xml.id("tag:#{@view.request.host_with_port}:#{record.class}#{record.id}") 
     92            @xml.id("tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}") 
    8993 
    9094            if options[:published] || (record.respond_to?(:created_at) && record.created_at) 
     
    103107 
    104108        private 
    105           def method_missing(method, *arguments
    106             @xml.__send__(method, *arguments
     109          def method_missing(method, *arguments, &block
     110            @xml.__send__(method, *arguments, &block
    107111          end 
    108112      end 
  • trunk/actionpack/test/template/atom_feed_helper_test.rb

    r7533 r8529  
    66  FEEDS = {} 
    77  FEEDS["defaults"] = <<-EOT 
    8         atom_feed do |feed| 
     8        atom_feed(:schema_date => '2008') do |feed| 
    99          feed.title("My great blog!") 
    1010          feed.updated((@scrolls.first.created_at)) 
     
    3939        end 
    4040    EOT 
     41    FEEDS["xml_block"] = <<-EOT 
     42        atom_feed do |feed| 
     43          feed.title("My great blog!") 
     44          feed.updated((@scrolls.first.created_at)) 
    4145 
     46          feed.author do |author| 
     47            author.name("DHH") 
     48          end 
     49 
     50          for scroll in @scrolls 
     51            feed.entry(scroll, :url => "/otherstuff/" + scroll.to_param, :updated => Time.utc(2007, 1, scroll.id)) do |entry| 
     52              entry.title(scroll.title) 
     53              entry.content(scroll.body, :type => 'html') 
     54            end 
     55          end 
     56        end 
     57    EOT 
    4258  def index 
    4359    @scrolls = [ 
     
    4763     
    4864    render :inline => FEEDS[params[:id]], :type => :builder 
     65  end 
     66 
     67  protected 
     68 
     69  def rescue_action(e) 
     70    raise(e) 
    4971  end 
    5072end 
     
    89111  end 
    90112 
     113  def test_self_url_should_default_to_current_request_url 
     114    with_restful_routing(:scrolls) do 
     115      get :index, :id => "defaults" 
     116      assert_select "link[rel=self][href=http://www.nextangle.com/scrolls?id=defaults]" 
     117    end 
     118  end 
     119 
     120  def test_feed_id_should_be_a_valid_tag 
     121    with_restful_routing(:scrolls) do 
     122      get :index, :id => "defaults" 
     123      assert_select "id", :text => "tag:www.nextangle.com,2008:/scrolls?id=defaults" 
     124    end 
     125  end 
     126 
     127  def test_entry_id_should_be_a_valid_tag 
     128    with_restful_routing(:scrolls) do 
     129      get :index, :id => "defaults" 
     130      assert_select "entry id", :text => "tag:www.nextangle.com,2008:Scroll/1" 
     131      assert_select "entry id", :text => "tag:www.nextangle.com,2008:Scroll/2" 
     132    end 
     133  end 
     134 
     135  def test_feed_should_allow_nested_xml_blocks 
     136    with_restful_routing(:scrolls) do 
     137      get :index, :id => "xml_block" 
     138      assert_select "author name", :text => "DHH" 
     139    end 
     140  end 
    91141 
    92142  private