From 40ebdcd289392096d1f303d6f9e7debdaaeb65fc Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Sun, 9 Aug 2009 20:25:01 +0800 Subject: [PATCH] add priority support --- lib/sitemap.rb | 23 ++++++++++++++--------- spec/sitemap_spec.rb | 23 +++++++++++++---------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/sitemap.rb b/lib/sitemap.rb index cffeda6..979d62c 100644 --- a/lib/sitemap.rb +++ b/lib/sitemap.rb @@ -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 @@ -30,9 +31,12 @@ 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 @@ -40,13 +44,14 @@ def generate(sitemap_result_file) 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] @@ -57,19 +62,19 @@ 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 @@ -77,7 +82,7 @@ def parse_path(path, options, prefix, parent) 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 diff --git a/spec/sitemap_spec.rb b/spec/sitemap_spec.rb index 543b941..0c879dc 100644 --- a/spec/sitemap_spec.rb +++ b/spec/sitemap_spec.rb @@ -49,7 +49,8 @@ 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 @@ -57,7 +58,7 @@ def to_param 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 @@ -65,7 +66,7 @@ def to_param 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 @@ -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 @@ -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 @@ -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