From 8c5cc99eb50cda3dca914ed3ee5d6547d23e5613 Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Thu, 13 Aug 2009 21:05:05 +0800 Subject: [PATCH] add array substitution --- README | 3 +++ lib/sitemap.rb | 24 +++++++++++++++++------- spec/sitemap_spec.rb | 9 +++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/README b/README index 97bbace..03b9def 100644 --- a/README +++ b/README @@ -45,6 +45,9 @@ Example # As the route defines below, the url will be replaced with "posts/#{post.year}/#{post.month}/#{post.day}", the post the object of Post map.connect 'posts/:year/:month/:day', :controller => 'posts', :action => 'find_by_date', :substitution => {:model => 'Post', :year => 'year', :month => 'month', :day => 'day'} + # connect routes, you can also use substitution for an array substitution + map.connect 'posts/:locale', :controller => 'posts', :action => 'index', :substitution => {:locale => ['en', 'zh', 'fr']} + # named_route routes, these routes should use :substitution keyword if necessary map.sitemap '/sitemap', :controller => 'sitemaps', :action => 'index' diff --git a/lib/sitemap.rb b/lib/sitemap.rb index 2152275..641aa7e 100644 --- a/lib/sitemap.rb +++ b/lib/sitemap.rb @@ -91,18 +91,28 @@ def parse_path_without_substitution(path, options, prefix, parent) end # Parse connect path or named route path who has a substitution option, such as: - # map.connect 'posts/:year/:month/:day', :substitution => {:model => 'Post', :year => year, :month => month, :day => day} + # map.connect 'posts/:year/:month/:day', :substitution => {:model => 'Post', :year => year, :month => month, :day => day} or + # map.connect 'posts/:locale, :substitution => {:locale => ['en', 'zh', 'fr']} def parse_path_with_substitution(path, options) begin substitution = options[:substitution] model_name = substitution.delete(:model) - klazz = Object.const_get(model_name) - klazz.all.each do |obj| - path_dup = path.dup - substitution.each do |key, value| - path_dup.gsub!(':' + key.to_s, obj.send(value).to_s) + if model_name.nil? + key = substitution.keys.first + substitution.values.first.each do |value| + path_dup = path.dup + path_dup.gsub!(':' + key.to_s, value) + @@results << {:location => path_dup, :priority => options[:priority] || @@priority} + end + else + klazz = Object.const_get(model_name) + klazz.all.each do |obj| + path_dup = path.dup + substitution.each do |key, value| + path_dup.gsub!(':' + key.to_s, obj.send(value).to_s) + end + @@results << {:location => path_dup, :priority => options[:priority] || @@priority} end - @@results << {:location => path_dup, :priority => options[:priority] || @@priority} end rescue puts "can't parse prefix: #{prefix}, path: #{path}, parent: #{parent}" diff --git a/spec/sitemap_spec.rb b/spec/sitemap_spec.rb index c6aa973..59f4c57 100644 --- a/spec/sitemap_spec.rb +++ b/spec/sitemap_spec.rb @@ -116,6 +116,14 @@ def to_param Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/posts/2009/8/9', '/posts/2009/8/10'] end + it "should parse connect with substitution array" do + Sitemap::Routes.draw do |map| + map.connect 'posts/:locale', :controller => 'posts', :action => 'index', :substitution => {:locale => ['en', 'zh', 'fr']} + end + Sitemap::Routes.parse + Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/posts/en', '/posts/zh', '/posts/fr'] + end + it "should parse named_route" do Sitemap::Routes.draw do |map| map.sitemap '/sitemap', :controller => 'sitemaps', :action => 'index', :priority => 0.5 @@ -138,4 +146,5 @@ def to_param Sitemap::Routes.parse Sitemap::Routes.results.collect {|result| result[:location]}.should == ['/categories', '/categories/1', '/categories/1/posts', '/categories/1/posts/1', '/categories/1/posts/2'] end + end