1
0
Fork 0
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:
stjhimy 2015-12-04 08:45:32 -02:00
parent ac9eb853d2
commit 36c20fae65
2 changed files with 29 additions and 0 deletions

View file

@ -89,6 +89,18 @@ module Sinatra
# # More admin routes... # # More admin routes...
# end # 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 # === Classic Application Setup
# #
# To be able to use namespaces in a classic application all you need to do is # 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 def template_cache
super.fetch(:nested, @namespace) { Tilt::Cache.new } super.fetch(:nested, @namespace) { Tilt::Cache.new }
end end
def redirect_to(uri, *args)
redirect("#{@namespace.pattern}#{uri}", *args)
end
end end
module SharedMethods module SharedMethods
@ -226,6 +242,11 @@ module Sinatra
template name, &block template name, &block
end end
def pattern
@pattern
end
private private
def app def app

View file

@ -25,6 +25,14 @@ describe Sinatra::Namespace do
send(verb, '/foo/baz').should_not be_ok send(verb, '/foo/baz').should_not be_ok
end 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 context 'when namespace is a string' do
it 'accepts routes with no path' do it 'accepts routes with no path' do
namespace('/foo') { send(verb) { 'bar' } } namespace('/foo') { send(verb) { 'bar' } }