public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Allow overriding id for feed and entry with atom_feed_builder. [#485 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Darragh Curran (author)
Wed Jun 25 03:15:26 -0700 2008
lifo (committer)
Thu Aug 21 05:37:26 -0700 2008
commit  7e4ea5f4a2fd25e06820689688e3db5a4851f8e0
tree    2bb3151f710e86545ff09d0ad8d69b15a9c11a28
parent  2415652660242d6b0da97119c562ecff82928575
...
17
18
19
20
 
21
22
23
...
29
30
31
32
 
33
34
35
36
37
 
38
39
40
...
47
48
49
50
51
 
 
 
52
53
54
...
81
82
83
84
 
85
86
87
...
89
90
91
92
 
93
94
95
 
96
97
98
...
102
103
104
105
 
106
107
108
...
115
116
117
 
118
119
120
 
 
121
122
123
...
17
18
19
 
20
21
22
23
...
29
30
31
 
32
33
34
35
36
 
37
38
39
40
...
47
48
49
 
 
50
51
52
53
54
55
...
82
83
84
 
85
86
87
88
...
90
91
92
 
93
94
95
 
96
97
98
99
...
103
104
105
 
106
107
108
109
...
116
117
118
119
120
 
 
121
122
123
124
125
0
@@ -17,7 +17,7 @@ module ActionView
0
       #       # GET /posts.atom
0
       #       def index
0
       #         @posts = Post.find(:all)
0
-      #         
0
+      #
0
       #         respond_to do |format|
0
       #           format.html
0
       #           format.atom
0
@@ -29,12 +29,12 @@ module ActionView
0
       #     atom_feed do |feed|
0
       #       feed.title("My great blog!")
0
       #       feed.updated((@posts.first.created_at))
0
-      #     
0
+      #
0
       #       for post in @posts
0
       #         feed.entry(post) do |entry|
0
       #           entry.title(post.title)
0
       #           entry.content(post.body, :type => 'html')
0
-      #     
0
+      #
0
       #           entry.author do |author|
0
       #             author.name("DHH")
0
       #           end
0
@@ -47,8 +47,9 @@ module ActionView
0
       # * <tt>:language</tt>: Defaults to "en-US".
0
       # * <tt>:root_url</tt>: The HTML alternative that this feed is doubling for. Defaults to / on the current host.
0
       # * <tt>:url</tt>: The URL for this feed. Defaults to the current URL.
0
-      # * <tt>:schema_date</tt>: The date at which the tag scheme for the feed was first used. A good default is the year you 
0
-      #   created the feed. See http://feedvalidator.org/docs/error/InvalidTAG.html for more information. If not specified, 
0
+      # * <tt>:id</tt>: The id for this feed. Defaults to "tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}"
0
+      # * <tt>:schema_date</tt>: The date at which the tag scheme for the feed was first used. A good default is the year you
0
+      #   created the feed. See http://feedvalidator.org/docs/error/InvalidTAG.html for more information. If not specified,
0
       #   2005 is used (as an "I don't care" value).
0
       #
0
       # Other namespaces can be added to the root element:
0
@@ -81,7 +82,7 @@ module ActionView
0
         else
0
           options[:schema_date] = "2005" # The Atom spec copyright date
0
         end
0
-        
0
+
0
         xml = options[:xml] || eval("xml", block.binding)
0
         xml.instruct!
0
 
0
@@ -89,10 +90,10 @@ module ActionView
0
         feed_opts.merge!(options).reject!{|k,v| !k.to_s.match(/^xml/)}
0
 
0
         xml.feed(feed_opts) do
0
-          xml.id("tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}")      
0
+          xml.id(options[:id] || "tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}")
0
           xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port))
0
           xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url)
0
-          
0
+
0
           yield AtomFeedBuilder.new(xml, self, options)
0
         end
0
       end
0
@@ -102,7 +103,7 @@ module ActionView
0
         def initialize(xml, view, feed_options = {})
0
           @xml, @view, @feed_options = xml, view, feed_options
0
         end
0
-        
0
+
0
         # Accepts a Date or Time object and inserts it in the proper format. If nil is passed, current time in UTC is used.
0
         def updated(date_or_time = nil)
0
           @xml.updated((date_or_time || Time.now.utc).xmlschema)
