Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added prefix option to router. #58

Merged
merged 2 commits into from
Jun 10, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions lib/lotus/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def defined?
# # It will map to Flowers::Index.new, which is the
# # Lotus::Controller convention.
def get(path, options = {}, &blk)
@router.get(path, options, &blk)
@router.prefixed_namespace.get(path, options, &blk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlfonsoUceda This leaks Lotus::Routing::HttpRouter details to the higher level. That low level class should hide this detail. Lotus::Router#get should remain untouched, while Lotus::Routing::HttpRouter#get should handle this.

end

# Defines a route that accepts a POST request for the given path.
Expand All @@ -360,7 +360,7 @@ def get(path, options = {}, &blk)
#
# @since 0.1.0
def post(path, options = {}, &blk)
@router.post(path, options, &blk)
@router.prefixed_namespace.post(path, options, &blk)
end

# Defines a route that accepts a PUT request for the given path.
Expand All @@ -379,7 +379,7 @@ def post(path, options = {}, &blk)
#
# @since 0.1.0
def put(path, options = {}, &blk)
@router.put(path, options, &blk)
@router.prefixed_namespace.put(path, options, &blk)
end

# Defines a route that accepts a PATCH request for the given path.
Expand All @@ -398,7 +398,7 @@ def put(path, options = {}, &blk)
#
# @since 0.1.0
def patch(path, options = {}, &blk)
@router.patch(path, options, &blk)
@router.prefixed_namespace.patch(path, options, &blk)
end

# Defines a route that accepts a DELETE request for the given path.
Expand All @@ -417,7 +417,7 @@ def patch(path, options = {}, &blk)
#
# @since 0.1.0
def delete(path, options = {}, &blk)
@router.delete(path, options, &blk)
@router.prefixed_namespace.delete(path, options, &blk)
end

# Defines a route that accepts a TRACE request for the given path.
Expand All @@ -436,7 +436,7 @@ def delete(path, options = {}, &blk)
#
# @since 0.1.0
def trace(path, options = {}, &blk)
@router.trace(path, options, &blk)
@router.prefixed_namespace.trace(path, options, &blk)
end

# Defines a route that accepts a OPTIONS request for the given path.
Expand All @@ -455,7 +455,7 @@ def trace(path, options = {}, &blk)
#
# @since 0.1.0
def options(path, options = {}, &blk)
@router.options(path, options, &blk)
@router.prefixed_namespace.options(path, options, &blk)
end

# Defines an HTTP redirect
Expand Down
9 changes: 9 additions & 0 deletions lib/lotus/routing/http_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def initialize(options = {}, &blk)
@route_class = options[:route] || Routing::Route
@resolver = options[:resolver] || Routing::EndpointResolver.new(options)
@parsers = Routing::Parsers.new(options[:parsers])
@prefix = Routing::Namespace.new(self, options[:prefix]) if options[:prefix]
end

# Separator between controller and action name.
Expand Down Expand Up @@ -133,6 +134,14 @@ def no_response(request, env)
end
end

# Returns a prefix namespace or the current router
#
# @since x.x.x
# @api private
def prefixed_namespace
@prefix || self
end

private
def add_with_request_method(path, method, opts = {}, &app)
super.generate(@resolver, opts, &app)
Expand Down
8 changes: 7 additions & 1 deletion lib/lotus/routing/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(router, name, &blk)
@router = router
@name = Utils::PathPrefix.new(name)
__setobj__(@router)
instance_eval(&blk)
instance_eval(&blk) if block_given?
end

# @api private
Expand Down Expand Up @@ -57,6 +57,12 @@ def trace(path, options = {}, &endpoint)
super(@name.join(path), options, &endpoint)
end

# @api private
# @since 0.1.0
def options(path, options = {}, &endpoint)
super(@name.join(path), options, &endpoint)
end

# @api private
# @since 0.1.0
def resource(name, options = {})
Expand Down
56 changes: 56 additions & 0 deletions test/prefix_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'test_helper'

describe Lotus::Router do
describe 'with prefix option' do
it 'generates routes with prefix' do
router = Lotus::Router.new(prefix: '/admin') do
get '/home', to: 'home#index', as: :get_home
post '/home', to: 'home#index', as: :post_home
put '/home', to: 'home#index', as: :put_home
patch '/home', to: 'home#index', as: :patch_home
delete '/home', to: 'home#index', as: :delete_home
trace '/home', to: 'home#index', as: :trace_home
options '/home', to: 'home#index', as: :options_home


resources :users
resource :asteroid

namespace :dashboard do
get '/home', to: 'dashboard#index', as: :dashboard_home
end
end
router.path(:get_home).must_equal '/admin/home'
router.path(:post_home).must_equal '/admin/home'
router.path(:put_home).must_equal '/admin/home'
router.path(:patch_home).must_equal '/admin/home'
router.path(:delete_home).must_equal '/admin/home'
router.path(:trace_home).must_equal '/admin/home'
router.path(:options_home).must_equal'/admin/home'

router.path(:users).must_equal '/admin/users'
router.path(:new_user).must_equal '/admin/users/new'
router.path(:users).must_equal '/admin/users'
router.path(:user, id: 1).must_equal '/admin/users/1'
router.path(:edit_user, id: 1).must_equal '/admin/users/1/edit'

router.path(:new_asteroid).must_equal '/admin/asteroid/new'
router.path(:asteroid).must_equal '/admin/asteroid'
router.path(:edit_asteroid).must_equal '/admin/asteroid/edit'

router.path(:dashboard_home).must_equal '/admin/dashboard/home'
end

it 'redirect works with prefix' do
router = Lotus::Router.new(prefix: '/admin')
endpoint = ->(env) { [200, {}, ['Redirect destination!']] }
router.redirect('/redirect', to: '/redirect_destination')

env = Rack::MockRequest.env_for('/admin/redirect')
status, headers, _ = router.call(env)

status.must_equal 301
headers['Location'].must_equal '/redirect_destination'
end
end
end