Skip to content

Commit

Permalink
add priority support
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Aug 9, 2009
1 parent 7e3a204 commit 40ebdcd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
23 changes: 14 additions & 9 deletions lib/sitemap.rb
Expand Up @@ -3,8 +3,9 @@

module Sitemap
class Routes
cattr_writer :host
cattr_reader :paths
cattr_writer :host, :priority
cattr_reader :results
@@priority = 1.0

class << self
include ActiveSupport::Inflector
Expand All @@ -30,23 +31,27 @@ def generate(sitemap_result_file)
xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
xml.instruct!
xml.urlset(:xmlns => "http://www.sitemaps.org/schema/sitemap/0.9") do
@@paths.each do |path|
now = Time.now
@@results.each do |result|
xml.url do
xml.loc(@@host + path)
xml.loc(@@host + result[:location])
xml.priority result[:priority]
xml.lastmod now
end
end
end
end
end

def parse
@@paths = []
@@results = []
@@routes.each do |route|
parse_path(route[:path], route[:options], '', nil)
end
end

def parse_path(path, options, prefix, parent)
priority = options[:priority] || @@priority
begin
if options[:substitution]
substitution = options[:substitution]
Expand All @@ -57,27 +62,27 @@ def parse_path(path, options, prefix, parent)
substitution.each do |key, value|
path_dup.gsub!(':' + key.to_s, obj.send(value).to_s)
end
@@paths << path_dup
@@results << {:location => path_dup, :priority => priority}
end
else
items = path.split('/')
if items[2].nil?
@@paths << prefix + path
@@results << {:location => prefix + path, :priority => priority}
elsif items[2] =~ /^:.*id$/
objects = parent.nil? ? Object.const_get(items[1].singularize.camelize).all : parent.send(items[1])
objects.each do |obj|
if items.size > 3
parse_path('/' + items[3..-1].join('/'), options, "#{prefix}/#{items[1]}/#{obj.to_param}", obj)
else
@@paths << "#{prefix}/#{items[1]}/#{obj.to_param}"
@@results << {:location => "#{prefix}/#{items[1]}/#{obj.to_param}", :priority => priority}
end
end
return nil
else
if items.size > 2
parse_path('/' + items[2..-1].join('/'), options, "#{prefix}/#{items[1]}", nil)
else
@@paths << prefix + path
@@results << {:location => prefix + path, :priority => priority}
end
end
end
Expand Down
23 changes: 13 additions & 10 deletions spec/sitemap_spec.rb
Expand Up @@ -49,23 +49,24 @@ def to_param
map.resources :posts
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['/posts', '/posts/1', '/posts/2']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/posts', '/posts/1', '/posts/2']
Sitemap::Routes.results.collect {|result| result[:priority]}.should == [1.0, 1.0, 1.0]
end

it "should add collection" do
Sitemap::Routes.draw do |map|
map.resources :posts, :collection => {:all => :get}
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['/posts/all', '/posts', '/posts/1', '/posts/2']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/posts/all', '/posts', '/posts/1', '/posts/2']
end

it "should add member" do
Sitemap::Routes.draw do |map|
map.resources :posts, :member => {:display => :get}
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['/posts', '/posts/1/display', '/posts/2/display', '/posts/1', '/posts/2']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/posts', '/posts/1/display', '/posts/2/display', '/posts/1', '/posts/2']
end
end

Expand All @@ -74,7 +75,7 @@ def to_param
map.root :controller => 'posts', :action => 'index'
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['']
end

it "should parse namespace" do
Expand All @@ -84,23 +85,25 @@ def to_param
end
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['/admin/posts', '/admin/posts/1', '/admin/posts/2']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/admin/posts', '/admin/posts/1', '/admin/posts/2']
end

it "should parse connect" do
Sitemap::Routes.draw do |map|
map.connect 'posts/:year/:month/:day', :controller => 'posts', :action => 'find_by_date', :substitution => {:model => 'Post', :year => 'year', :month => 'month', :day => 'day'}
map.connect 'posts/:year/:month/:day', :controller => 'posts', :action => 'find_by_date', :substitution => {:model => 'Post', :year => 'year', :month => 'month', :day => 'day'}, :priority => 0.8
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['/posts/2009/8/9', '/posts/2009/8/10']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/posts/2009/8/9', '/posts/2009/8/10']
Sitemap::Routes.results.collect {|result| result[:priority]}.should == [0.8, 0.8]
end

it "should parse named_route" do
Sitemap::Routes.draw do |map|
map.sitemap '/sitemap', :controller => 'sitemaps', :action => 'index'
map.sitemap '/sitemap', :controller => 'sitemaps', :action => 'index', :priority => 0.5
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['/sitemap']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/sitemap']
Sitemap::Routes.results.collect {|result| result[:priority]}.should == [0.5]
end

it "should parse nested resources" do
Expand All @@ -114,6 +117,6 @@ def to_param
end
end
Sitemap::Routes.parse
Sitemap::Routes.paths.should == ['/categories', '/categories/1', '/categories/1/posts', '/categories/1/posts/1', '/categories/1/posts/2']
Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/categories', '/categories/1', '/categories/1/posts', '/categories/1/posts/1', '/categories/1/posts/2']
end
end

0 comments on commit 40ebdcd

Please sign in to comment.