1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Merge pull request #180 from stjhimy/redirect_to

Add redirect_to method allowing redirects within a namespace
This commit is contained in:
Zachary Scott 2016-05-04 17:22:22 +09:00
commit 16bb6a78df
2 changed files with 29 additions and 0 deletions

View file

@ -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

View file

@ -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' } }