Permalink
Browse files

support <changefreq> and <priority> lambdas

  • Loading branch information...
mislav committed Apr 5, 2009
1 parent 45c8680 commit a4226a7b8ea478aff3845023565cda96de4f9fdf
Showing with 23 additions and 6 deletions.
  1. +6 −1 lib/big_sitemap.rb
  2. +4 −3 lib/big_sitemap/builder.rb
  3. +13 −2 spec/builder_spec.rb
View
@@ -52,12 +52,17 @@ def generate
with_sitemap(model.name.tableize) do |sitemap|
find_options = options.dup
changefreq = find_options.delete(:change_frequency) || 'weekly'
+ priority = find_options.delete(:priority)
+
find_options[:batch_size] ||= @options[:batch_size]
timestamp_column = model.column_names.find { |col| TIMESTAMP_COLUMNS.include? col }
model.find_each(find_options) do |record|
last_updated = timestamp_column && record.read_attribute(timestamp_column)
- sitemap.add_url!(polymorphic_url(record), last_updated, changefreq)
+ freq = changefreq.is_a?(Proc) ? changefreq.call(record) : changefreq
+ pri = priority.is_a?(Proc) ? priority.call(record) : priority
+
+ sitemap.add_url!(polymorphic_url(record), last_updated, freq, pri)
end
end
end
@@ -23,13 +23,14 @@ def initialize(options)
_init_document
end
- def add_url!(url, time = nil, frequency = nil)
+ def add_url!(url, time = nil, frequency = nil, priority = nil)
_rotate if @max_urls == @urls
tag!(@index ? 'sitemap' : 'url') do
loc url
- lastmod time.to_s(:sitemap) unless time.nil?
- changefreq frequency unless frequency.nil?
+ lastmod(time.to_s(:sitemap)) unless time.nil?
+ changefreq(frequency) unless frequency.nil?
+ priority(priority) unless priority.nil?
end
@urls += 1
end
View
@@ -10,7 +10,7 @@
end
it "should add location" do
- @xml.add_url!("http://example.com/mooslav", @time, "all the f-in time")
+ @xml.add_url!("http://example.com/mooslav", @time)
@xml.close!
result.should == strip(<<-XML)
@@ -19,11 +19,22 @@
<url>
<loc>http://example.com/mooslav</loc>
<lastmod>#{@time_string}</lastmod>
- <changefreq>all the f-in time</changefreq>
</url>
</urlset>
XML
end
+
+ it "should support frequency" do
+ @xml.add_url!("http://example.com/mooslav", nil, "all the f-in time")
+ @xml.close!
+ result.should include("<changefreq>all the f-in time</changefreq>")
+ end
+
+ it "should support priority" do
+ @xml.add_url!("http://example.com/mooslav", nil, nil, 0.6)
+ @xml.close!
+ result.should include("<priority>0.6</priority>")
+ end
it "should rotate files when it reaches maximum number of URLs" do
@xml.add_url!("http://example.com/loc1", @time)

0 comments on commit a4226a7

Please sign in to comment.