Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Add redirect_to method allowing redirects within a namespace #180

Merged
merged 1 commit into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/sinatra/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -137,6 +149,10 @@ def settings
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
Expand Down Expand Up @@ -226,6 +242,11 @@ def layout(name=:layout, &block)
template name, &block
end

def pattern
@pattern
end


private

def app
Expand Down
8 changes: 8 additions & 0 deletions spec/namespace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def namespace(*args, &block)
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' } }
Expand Down