Skip to content

Commit

Permalink
fix: namespaced resources without root controllers/serializers is not… (
Browse files Browse the repository at this point in the history
#454)

* fix: namespaced resources without root controllers/serializers is not working

* chore: remove unecessary export from loader/builder/index.js
  • Loading branch information
zacharygolba authored Oct 12, 2016
1 parent a2bee41 commit 0a6c953
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/packages/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type Server from '../server';
import type Controller from '../controller';
import type Serializer from '../serializer';
import type Database, { Model } from '../database';
import type { FreezeableMap } from '../freezeable';

import initialize from './initialize';
import type { Application$opts } from './interfaces';
Expand Down Expand Up @@ -58,7 +59,7 @@ class Application {
* @instance
* @readonly
*/
models: Map<string, Class<Model>>;
models: FreezeableMap<string, Class<Model>>;

/**
* A reference to the instance of `Logger`.
Expand All @@ -78,7 +79,7 @@ class Application {
* @instance
* @readonly
*/
controllers: Map<string, Controller>;
controllers: FreezeableMap<string, Controller>;

/**
* A map containing each `Serializer` class in an application instance.
Expand All @@ -88,7 +89,7 @@ class Application {
* @instance
* @readonly
*/
serializers: Map<string, Serializer<*>>;
serializers: FreezeableMap<string, Serializer<*>>;

/**
* A reference to the instance of `Router`.
Expand Down
4 changes: 2 additions & 2 deletions src/packages/application/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Database from '../database';
import Logger from '../logger';
import Router from '../router';
import Server from '../server';
import { build, createLoader } from '../loader';
import { build, createLoader, closestChild } from '../loader';
import { freezeProps, deepFreezeProps } from '../freezeable';
import ControllerMissingError from '../../errors/controller-missing-error';

Expand Down Expand Up @@ -49,7 +49,7 @@ export default async function initialize<T: Application>(app: T, {

models.forEach(model => {
Reflect.defineProperty(model, 'serializer', {
value: serializers.get(model.resourceName),
value: closestChild(serializers, model.resourceName),
writable: false,
enumerable: false,
configurable: false
Expand Down
1 change: 1 addition & 0 deletions src/packages/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { build } from './builder';
export {
stripNamespaces,
closestAncestor,
closestChild,
getParentKey as getNamespaceKey
} from './resolver';

Expand Down
1 change: 1 addition & 0 deletions src/packages/loader/resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ export function resolve<T>(
export { default as getParentKey } from './utils/get-parent-key';
export { default as stripNamespaces } from './utils/strip-namespaces';
export { default as closestAncestor } from './utils/closest-ancestor';
export { default as closestChild } from './utils/closest-child';
14 changes: 14 additions & 0 deletions src/packages/loader/resolver/utils/closest-child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @flow
import type { Bundle$Namespace } from '../../index';

export default function closestChild<T>(
source: Bundle$Namespace<T>,
key: string
): void | T {
const [[, result] = []] = Array
.from(source)
.map(([path, value]) => [path.split('/').pop(), value])
.filter(([resource]) => key === resource);

return result;
}
42 changes: 42 additions & 0 deletions src/packages/loader/test/resolver.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @flow
import { expect } from 'chai';
import { it, describe, before } from 'mocha';

import { getTestApp } from '../../../../test/utils/get-test-app';
import { closestChild, closestAncestor } from '../resolver';

describe('module "loader/resolver"', () => {
describe('#closestChild()', () => {
let serializers;

before(async () => {
const app = await getTestApp();

serializers = app.serializers;
});

it('can find the closest child by a namespaced key suffix', () => {
const result = closestChild(serializers, 'users');

expect(result).to.be.ok;
});
});

describe('#closestAncestor()', () => {
let serializers;

before(async () => {
const app = await getTestApp();

serializers = app.serializers;
});

it('can find the closest ancestor by a namespaced key', () => {
const result = closestAncestor(serializers, 'admin/users');

expect(result)
.to.be.ok
.and.have.deep.property('constructor.name', 'UsersSerializer');
});
});
});

0 comments on commit 0a6c953

Please sign in to comment.