Skip to content

Commit

Permalink
support <changefreq> and <priority> lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 5, 2009
1 parent 45c8680 commit a4226a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/big_sitemap.rb
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/big_sitemap/builder.rb
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions spec/builder_spec.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a4226a7

Please sign in to comment.