Skip to content

Commit

Permalink
feat(routing): prefix route's name via group
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Jan 18, 2019
1 parent a4998b4 commit 98ca091
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Route/Group.js
Expand Up @@ -37,6 +37,31 @@ class RouteGroup extends Macroable {
this._name = name
}

/**
* Give a name to a group of routes.
* This will prefix all routes name.
*
* @method as
*
* @chainable
*
* @example
* ```js
* Route
* .group()
* .as('admin')
* ```
*/
as (name) {
this._routes.forEach((route) => {
if (route.name.length > 0) {
route.name = `${name}.${route.name}`
}
})

return this
}

/**
* Add middleware to a group of routes.
* Also see @ref('Route/middleware').
Expand Down
7 changes: 7 additions & 0 deletions test/unit/route.spec.js
Expand Up @@ -267,6 +267,13 @@ test.group('Route | Group', () => {
assert.deepEqual(group._routes, [route])
})

test('prefix route\'s name via group', (assert) => {
const route = new Route('/', () => {}).as('test')
const group = new RouteGroup([route])
group.as('admin')
assert.equal(route.name, 'admin.test')
})

test('add middleware to route via group', (assert) => {
const route = new Route('/', function () {})
const group = new RouteGroup([route])
Expand Down

0 comments on commit 98ca091

Please sign in to comment.