Skip to content

Commit

Permalink
add array substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Aug 13, 2009
1 parent 69c0588 commit 8c5cc99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README
Expand Up @@ -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'

Expand Down
24 changes: 17 additions & 7 deletions lib/sitemap.rb
Expand Up @@ -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}"
Expand Down
9 changes: 9 additions & 0 deletions spec/sitemap_spec.rb
Expand Up @@ -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
Expand All @@ -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.