0
@@ -115,9 +116,10 @@ module ActionView
0
         # * <tt>:published</tt>: Time first published. Defaults to the created_at attribute on the record if one such exists.
0
         # * <tt>:updated</tt>: Time of update. Defaults to the updated_at attribute on the record if one such exists.
0
         # * <tt>:url</tt>: The URL for this entry. Defaults to the polymorphic_url for the record.
0
+        # * <tt>:id</tt>: The ID for this entry. Defaults to "tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}"
0
         def entry(record, options = {})
0
-          @xml.entry do 
0
-            @xml.id("tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}")
0
+          @xml.entry do
0
+            @xml.id(options[:id] || "tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}")
0
 
0
             if options[:published] || (record.respond_to?(:created_at) && record.created_at)
0
               @xml.published((options[:published] || record.created_at).xmlschema)
...
74
75
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
78
79
80
81
82
 
83
84
85
...
98
99
100
101
 
102
103
104
105
106
107
108
 
109
110
111
112
113
114
115
 
116
117
118
...
167
168
169
170
 
 
 
 
 
 
 
 
 
 
171
172
173
...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 
100
101
102
103
...
116
117
118
 
119
120
121
122
123
124
125
 
126
127
128
129
130
131
132
 
133
134
135
136
...
185
186
187
 
188
189
190
191
192
193
194
195
196
197
198
199
200
0
@@ -74,12 +74,30 @@ class ScrollsController < ActionController::Base
0
           end
0
         end
0
     EOT
0
+    FEEDS["feed_with_overridden_ids"] = <<-EOT
0
+        atom_feed({:id => 'tag:test.rubyonrails.org,2008:test/'}) do |feed|
0
+          feed.title("My great blog!")
0
+          feed.updated((@scrolls.first.created_at))
0
+
0
+          for scroll in @scrolls
0
+            feed.entry(scroll, :id => "tag:test.rubyonrails.org,2008:"+scroll.id.to_s) do |entry|
0
+              entry.title(scroll.title)
0
+              entry.content(scroll.body, :type => 'html')
0
+              entry.tag!('app:edited', Time.now)
0
+
0
+              entry.author do |author|
0
+                author.name("DHH")
0
+              end
0
+            end
0
+          end
0
+        end
0
+    EOT
0
   def index
0
     @scrolls = [
0
       Scroll.new(1, "1", "Hello One", "Something <i>COOL!</i>", Time.utc(2007, 12, 12, 15), Time.utc(2007, 12, 12, 15)),
0
       Scroll.new(2, "2", "Hello Two", "Something Boring", Time.utc(2007, 12, 12, 15)),
0
     ]
0
-    
0
+
0
     render :inline => FEEDS[params[:id]], :type => :builder
0
   end
0
 
0
@@ -98,21 +116,21 @@ class AtomFeedTest < Test::Unit::TestCase
0
 
0
     @request.host = "www.nextangle.com"
0
   end
0
-  
0
+
0
   def test_feed_should_use_default_language_if_none_is_given
0
     with_restful_routing(:scrolls) do
0
       get :index, :id => "defaults"
0
       assert_match %r{xml:lang="en-US"}, @response.body
0
     end
0
   end
0
-  
0
+
0
   def test_feed_should_include_two_entries
0
     with_restful_routing(:scrolls) do
0
       get :index, :id => "defaults"
0
       assert_select "entry", 2
0
     end
0
   end
0
-  
0
+
0
   def test_entry_should_only_use_published_if_created_at_is_present
0
     with_restful_routing(:scrolls) do
0
       get :index, :id => "defaults"
0
@@ -167,7 +185,16 @@ class AtomFeedTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
-  private
0
+  def test_feed_should_allow_overriding_ids
0
+    with_restful_routing(:scrolls) do
0
+      get :index, :id => "feed_with_overridden_ids"
0
+      assert_select "id", :text => "tag:test.rubyonrails.org,2008:test/"
0
+      assert_select "entry id", :text => "tag:test.rubyonrails.org,2008:1"
0
+      assert_select "entry id", :text => "tag:test.rubyonrails.org,2008:2"
0
+    end
0
+  end
0
+
0
+private
0
     def with_restful_routing(resources)
0
       with_routing do |set|
0
         set.draw do |map|

Comments

darragh Thu Aug 21 05:49:25 -0700 2008

thanks!