From 87e422171ca99a8d47c3dbd47bc060afe7a520c1 Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Tue, 9 Sep 2008 15:07:28 -0400 Subject: [PATCH] Add XHTML support to the atom_feed_helper --- .../lib/action_view/helpers/atom_feed_helper.rb | 28 ++++++++++++++---- actionpack/test/template/atom_feed_helper_test.rb | 30 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_view/helpers/atom_feed_helper.rb b/actionpack/lib/action_view/helpers/atom_feed_helper.rb index e65d5d1..fb7383b 100644 --- a/actionpack/lib/action_view/helpers/atom_feed_helper.rb +++ b/actionpack/lib/action_view/helpers/atom_feed_helper.rb @@ -98,8 +98,26 @@ module ActionView end end + class AtomBuilder + def initialize(xml) + @xml = xml + end + private + def method_missing(method, *arguments, &block) + if %w[content rights title subtitle summary].include? method.to_s and + arguments.last.respond_to? :[] and arguments.last[:type].to_s == 'xhtml' + @xml.__send__(method, *arguments) do + @xml.div :xmlns=>'http://www.w3.org/1999/xhtml' do |xhtml| + block.call(xhtml) + end + end + else + @xml.__send__(method, *arguments, &block) + end + end + end - class AtomFeedBuilder + class AtomFeedBuilder < AtomBuilder def initialize(xml, view, feed_options = {}) @xml, @view, @feed_options = xml, view, feed_options end @@ -131,15 +149,11 @@ module ActionView @xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:url] || @view.polymorphic_url(record)) - yield @xml + yield AtomBuilder.new(@xml) end end - - private - def method_missing(method, *arguments, &block) - @xml.__send__(method, *arguments, &block) - end end + end end end diff --git a/actionpack/test/template/atom_feed_helper_test.rb b/actionpack/test/template/atom_feed_helper_test.rb index ef31ab2..151335b 100644 --- a/actionpack/test/template/atom_feed_helper_test.rb +++ b/actionpack/test/template/atom_feed_helper_test.rb @@ -92,6 +92,28 @@ class ScrollsController < ActionController::Base end end EOT + FEEDS["feed_with_xhtml_content"] = <<-'EOT' + atom_feed 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.summary(:type => 'xhtml') do |xhtml| + xhtml.p "before #{scroll.id}" + xhtml.p {xhtml << scroll.body} + xhtml.p "after #{scroll.id}" + end + 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)), @@ -194,6 +216,14 @@ class AtomFeedTest < Test::Unit::TestCase end end + def test_feed_xhtml + with_restful_routing(:scrolls) do + get :index, :id => "feed_with_xhtml_content" + assert_match %r{xmlns="http://www.w3.org/1999/xhtml"}, @response.body + assert_select "summary div p", :text => "Something Boring" + assert_select "summary div p", :text => "after 2" + end + end private def with_restful_routing(resources) with_routing do |set| -- 1.5.2.5