Permalink
Browse files

add array substitution

  • Loading branch information...
1 parent 69c0588 commit 8c5cc99eb50cda3dca914ed3ee5d6547d23e5613 @flyerhzm committed Aug 13, 2009
Showing with 29 additions and 7 deletions.
  1. +3 −0 README
  2. +17 −7 lib/sitemap.rb
  3. +9 −0 spec/sitemap_spec.rb
View
@@ -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'
View
@@ -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}"
@@ -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

0 comments on commit 8c5cc99

Please sign in to comment.