diff options
Diffstat (limited to 'actionpack/lib/action_view/helpers/atom_feed_helper.rb')
-rw-r--r-- | actionpack/lib/action_view/helpers/atom_feed_helper.rb | 64 |
1 files changed, 56 insertions, 8 deletions
diff --git a/actionpack/lib/action_view/helpers/atom_feed_helper.rb b/actionpack/lib/action_view/helpers/atom_feed_helper.rb index e65d5d1f60..ccb7df212a 100644 --- a/actionpack/lib/action_view/helpers/atom_feed_helper.rb +++ b/actionpack/lib/action_view/helpers/atom_feed_helper.rb @@ -51,6 +51,7 @@ module ActionView # * <tt>:schema_date</tt>: 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. If not specified, # 2005 is used (as an "I don't care" value). + # * <tt>:instruct</tt>: Hash of XML processing instructions in the form {target => {attribute => value, }} or {target => [{attribute => value, }, ]} # # Other namespaces can be added to the root element: # @@ -74,8 +75,20 @@ module ActionView # 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) @@ -85,6 +98,15 @@ module ActionView xml = options[:xml] || eval("xml", block.binding) xml.instruct! + if options[:instruct] + options[:instruct].each do |target,attrs| + if attrs.respond_to?(:keys) + xml.instruct!(target, attrs) + elsif attrs.respond_to?(:each) + attrs.each { |attr_group| xml.instruct!(target, attr_group) } + end + end + end 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/)} @@ -98,8 +120,38 @@ module ActionView end end + class AtomBuilder + def initialize(xml) + @xml = xml + end + + 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 + class AtomFeedBuilder < AtomBuilder def initialize(xml, view, feed_options = {}) @xml, @view, @feed_options = xml, view, feed_options end @@ -131,15 +183,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 |