Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support <changefreq> and <priority> lambdas

  • Loading branch information...
commit a4226a7b8ea478aff3845023565cda96de4f9fdf 1 parent 45c8680
@mislav authored
View
7 lib/big_sitemap.rb
@@ -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
View
7 lib/big_sitemap/builder.rb
@@ -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
15 spec/builder_spec.rb
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.