Skip to content

Commit

Permalink
Builder#add_url! method now accepts options hash instead of many args
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 6, 2009
1 parent 7d9aaf2 commit e966e55
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
11 changes: 6 additions & 5 deletions lib/big_sitemap.rb
Expand Up @@ -58,11 +58,12 @@ def generate
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)
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)
sitemap.add_url!(
polymorphic_url(record),
:time => timestamp_column && record.read_attribute(timestamp_column),
:frequency => changefreq.is_a?(Proc) ? changefreq.call(record) : changefreq,
:priority => priority.is_a?(Proc) ? priority.call(record) : priority
)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/big_sitemap/builder.rb
Expand Up @@ -26,14 +26,14 @@ def index?
@type == 'index'
end

def add_url!(url, time = nil, frequency = nil, priority = nil)
def add_url!(url, options = {})
_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?
priority(priority) unless priority.nil?
loc(url)
lastmod(options[:time].to_s(:sitemap)) if options[:time]
changefreq(options[:frequency]) if options[:frequency]
priority(options[:priority]) if options[:priority]
end
@urls += 1
end
Expand Down
14 changes: 7 additions & 7 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)
@xml.add_url!("http://example.com/mooslav", :time => @time)
@xml.close!

result.should == strip(<<-XML)
Expand All @@ -25,28 +25,28 @@
end

it "should support frequency" do
@xml.add_url!("http://example.com/mooslav", nil, "all the f-in time")
@xml.add_url!("http://example.com/mooslav", :frequency => "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.add_url!("http://example.com/mooslav", :priority => 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)
@xml.add_url!("http://example.com/loc2", @time)
@xml.add_url!("http://example.com/loc3", @time)
@xml.add_url!("http://example.com/loc1")
@xml.add_url!("http://example.com/loc2")
@xml.add_url!("http://example.com/loc3")
@xml.close!
result.scan('<urlset').size.should == 2
end
end

it "should have sitemap index mode" do
@xml = described_class.new(:indent => 2, :index => true)
@xml = described_class.new(:indent => 2, :type => 'index')
@xml.add_url!("/sitemap1.xml")
@xml.close!

Expand Down

0 comments on commit e966e55

Please sign in to comment.