Skip to content

Commit 42cbd71

Browse files
mmblifo
authored andcommitted
Add support for xml processing instructions in atom_feed_helper [#926 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
1 parent b47c76b commit 42cbd71

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

actionpack/lib/action_view/helpers/atom_feed_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module AtomFeedHelper
5151
# * <tt>:schema_date</tt>: The date at which the tag scheme for the feed was first used. A good default is the year you
5252
# created the feed. See http://feedvalidator.org/docs/error/InvalidTAG.html for more information. If not specified,
5353
# 2005 is used (as an "I don't care" value).
54+
# * <tt>:instruct</tt>: Hash of XML processing instructions in the form {target => {attribute => value, }} or {target => [{attribute => value, }, ]}
5455
#
5556
# Other namespaces can be added to the root element:
5657
#
@@ -85,6 +86,15 @@ def atom_feed(options = {}, &block)
8586

8687
xml = options[:xml] || eval("xml", block.binding)
8788
xml.instruct!
89+
if options[:instruct]
90+
options[:instruct].each do |target,attrs|
91+
if attrs.respond_to?(:keys)
92+
xml.instruct!(target, attrs)
93+
elsif attrs.respond_to?(:each)
94+
attrs.each { |attr_group| xml.instruct!(target, attr_group) }
95+
end
96+
end
97+
end
8898

8999
feed_opts = {"xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom'}
90100
feed_opts.merge!(options).reject!{|k,v| !k.to_s.match(/^xml/)}

actionpack/test/template/atom_feed_helper_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,42 @@ class ScrollsController < ActionController::Base
8585
entry.content(scroll.body, :type => 'html')
8686
entry.tag!('app:edited', Time.now)
8787
88+
entry.author do |author|
89+
author.name("DHH")
90+
end
91+
end
92+
end
93+
end
94+
EOT
95+
FEEDS["feed_with_xml_processing_instructions"] = <<-EOT
96+
atom_feed(:schema_date => '2008',
97+
:instruct => {'xml-stylesheet' => { :href=> 't.css', :type => 'text/css' }}) do |feed|
98+
feed.title("My great blog!")
99+
feed.updated((@scrolls.first.created_at))
100+
101+
for scroll in @scrolls
102+
feed.entry(scroll) do |entry|
103+
entry.title(scroll.title)
104+
entry.content(scroll.body, :type => 'html')
105+
106+
entry.author do |author|
107+
author.name("DHH")
108+
end
109+
end
110+
end
111+
end
112+
EOT
113+
FEEDS["feed_with_xml_processing_instructions_duplicate_targets"] = <<-EOT
114+
atom_feed(:schema_date => '2008',
115+
:instruct => {'target1' => [{ :a => '1', :b => '2' }, { :c => '3', :d => '4' }]}) do |feed|
116+
feed.title("My great blog!")
117+
feed.updated((@scrolls.first.created_at))
118+
119+
for scroll in @scrolls
120+
feed.entry(scroll) do |entry|
121+
entry.title(scroll.title)
122+
entry.content(scroll.body, :type => 'html')
123+
88124
entry.author do |author|
89125
author.name("DHH")
90126
end
@@ -194,6 +230,21 @@ def test_feed_should_allow_overriding_ids
194230
end
195231
end
196232

233+
def test_feed_xml_processing_instructions
234+
with_restful_routing(:scrolls) do
235+
get :index, :id => 'feed_with_xml_processing_instructions'
236+
assert_match %r{<\?xml-stylesheet type="text/css" href="t.css"\?>}, @response.body
237+
end
238+
end
239+
240+
def test_feed_xml_processing_instructions_duplicate_targets
241+
with_restful_routing(:scrolls) do
242+
get :index, :id => 'feed_with_xml_processing_instructions_duplicate_targets'
243+
assert_match %r{<\?target1 (a="1" b="2"|b="2" a="1")\?>}, @response.body
244+
assert_match %r{<\?target1 (c="3" d="4"|d="4" c="3")\?>}, @response.body
245+
end
246+
end
247+
197248
private
198249
def with_restful_routing(resources)
199250
with_routing do |set|

0 commit comments

Comments
 (0)