mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Add reredict_to method allowing redirects within a namespace
This commit is contained in:
parent
ac9eb853d2
commit
36c20fae65
2 changed files with 29 additions and 0 deletions
|
@ -89,6 +89,18 @@ module Sinatra
|
|||
# # More admin routes...
|
||||
# end
|
||||
#
|
||||
# Redirecting within the namespace can be done using redirect_to:
|
||||
#
|
||||
# namespace '/admin' do
|
||||
# get '/foo' do
|
||||
# redirect_to '/bar' # Redirects to /admin/bar
|
||||
# end
|
||||
#
|
||||
# get '/foo' do
|
||||
# redirect '/bar' # Redirects to /bar
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# === Classic Application Setup
|
||||
#
|
||||
# To be able to use namespaces in a classic application all you need to do is
|
||||
|
@ -137,6 +149,10 @@ module Sinatra
|
|||
def template_cache
|
||||
super.fetch(:nested, @namespace) { Tilt::Cache.new }
|
||||
end
|
||||
|
||||
def redirect_to(uri, *args)
|
||||
redirect("#{@namespace.pattern}#{uri}", *args)
|
||||
end
|
||||
end
|
||||
|
||||
module SharedMethods
|
||||
|
@ -226,6 +242,11 @@ module Sinatra
|
|||
template name, &block
|
||||
end
|
||||
|
||||
def pattern
|
||||
@pattern
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def app
|
||||
|
|
|
@ -25,6 +25,14 @@ describe Sinatra::Namespace do
|
|||
send(verb, '/foo/baz').should_not be_ok
|
||||
end
|
||||
|
||||
describe 'redirect_to' do
|
||||
it 'redirect within namespace' do
|
||||
namespace('/foo') { send(verb, '/bar') { redirect_to '/foo_bar' }}
|
||||
send(verb, '/foo/bar').should be_redirect
|
||||
send(verb, '/foo/bar').location.should include("/foo/foo_bar")
|
||||
end
|
||||
end
|
||||
|
||||
context 'when namespace is a string' do
|
||||
it 'accepts routes with no path' do
|
||||
namespace('/foo') { send(verb) { 'bar' } }
|
||||
|
|
Loading…
Reference in a new issue