From 7e1c04d86691075d172bccb8bc1c7df2e71383c3 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sun, 13 Jan 2008 00:11:39 +0000 Subject: Allow users to declare other namespaces when using the atom feed helpers. Closes #10304 [david.calavera] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8637 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ .../lib/action_view/helpers/atom_feed_helper.rb | 30 ++++++++++++++++++++-- actionpack/test/template/atom_feed_helper_test.rb | 28 ++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 310f973502..e89123e679 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Allow users to declare other namespaces when using the atom feed helpers. #10304 [david.calavera] + * Introduce send_file :x_sendfile => true to send an X-Sendfile response header. [Jeremy Kemper] * Fixed ActionView::Helpers::ActiveRecordHelper::form for when protect_from_forgery is used #10739 [jeremyevans] diff --git a/actionpack/lib/action_view/helpers/atom_feed_helper.rb b/actionpack/lib/action_view/helpers/atom_feed_helper.rb index 96721b5b03..aee709a116 100644 --- a/actionpack/lib/action_view/helpers/atom_feed_helper.rb +++ b/actionpack/lib/action_view/helpers/atom_feed_helper.rb @@ -42,13 +42,36 @@ module ActionView # end # end # - # The options are for atom_feed are: + # The options for atom_feed are: # # * :schema_date: 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. # * :language: Defaults to "en-US". # * :root_url: The HTML alternative that this feed is doubling for. Defaults to / on the current host. # * :url: The URL for this feed. Defaults to the current URL. # + # Other namespaces can be added to the root element: + # + # app/views/posts/index.atom.builder: + # atom_feed({'xmlns:app' => 'http://www.w3.org/2007/app', + # 'xmlns:openSearch' => 'http://a9.com/-/spec/opensearch/1.1/'}) do |feed| + # feed.title("My great blog!") + # feed.updated((@posts.first.created_at)) + # feed.tag!(openSearch:totalResults, 10) + # + # for post in @posts + # feed.entry(post) do |entry| + # entry.title(post.title) + # entry.content(post.body, :type => 'html') + # entry.tag!('app:edited', Time.now) + # + # entry.author do |author| + # author.name("DHH") + # end + # end + # end + # end + # + # # atom_feed yields an AtomFeedBuilder instance. def atom_feed(options = {}, &block) if options[:schema_date].blank? @@ -60,7 +83,10 @@ module ActionView 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 + feed_opts = {"xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom'} + feed_opts.merge!(options).reject!{|k,v| !k.to_s.match(/^xml/)} + + xml.feed(feed_opts) do 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)) xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url) diff --git a/actionpack/test/template/atom_feed_helper_test.rb b/actionpack/test/template/atom_feed_helper_test.rb index 9efc29fbdf..9f7e5b4c6c 100644 --- a/actionpack/test/template/atom_feed_helper_test.rb +++ b/actionpack/test/template/atom_feed_helper_test.rb @@ -55,6 +55,25 @@ class ScrollsController < ActionController::Base end end EOT + FEEDS["feed_with_atomPub_namespace"] = <<-EOT + atom_feed({'xmlns:app' => 'http://www.w3.org/2007/app', + 'xmlns:openSearch' => 'http://a9.com/-/spec/opensearch/1.1/'}) do |feed| + feed.title("My great blog!") + feed.updated((@scrolls.first.created_at)) + + for scroll in @scrolls + feed.entry(scroll) do |entry| + entry.title(scroll.title) + entry.content(scroll.body, :type => 'html') + entry.tag!('app:edited', Time.now) + + entry.author do |author| + author.name("DHH") + end + end + end + end + EOT def index @scrolls = [ Scroll.new(1, "1", "Hello One", "Something COOL!", Time.utc(2007, 12, 12, 15), Time.utc(2007, 12, 12, 15)), @@ -139,6 +158,15 @@ class AtomFeedTest < Test::Unit::TestCase end end + def test_feed_should_include_atomPub_namespace + with_restful_routing(:scrolls) do + get :index, :id => "feed_with_atomPub_namespace" + assert_match %r{xml:lang="en-US"}, @response.body + assert_match %r{xmlns="http://www.w3.org/2005/Atom"}, @response.body + assert_match %r{xmlns:app="http://www.w3.org/2007/app"}, @response.body + end + end + private def with_restful_routing(resources) with_routing do |set| -- cgit v1.2.3