URI rewriting to determine a controller based on an action name

This commit is contained in:
Alexey Verkhovsky 2005-01-22 01:57:05 +00:00
parent 12a34823a8
commit 9d90901cd0
3 changed files with 41 additions and 1 deletions

View file

@ -1,4 +1,5 @@
require 'application'
class FileController < ApplicationController
layout 'default', :except => [:rss_feed, :rss_with_headlines, :tex, :export_tex, :export_html]

View file

@ -6,7 +6,7 @@
# In Instiki URLs are mapped to the ActionPack actions, possibly performed on a particular
# web (sub-wiki) and page within that web.
#
# 1. Controller is always 'wiki'
# 1. Controller is determined by action name (default is 'wiki')
# 2. '/name1/' maps to action 'name1', unspecified web
# Example: http://localhost/new_system/
# 3. Special case of above, URI '/wiki/' maps to action 'index', because Rails sets this address
@ -24,6 +24,16 @@ require 'dispatcher'
class DispatchServlet
def self.parse_uri(path)
result = parse_path(path)
if result
result[:controller] = ActionMapper.map_to_controller(result[:action])
result
else
false
end
end
def self.parse_path(path)
ApplicationController.logger.debug "Parsing URI '#{path}'"
component = '([-_a-zA-Z0-9]+)'
page_name = '(.*)'
@ -48,6 +58,19 @@ class DispatchServlet
line
end
end
class ActionMapper
@@action_to_controller_map = {
'file' => 'file',
'pic' => 'file'
}
def self.map_to_controller(action)
@@action_to_controller_map[action] || 'wiki'
end
end
end

View file

@ -72,5 +72,21 @@ class UrlRewritingHackTest < Test::Unit::TestCase
assert_equal 'http://test.host/',
ur.rewrite(:controller => 'wiki')
end
def test_controller_mapping
request = ActionController::TestRequest.new
ur = ActionController::UrlRewriter.new(request, 'wiki', 'show')
assert_equal 'http://test.host/file',
ur.rewrite(:controller => 'file', :action => 'file')
assert_equal 'http://test.host/pic/abc.jpg',
ur.rewrite(:controller => 'file', :action => 'pic', :id => 'abc.jpg')
assert_equal 'http://test.host/web/pic/abc.jpg',
ur.rewrite(:web => 'web', :controller => 'file', :action => 'pic', :id => 'abc.jpg')
# default option is wiki
assert_equal 'http://test.host/unknown_action',
ur.rewrite(:controller => 'wiki', :action => 'unknown_action')
end
end