Skip to content

Commit

Permalink
Merge pull request #19857 from mixonic/mixonic/update-assertion-language
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Nov 30, 2021
2 parents 2c72b85 + 9937a50 commit a57b157
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
25 changes: 19 additions & 6 deletions packages/@ember/-internals/routing/lib/system/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { ROUTER_EVENTS } from '@ember/deprecated-features';
import { dependentKeyCompat } from '@ember/object/compat';
import { assign } from '@ember/polyfills';
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 @@ -2334,16 +2333,18 @@ Route.reopen(ActionHandler, Evented, {
get(this: Route) {
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 @@ -2354,7 +2355,19 @@ Route.reopen(ActionHandler, Evented, {
modelClass = modelClass.class;

assert(
`${classify(name)} has no method \`find\`.`,
`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'
);

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 @@ -207,14 +207,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 @@ -223,13 +240,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

0 comments on commit a57b157

Please sign in to comment.