Permalink
Browse files

Fix atom_feed_helper to comply with the atom spec. Closes #10672 [xav…

…iershay]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8529 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4261b83 commit 744b1d7f4d2df583c77f99a33ae560ad64414c66 @technoweenie technoweenie committed Jan 2, 2008
@@ -1,5 +1,12 @@
*SVN*
+* Fix atom_feed_helper to comply with the atom spec. Closes #10672 [xaviershay]
+
+ * The tags created do not contain a date (http://feedvalidator.org/docs/error/InvalidTAG.html)
+ * IDs are not guaranteed unique
+ * A default self link was not provided, contrary to the documentation
+ * NOTE: This changes tags for existing atom entries, but at least they validate now.
+
* Correct indentation in tests. Closes #10671 [l.guidi]
* Fix that auto_link looks for ='s in url paths (Amazon urls have them). Closes #10640 [bgreenlee]
@@ -26,7 +26,7 @@ module AtomFeedHelper
# end
#
# app/views/posts/index.atom.builder:
- # atom_feed do |feed|
+ # atom_feed(:tag_uri => "2008") do |feed|
# feed.title("My great blog!")
# feed.updated((@posts.first.created_at))
#
@@ -44,31 +44,35 @@ module AtomFeedHelper
#
# The options are for atom_feed are:
#
+ # * <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.
# * <tt>:language</tt>: Defaults to "en-US".
# * <tt>:root_url</tt>: The HTML alternative that this feed is doubling for. Defaults to / on the current host.
# * <tt>:url</tt>: The URL for this feed. Defaults to the current URL.
#
# atom_feed yields a AtomFeedBuilder instance.
def atom_feed(options = {}, &block)
+ if options[:schema_date].blank?
+ 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?
+ else
+ options[:schema_date] = options[:schema_date].strftime("%Y-%m-%d") if options[:schema_date].respond_to?(:strftime)
+ end
+
xml = options[:xml] || eval("xml", block.binding)
xml.instruct!
xml.feed "xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do
- xml.id("tag:#{request.host}:#{request.request_uri.split(".")[0].gsub("/", "")}")
+ xml.id("tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}")
xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port))
-
- if options[:url]
- xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url)
- end
-
- yield AtomFeedBuilder.new(xml, self)
+ xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url)
+
+ yield AtomFeedBuilder.new(xml, self, options)
end
end
class AtomFeedBuilder
- def initialize(xml, view)
- @xml, @view = xml, view
+ def initialize(xml, view, feed_options = {})
+ @xml, @view, @feed_options = xml, view, feed_options
end
# Accepts a Date or Time object and inserts it in the proper format. If nil is passed, current time in UTC is used.
@@ -85,7 +89,7 @@ def updated(date_or_time = nil)
# * <tt>:url</tt>: The URL for this entry. Defaults to the polymorphic_url for the record.
def entry(record, options = {})
@xml.entry do
- @xml.id("tag:#{@view.request.host_with_port}:#{record.class}#{record.id}")
+ @xml.id("tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}")
if options[:published] || (record.respond_to?(:created_at) && record.created_at)
@xml.published((options[:published] || record.created_at).xmlschema)
@@ -102,10 +106,10 @@ def entry(record, options = {})
end
private
- def method_missing(method, *arguments)
- @xml.__send__(method, *arguments)
+ def method_missing(method, *arguments, &block)
+ @xml.__send__(method, *arguments, &block)
end
end
end
end
-end
+end
@@ -5,7 +5,7 @@
class ScrollsController < ActionController::Base
FEEDS = {}
FEEDS["defaults"] = <<-EOT
- atom_feed do |feed|
+ atom_feed(:schema_date => '2008') do |feed|
feed.title("My great blog!")
feed.updated((@scrolls.first.created_at))
@@ -38,7 +38,23 @@ class ScrollsController < ActionController::Base
end
end
EOT
+ FEEDS["xml_block"] = <<-EOT
+ atom_feed do |feed|
+ feed.title("My great blog!")
+ feed.updated((@scrolls.first.created_at))
+ feed.author do |author|
+ author.name("DHH")
+ end
+
+ for scroll in @scrolls
+ feed.entry(scroll, :url => "/otherstuff/" + scroll.to_param, :updated => Time.utc(2007, 1, scroll.id)) do |entry|
+ entry.title(scroll.title)
+ entry.content(scroll.body, :type => 'html')
+ end
+ end
+ end
+ EOT
def index
@scrolls = [
Scroll.new(1, "1", "Hello One", "Something <i>COOL!</i>", Time.utc(2007, 12, 12, 15), Time.utc(2007, 12, 12, 15)),
@@ -47,6 +63,12 @@ def index
render :inline => FEEDS[params[:id]], :type => :builder
end
+
+ protected
+
+ def rescue_action(e)
+ raise(e)
+ end
end
class AtomFeedTest < Test::Unit::TestCase
@@ -88,6 +110,34 @@ def test_entry_with_prefilled_options_should_use_those_instead_of_querying_the_r
end
end
+ def test_self_url_should_default_to_current_request_url
+ with_restful_routing(:scrolls) do
+ get :index, :id => "defaults"
+ assert_select "link[rel=self][href=http://www.nextangle.com/scrolls?id=defaults]"
+ end
+ end
+
+ def test_feed_id_should_be_a_valid_tag
+ with_restful_routing(:scrolls) do
+ get :index, :id => "defaults"
+ assert_select "id", :text => "tag:www.nextangle.com,2008:/scrolls?id=defaults"
+ end
+ end
+
+ def test_entry_id_should_be_a_valid_tag
+ with_restful_routing(:scrolls) do
+ get :index, :id => "defaults"
+ assert_select "entry id", :text => "tag:www.nextangle.com,2008:Scroll/1"
+ assert_select "entry id", :text => "tag:www.nextangle.com,2008:Scroll/2"
+ end
+ end
+
+ def test_feed_should_allow_nested_xml_blocks
+ with_restful_routing(:scrolls) do
+ get :index, :id => "xml_block"
+ assert_select "author name", :text => "DHH"
+ end
+ end
private
def with_restful_routing(resources)
@@ -98,4 +148,4 @@ def with_restful_routing(resources)
yield
end
end
-end
+end

0 comments on commit 744b1d7

Please sign in to comment.