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

[BUGFIX release] Improve assert in default store #19858

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 22 additions & 6 deletions packages/@ember/-internals/routing/lib/system/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import Controller from '@ember/controller';
import { assert, info, isTesting } from '@ember/debug';
import { dependentKeyCompat } from '@ember/object/compat';
import { once } from '@ember/runloop';
import { classify } from '@ember/string';
import { DEBUG } from '@glimmer/env';
import { Template, TemplateFactory } from '@glimmer/interfaces';
import {
Expand Down Expand Up @@ -1736,16 +1735,18 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute
protected get store() {
let owner = getOwner(this);
let routeName = this.routeName;
let namespace = get(this, '_router.namespace');

return {
find(name: string, value: unknown) {
let modelClass: any = owner.factoryFor(`model:${name}`);

assert(
`You used the dynamic segment ${name}_id in your route ${routeName}, but ${namespace}.${classify(
name
)} did not exist and you did not override your route's \`model\` hook.`,
`You used the dynamic segment \`${name}_id\` in your route ` +
`\`${routeName}\` for which Ember requires you provide a ` +
`data-loading implementation. Commonly, that is done by ` +
`adding a model hook implementation on the route ` +
`(\`model({${name}_id}) {\`) or by injecting an implemention of ` +
`a data store: \`@service store;\`.`,
Boolean(modelClass)
);

Expand All @@ -1755,7 +1756,22 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute

modelClass = modelClass.class;

assert(`${classify(name)} has no method \`find\`.`, typeof modelClass.find === 'function');
assert(
`You used the dynamic segment \`${name}_id\` in your route ` +
`\`${routeName}\` for which Ember requires you provide a ` +
`data-loading implementation. Commonly, that is done by ` +
`adding a model hook implementation on the route ` +
`(\`model({${name}_id}) {\`) or by injecting an implemention of ` +
`a data store: \`@service store;\`.\n\n` +
`Rarely, applications may attempt to use a legacy behavior where ` +
`the model class (in this case \`${name}\`) is resolved and the ` +
`\`find\` method on that class is invoked to load data. In this ` +
`application, a model of \`${name}\` was found but it did not ` +
`provide a \`find\` method. You should not add a \`find\` ` +
`method to your model. Instead, please implement an appropriate ` +
`\`model\` hook on the \`${routeName}\` route.`,
typeof modelClass.find === 'function'
);

return modelClass.find(value);
},
Expand Down
35 changes: 31 additions & 4 deletions packages/@ember/-internals/routing/tests/system/route_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,31 @@ moduleFor(
let owner = buildOwner();
let Post = EmberObject.extend();

owner.register('route:index', EmberRoute);
owner.register(
'route:index',
EmberRoute.extend({
routeName: 'index',
})
);
owner.register('model:post', Post);

route = owner.lookup('route:index');

expectAssertion(function () {
route.findModel('post', 1);
}, 'Post has no method `find`.');
}, `You used the dynamic segment \`post_id\` in your route ` +
`\`index\` for which Ember requires you provide a ` +
`data-loading implementation. Commonly, that is done by ` +
`adding a model hook implementation on the route ` +
`(\`model({post_id}) {\`) or by injecting an implemention of ` +
`a data store: \`@service store;\`.\n\n` +
`Rarely, applications may attempt to use a legacy behavior where ` +
`the model class (in this case \`post\`) is resolved and the ` +
`\`find\` method on that class is invoked to load data. In this ` +
`application, a model of \`post\` was found but it did not ` +
`provide a \`find\` method. You should not add a \`find\` ` +
`method to your model. Instead, please implement an appropriate ` +
`\`model\` hook on the \`index\` route.`);

runDestroy(owner);
}
Expand All @@ -86,13 +103,23 @@ moduleFor(
runDestroy(route);

let owner = buildOwner();
owner.register('route:index', EmberRoute);
owner.register(
'route:index',
EmberRoute.extend({
routeName: 'index',
})
);

route = owner.lookup('route:index');

expectAssertion(function () {
route.model({ post_id: 1 });
}, /You used the dynamic segment post_id in your route undefined, but <.*:ember\d+>.Post did not exist and you did not override your route's `model` hook./);
}, `You used the dynamic segment \`post_id\` in your route ` +
`\`index\` for which Ember requires you provide a ` +
`data-loading implementation. Commonly, that is done by ` +
`adding a model hook implementation on the route ` +
`(\`model({post_id}) {\`) or by injecting an implemention of ` +
`a data store: \`@service store;\`.`);

runDestroy(owner);
}
Expand Down