Skip to content

Commit

Permalink
Merge pull request #1537 from padrino/fix-1535
Browse files Browse the repository at this point in the history
rebase string urls to uri_root, closes #1535
  • Loading branch information
ujifgc committed Dec 31, 2013
2 parents 9db3754 + 356d5df commit fdfe6cf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
47 changes: 34 additions & 13 deletions padrino-core/lib/padrino-core/application/routing.rb
Expand Up @@ -582,10 +582,7 @@ def url(*args)
else
compiled_router.path(name, *(params_array << params))
end
url[0,0] = conform_uri(uri_root) if defined?(uri_root)
url[0,0] = conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI']
url = "/" if url.blank?
url
rebase_url(url)
rescue HttpRouter::InvalidRouteException
route_error = "route mapping for url(#{name.inspect}) could not be found!"
raise Padrino::Routing::UnrecognizedException.new(route_error)
Expand All @@ -600,6 +597,17 @@ def get(path, *args, &block)
route('HEAD', path, *args, &block)
end

def rebase_url(url)
if url.start_with?('/')
new_url = ''
new_url << conform_uri(uri_root) if defined?(uri_root)
new_url << conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI']
new_url << url
else
url.blank? ? '/' : url
end
end

private
# Parse params from the url method
def value_to_param(value)
Expand Down Expand Up @@ -944,17 +952,24 @@ module InstanceMethods
# url(:show, :id => 1)
# url(:show, :name => :test)
# url(:show, 1)
# url("/foo")
# url("/foo", false, false)
#
# @see Padrino::Routing::ClassMethods#url
#
def url(*args)
# Delegate to Sinatra 1.2 for simple url("/foo")
# http://www.sinatrarb.com/intro#Generating%20URLs
return super if args.first.is_a?(String) && !args[1].is_a?(Hash)

# Delegate to Padrino named route URL generation.
settings.url(*args)
if args.first.is_a?(String)
url_path = settings.rebase_url(args.shift)
if args.empty?
url_path
else
# Delegate sinatra-style urls to Sinatra. Ex: url("/foo", false, false)
# http://www.sinatrarb.com/intro#Generating%20URLs
super url_path, *args
end
else
# Delegate to Padrino named route URL generation.
settings.url(*args)
end
end
alias :url_for :url

Expand All @@ -964,9 +979,15 @@ def url(*args)
# @example
# absolute_url(:show, :id => 1) # => http://example.com/show?id=1
# absolute_url(:show, 24) # => https://example.com/admin/show/24
# absolute_url('/foo/bar') # => https://example.com/admin/foo/bar
# absolute_url('baz') # => https://example.com/admin/foo/baz
#
def absolute_url( *args )
uri url(*args), true, false
def absolute_url(*args)
url_path = args.shift
if url_path.is_a?(String) && !url_path.start_with?('/')
url_path = request.env['PATH_INFO'].rpartition('/').first << '/' << url_path
end
uri url(url_path, *args), true, false
end

def recognize_path(path)
Expand Down
18 changes: 18 additions & 0 deletions padrino-core/test/test_routing.rb
Expand Up @@ -296,6 +296,24 @@ class Test < Padrino::Application
assert_equal 'http://example.org/test/foo?id=1', body
end

should 'rebase simple string urls to app uri_root' do
mock_app do
set :uri_root, '/app'
get(:a){ url('/foo') }
get(:b){ url('bar') }
get(:c){ absolute_url('/foo') }
get(:d, :map => '/d/e/f'){ absolute_url('bar') }
end
get "/a"
assert_equal "/app/foo", body
get "/b"
assert_equal "bar", body
get "/c"
assert_equal "http://example.org/app/foo", body
get "/d/e/f"
assert_equal "http://example.org/app/d/e/bar", body
end

should 'allow regex url with format' do
mock_app do
get(/.*/, :provides => :any) { "regexp" }
Expand Down

0 comments on commit fdfe6cf

Please sign in to comment.