Skip to content

Commit

Permalink
fix: model-less controllers prevent applications from starting (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba authored Aug 14, 2016
1 parent 2c90cfa commit 2b2829f
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 46 deletions.
4 changes: 3 additions & 1 deletion src/packages/application/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import loader from '../loader';

import { ControllerMissingError, SerializerMissingError } from './errors';

import { tryCatchSync } from '../../utils/try-catch';

import type Application, { Application$opts } from './index'; // eslint-disable-line no-unused-vars, max-len

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

controllers.forEach((controller, key) => {
if (key !== 'application') {
const model = store.modelFor(singularize(key));
const model = tryCatchSync(() => store.modelFor(singularize(key)));

controller = new controller({
store,
Expand Down
48 changes: 42 additions & 6 deletions src/packages/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ import type { Controller$opts } from './interfaces';
* and returns data relative to what the client has request.
*/
class Controller {
/**
* A boolean value representing whether or not a `Controller` has a backing
* `Model`.
*
* @property hasModel
* @memberof Controller
* @instance
*/
hasModel: boolean;

/**
* A boolean value representing whether or not a `Controller` has a backing
* `Serializer`.
*
* @property hasSerializer
* @memberof Controller
* @instance
*/
hasSerializer: boolean;

/**
* The number of records to return for the #index action when a `?limit`
* parameter is not specified.
Expand Down Expand Up @@ -111,10 +131,12 @@ class Controller {
controllers,
parentController
}: Controller$opts) {
const hasModel = Boolean(model);
const hasSerializer = Boolean(serializer);
let attributes = [];
let relationships = [];

if (model && serializer) {
if (hasModel && hasSerializer) {
const { primaryKey, attributeNames, relationshipNames } = model;
const { attributes: serializedAttributes } = serializer;

Expand Down Expand Up @@ -143,22 +165,36 @@ class Controller {
configurable: false
},

hasModel: {
value: hasModel,
writable: false,
enumerable: true,
configurable: false
},

modelName: {
value: hasModel ? model.modelName : '',
writable: false,
enumerable: false,
configurable: false
},

serializer: {
value: serializer,
writable: false,
enumerable: false,
configurable: false
},

store: {
value: store,
hasSerializer: {
value: hasSerializer,
writable: false,
enumerable: false,
enumerable: true,
configurable: false
},

modelName: {
value: model ? model.modelName : '',
store: {
value: store,
writable: false,
enumerable: false,
configurable: false
Expand Down
20 changes: 11 additions & 9 deletions src/packages/route/action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ export function createAction(
controller: Controller,
action: Action<any>
): Array<Action<any>> {
switch (name) {
case 'index':
action = collection(action);
break;
if (controller.hasModel && controller.hasSerializer) {
switch (name) {
case 'index':
action = collection(action);
break;

case 'create':
case 'update':
case 'show':
action = member(action);
break;
case 'create':
case 'update':
case 'show':
action = member(action);
break;
}
}

function __FINAL__HANDLER__(req, res) {
Expand Down
24 changes: 14 additions & 10 deletions src/packages/route/params/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,22 @@ export function defaultParamsFor({
action: string;
controller: Controller
}): Object {
switch (action) {
case 'index':
return getDefaultCollectionParams(controller);
if (controller.hasModel) {
switch (action) {
case 'index':
return getDefaultCollectionParams(controller);

case 'show':
case 'create':
case 'update':
case 'destroy':
return getDefaultMemberParams(controller);
case 'show':
case 'create':
case 'update':
case 'destroy':
return getDefaultMemberParams(controller);

default:
return {};
default:
return {};
}
} else {
return {};
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/packages/route/params/utils/get-data-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,21 @@ export default function getDataParams(
controller: Controller,
includeID: boolean
): [string, ParameterLike] {
let params = [
getTypeParam(controller),
getAttributesParam(controller),
getRelationshipsParam(controller)
];
let params = [getTypeParam(controller)];

if (includeID) {
if (controller.hasModel) {
params = [
getIDParam(controller),
getAttributesParam(controller),
getRelationshipsParam(controller),
...params
];

if (includeID) {
params = [
getIDParam(controller),
...params
];
}
}

return ['data', new ParameterGroup(params, {
Expand Down
34 changes: 21 additions & 13 deletions src/packages/route/params/utils/get-query-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,15 @@ function getCustomParams({
export function getMemberQueryParams(
controller: Controller
): Array<[string, ParameterLike]> {
return [
getFieldsParam(controller),
getIncludeParam(controller),
...getCustomParams(controller)
];
if (controller.hasModel) {
return [
getFieldsParam(controller),
getIncludeParam(controller),
...getCustomParams(controller)
];
} else {
return getCustomParams(controller);
}
}

/**
Expand All @@ -137,12 +141,16 @@ export function getMemberQueryParams(
export function getCollectionQueryParams(
controller: Controller
): Array<[string, ParameterLike]> {
return [
getPageParam(),
getSortParam(controller),
getFilterParam(controller),
getFieldsParam(controller),
getIncludeParam(controller),
...getCustomParams(controller)
];
if (controller.hasModel) {
return [
getPageParam(),
getSortParam(controller),
getFilterParam(controller),
getFieldsParam(controller),
getIncludeParam(controller),
...getCustomParams(controller)
];
} else {
return getCustomParams(controller);
}
}

0 comments on commit 2b2829f

Please sign in to comment.