Permalink
Browse files

Added inline builder yield to atom_feed_helper tags where appropriate…

… (Sam Ruby) [#8994 status:committed]
  • Loading branch information...
1 parent 691aa20 commit 1abdc8752d7fbc0733da763e751a2671db42961a @dhh dhh committed Oct 13, 2008
View
@@ -1,5 +1,13 @@
*Edge*
+* Added inline builder yield to atom_feed_helper tags where appropriate [Sam Ruby]. Example:
+
+ entry.summary :type => 'xhtml' do |xhtml|
+ xhtml.p pluralize(order.line_items.count, "line item")
+ xhtml.p "Shipped to #{order.address}"
+ xhtml.p "Paid by #{order.pay_type}"
+ end
+
* Make PrototypeHelper#submit_to_remote a wrapper around PrototypeHelper#button_to_remote. [Tarmo Tänav]
* Set HttpOnly for the cookie session store's cookie. #1046
@@ -75,8 +75,20 @@ module AtomFeedHelper
# end
# end
#
+ # The Atom spec defines five elements (content rights title subtitle
+ # summary) which may directly contain xhtml content if :type => 'xhtml'
+ # is specified as an attribute. If so, this helper will take care of
+ # the enclosing div and xhtml namespace declaration. Example usage:
#
- # atom_feed yields an AtomFeedBuilder instance.
+ # entry.summary :type => 'xhtml' do |xhtml|
+ # xhtml.p pluralize(order.line_items.count, "line item")
+ # xhtml.p "Shipped to #{order.address}"
+ # xhtml.p "Paid by #{order.pay_type}"
+ # end
+ #
+ #
+ # atom_feed yields an AtomFeedBuilder instance. Nested elements yield
+ # an AtomBuilder instance.
def atom_feed(options = {}, &block)
if options[:schema_date]
options[:schema_date] = options[:schema_date].strftime("%Y-%m-%d") if options[:schema_date].respond_to?(:strftime)
@@ -108,8 +120,38 @@ def atom_feed(options = {}, &block)
end
end
+ class AtomBuilder
+ def initialize(xml)
+ @xml = xml
+ end
- class AtomFeedBuilder
+ private
+ # Delegate to xml builder, first wrapping the element in a xhtml
+ # namespaced div element if the method and arguments indicate
+ # that an xhtml_block? is desired.
+ def method_missing(method, *arguments, &block)
+ if xhtml_block?(method, arguments)
+ @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
+
+ # True if the method name matches one of the five elements defined
+ # in the Atom spec as potentially containing XHTML content and
+ # if :type => 'xhtml' is, in fact, specified.
+ def xhtml_block?(method, arguments)
+ %w( content rights title subtitle summary ).include?(method.to_s) &&
+ arguments.last.respond_to?(:[]) &&
+ arguments.last[:type].to_s == 'xhtml'
+ end
+ end
+
+ class AtomFeedBuilder < AtomBuilder
def initialize(xml, view, feed_options = {})
@xml, @view, @feed_options = xml, view, feed_options
end
@@ -141,15 +183,11 @@ def entry(record, options = {})
@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
@@ -128,6 +128,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 <i>COOL!</i>", Time.utc(2007, 12, 12, 15), Time.utc(2007, 12, 12, 15)),
@@ -245,6 +267,14 @@ def test_feed_xml_processing_instructions_duplicate_targets
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|

0 comments on commit 1abdc87

Please sign in to comment.