GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Add path_prefix support.

If Merb::Config[:path_prefix] is set when the Rack application instance
is created, then that value will be stripped off of the urls of any 
requests
coming in to the app and prepended to urls generated with the url() 
method.

Signed-off-by: Ezra Zygmuntowicz <ez@engineyard.com>
dudleyf (author)
Wed Feb 20 11:58:19 -0800 2008
ezmobius (committer)
Wed Feb 20 12:17:27 -0800 2008
commit  423ae48af6b7f6b83205fca724b7af6394e40741
tree    96c8749f72e734822409b7b9a9f1f7dbf3cc2821
parent  19883013e9184d27a8ee137e713c28b64e0d7f7a
...
223
224
225
226
 
227
228
229
230
231
 
 
 
232
233
234
...
223
224
225
 
226
227
228
229
230
 
231
232
233
234
235
236
0
@@ -223,12 +223,14 @@ module Merb
0
     # If a hash is used as the first argument, a default route will be
0
     # generated based on it and rparams.
0
     def url(name, rparams={})
0
- Merb::Router.generate(name, rparams,
0
+ uri = Merb::Router.generate(name, rparams,
0
         { :controller => controller_name,
0
           :action => action_name,
0
           :format => params[:format]
0
         }
0
- )
0
+ )
0
+ uri = Merb::Config[:path_prefix] + uri if Merb::Config[:path_prefix]
0
+ uri
0
     end
0
     
0
     # Escapes the string representation of +obj+ and escapes
...
5
6
7
8
 
 
 
 
9
10
11
12
 
 
 
13
14
15
...
31
32
33
34
 
35
36
37
...
42
43
44
 
 
 
 
 
 
 
 
 
45
46
47
48
...
5
6
7
 
8
9
10
11
12
13
 
 
14
15
16
17
18
19
...
35
36
37
 
38
39
40
41
...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
0
@@ -5,11 +5,15 @@ module Merb
0
     class Application
0
 
0
       def initialize(options={})
0
- @static_server = ::Rack::File.new(::File.join(Merb.root, "public"))
0
+ @static_server = ::Rack::File.new Merb.dir_for(:public)
0
+ if prefix = ::Merb::Config[:path_prefix]
0
+ @path_prefix = /^#{Regexp.escape(prefix)}/
0
+ end
0
       end
0
 
0
- def call(env)
0
- path = env['PATH_INFO'].chomp('/')
0
+ def call(env)
0
+ strip_path_prefix(env) if @path_prefix # Strip out the path_prefix if one was set
0
+ path = env['PATH_INFO'].chomp('/')
0
         cached_path = (path.empty? ? 'index' : path) + '.html'
0
         Merb.logger.info "Request: #{path}"
0
         if file_exist?(path) # Serve the file if it's there
0
@@ -31,7 +35,7 @@ module Merb
0
           [controller.status, controller.headers, controller.body]
0
         end
0
       end
0
-
0
+
0
       # TODO refactor this in File#can_serve?(path) ??
0
       def file_exist?(path)
0
         full_path = ::File.join(@static_server.root, ::Rack::Utils.unescape(path))
0
@@ -42,6 +46,15 @@ module Merb
0
         @static_server.call(env)
0
       end
0
       
0
+ def strip_path_prefix(env)
0
+ ['PATH_INFO', 'REQUEST_URI'].each do |path_key|
0
+ if env[path_key] =~ @path_prefix
0
+ env[path_key].sub!(@path_prefix, '')
0
+ env[path_key] = '/' if env[path_key].empty?
0
+ end
0
+ end
0
+ end
0
+
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
15
16
17
18
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
...
15
16
17
 
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
0
@@ -15,4 +15,30 @@ describe Merb::Rack::Application do
0
     res.should be_kind_of Rack::MockResponse
0
   end
0
   
0
-end
0
\ No newline at end of file
0
+end
0
+
0
+describe Merb::Rack::Application, "with :path_prefix set" do
0
+
0
+ before do
0
+ Merb::Config[:path_prefix] = "/quux"
0
+ @app = Merb::Rack::Application.new
0
+ @nullobj = mock('controller', :null_object => true)
0
+ end
0
+
0
+ it "should strip the path_prefix from a request's REQUEST_URI and PATH_INFO" do
0
+ Merb::Dispatcher.should_receive(:handle).with(
0
+ {'REQUEST_URI' => "/foo/bar", 'PATH_INFO' => "/foo/bar"}
0
+ ).and_return @nullobj
0
+
0
+ @app.call('REQUEST_URI' => "/quux/foo/bar", 'PATH_INFO' => "/quux/foo/bar")
0
+ end
0
+
0
+ it "should not leave REQUEST_URI or PATH_INFO empty" do
0
+ Merb::Dispatcher.should_receive(:handle).with(
0
+ {'REQUEST_URI' => "/", 'PATH_INFO' => "/"}
0
+ ).and_return @nullobj
0
+
0
+ @app.call('REQUEST_URI' => "/quux", 'PATH_INFO' => "/quux")
0
+ end
0
+
0
+end
0
\ No newline at end of file
...
81
82
83
 
 
 
 
 
 
 
84
85
...
81
82
83
84
85
86
87
88
89
90
91
92
0
@@ -81,4 +81,11 @@ describe Merb::Controller, " url" do
0
   it "should match the delete_red route" do
0
     @controller.url(:delete_red).should == "/red/delete"
0
   end
0
+
0
+ it "should add a path_prefix to the url if :path_prefix is set" do
0
+ Merb::Config[:path_prefix] = "/jungle"
0
+ @controller.url(:monkeys).should == "/jungle/monkeys"
0
+ Merb::Config[:path_prefix] = nil
0
+ end
0
+
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.