Skip to content

Commit

Permalink
fix(routing): allow to prefix a group without a starting slash (#416)
Browse files Browse the repository at this point in the history
* test(routing): test that prefix works without start slash

* fix(routing): allow to prefix a group without a starting slash

We should be able to prefix route's group without writing the first
slash.

Example:
```javascript
Route.group('v1', function () {
  // Some routes...
}).prefix('api/v1') // before it should be .prefix('/api/v1')
```

Closes: #415
  • Loading branch information
RomainLanz authored and thetutlage committed Jan 12, 2017
1 parent 58b22e9 commit 391b149
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Route/helpers.js
Expand Up @@ -155,6 +155,7 @@ RouterHelper.addFormats = function (routes, formats, strict) {
* @private
*/
RouterHelper.prefixRoute = function (routes, prefix) {
prefix = prefix.startsWith('/') ? prefix : `/${prefix}`
_.each(routes, function (route) {
route.route = route.route === '/' ? prefix : prefix + route.route
route.pattern = RouterHelper.makeRoutePattern(route.route)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/route.spec.js
Expand Up @@ -268,6 +268,15 @@ describe('Route', function () {
expect(routes[0].route).to.equal('/v1')
})

it('should be able to prefix routes inside a group without starting with a slash', function () {
Route.group('admin', function () {
Route.get('/', 'SomeController.method')
}).prefix('v1')
const routes = Route.routes()
expect(routes[0]).to.be.an('object')
expect(routes[0].route).to.equal('/v1')
})

it('should prefix all resourceful routes under a group', function () {
Route.group('v1', function () {
Route.resource('admin', 'SomeController')
Expand Down

0 comments on commit 391b149

Please sign in to comment.