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

deprecation: Model.reopen/reopenClass and eager static fields lookups #8092

Merged
merged 11 commits into from
Jul 30, 2022
Prev Previous commit
Next Next commit
add dep tests
runspired committed Jul 30, 2022
commit 9bae1915c0a4b82ef11c1ff46bc28bab65e291ef
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { module } from 'qunit';

import { setupTest } from 'ember-qunit';

import Model from '@ember-data/model';
import { deprecatedTest } from '@ember-data/unpublished-test-infra/test-support/deprecated-test';

module('Deprecations', function (hooks) {
setupTest(hooks);

const StaticModelMethods = [
{ name: 'typeForRelationship', count: 3 },
{ name: 'inverseFor', count: 6 },
{ name: '_findInverseFor', count: 4 },
{ name: 'eachRelationship', count: 3 },
{ name: 'eachRelatedType', count: 3 },
{ name: 'determineRelationshipType', count: 1 },
{ name: 'eachAttribute', count: 2 },
{ name: 'eachTransformedAttribute', count: 4 },
{ name: 'toString', count: 1 },
];
const StaticModelGetters = [
{ name: 'inverseMap', count: 1 },
{ name: 'relationships', count: 3 },
{ name: 'relationshipNames', count: 1 },
{ name: 'relatedTypes', count: 2 },
{ name: 'relationshipsByName', count: 2 },
{ name: 'relationshipsObject', count: 1 },
{ name: 'fields', count: 1 },
{ name: 'attributes', count: 1 },
{ name: 'transformedAttributes', count: 3 },
];

function checkDeprecationForProp(prop) {
deprecatedTest(
`Accessing static prop ${prop.name} is deprecated`,
{ id: 'ember-data:deprecate-early-static', until: '5.0', count: prop.count },
function (assert) {
class Post extends Model {}
Post[prop.name];
}
);
}
function checkDeprecationForMethod(method) {
deprecatedTest(
`Accessing static method ${method.name} is deprecated`,
{ id: 'ember-data:deprecate-early-static', until: '5.0', count: method.count },
function (assert) {
class Post extends Model {}
try {
Post[method.name]();
} catch {
// do nothing
}
}
);
}

StaticModelGetters.forEach(checkDeprecationForProp);
StaticModelMethods.forEach(checkDeprecationForMethod);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { module } from 'qunit';

import { setupTest } from 'ember-qunit';

import Model from '@ember-data/model';
import { deprecatedTest } from '@ember-data/unpublished-test-infra/test-support/deprecated-test';

module('Deprecations', function (hooks) {
setupTest(hooks);

deprecatedTest(
`Calling on natively extended class`,
{ id: 'ember-data:deprecate-model-reopenclass', until: '5.0', count: 1 },
function (assert) {
class Post extends Model {}
Post.reopenClass({});
}
);

deprecatedTest(
`Calling on classic extended class`,
{ id: 'ember-data:deprecate-model-reopenclass', until: '5.0', count: 1 },
function (assert) {
const Post = Model.extend();
Post.reopenClass({});
}
);
});
28 changes: 28 additions & 0 deletions packages/-ember-data/tests/deprecations/deprecate-reopen-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { module } from 'qunit';

import { setupTest } from 'ember-qunit';

import Model from '@ember-data/model';
import { deprecatedTest } from '@ember-data/unpublished-test-infra/test-support/deprecated-test';

module('Deprecations', function (hooks) {
setupTest(hooks);

deprecatedTest(
`Calling on natively extended class`,
{ id: 'ember-data:deprecate-model-reopen', until: '5.0', count: 1 },
function (assert) {
class Post extends Model {}
Post.reopen({});
}
);

deprecatedTest(
`Calling on classic extended class`,
{ id: 'ember-data:deprecate-model-reopen', until: '5.0', count: 1 },
function (assert) {
const Post = Model.extend();
Post.reopen({});
}
);
});
23 changes: 10 additions & 13 deletions packages/-ember-data/tests/integration/debug-adapter-test.js
Original file line number Diff line number Diff line change
@@ -15,24 +15,22 @@ import Model, { attr } from '@ember-data/model';
if (has('@ember-data/debug')) {
const DebugAdapter = require('@ember-data/debug').default;

class Post extends Model {
@attr()
title;
}

module('integration/debug-adapter - DS.DebugAdapter', function (hooks) {
module('integration/debug-adapter - DebugAdapter', function (hooks) {
setupTest(hooks);

let store;

hooks.beforeEach(function () {
let { owner } = this;
class Post extends Model {
@attr title;
}

owner.register('model:post', Post);
store = owner.lookup('service:store');
let _adapter = DebugAdapter.extend({
getModelTypes() {
return A([{ klass: Post, name: 'post' }]);
return A([{ klass: store.modelFor('post'), name: 'post' }]);
},
});
owner.register('data-adapter:main', _adapter);
@@ -205,14 +203,13 @@ if (has('@ember-data/debug')) {
let { owner } = this;
let debugAdapter = owner.lookup('data-adapter:main');
class Person extends Model {
@attr()
title;

@attr()
firstOrLastName;
@attr title;
@attr firstOrLastName;
}
owner.register('model:person', Person);
const store = owner.lookup('service:store');

const columns = debugAdapter.columnsForType(Person);
const columns = debugAdapter.columnsForType(store.modelFor('person'));

assert.strictEqual(columns[0].desc, 'Id');
assert.strictEqual(columns[1].desc, 'Title');
39 changes: 22 additions & 17 deletions packages/-ember-data/tests/unit/utils-test.js
Original file line number Diff line number Diff line change
@@ -3,10 +3,9 @@ import { run } from '@ember/runloop';

import { module, test } from 'qunit';

import DS from 'ember-data';
import { setupTest } from 'ember-qunit';

import Model from '@ember-data/model';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { modelHasAttributeOrRelationshipNamedType } from '@ember-data/serializer/-private';
import { assertPolymorphicType } from '@ember-data/store/-debug';
import testInDebug from '@ember-data/unpublished-test-infra/test-support/test-in-debug';
@@ -17,12 +16,12 @@ module('unit/utils', function (hooks) {
hooks.beforeEach(function () {
const Person = Model.extend();
const User = Model.extend({
messages: DS.hasMany('message', { async: false }),
messages: hasMany('message', { async: false }),
});

const Message = Model.extend();
const Post = Message.extend({
medias: DS.hasMany('medium', { async: false }),
medias: hasMany('medium', { async: false }),
});

const Medium = Mixin.create();
@@ -86,21 +85,27 @@ module('unit/utils', function (hooks) {
});

test('modelHasAttributeOrRelationshipNamedType', function (assert) {
let ModelWithTypeAttribute = Model.extend({
type: DS.attr(),
});
let ModelWithTypeBelongsTo = Model.extend({
type: DS.belongsTo(),
});
let ModelWithTypeHasMany = Model.extend({
type: DS.hasMany(),
});
class Blank extends Model {}
class ModelWithTypeAttribute extends Model {
@attr type;
}
class ModelWithTypeBelongsTo extends Model {
@belongsTo type;
}
class ModelWithTypeHasMany extends Model {
@hasMany type;
}
this.owner.register('model:blank', Blank);
this.owner.register('model:with-attr', ModelWithTypeAttribute);
this.owner.register('model:with-belongs-to', ModelWithTypeBelongsTo);
this.owner.register('model:with-has-many', ModelWithTypeHasMany);
const store = this.owner.lookup('service:store');

assert.false(modelHasAttributeOrRelationshipNamedType(Model));
assert.false(modelHasAttributeOrRelationshipNamedType(store.modelFor('blank')));

assert.true(modelHasAttributeOrRelationshipNamedType(ModelWithTypeAttribute));
assert.true(modelHasAttributeOrRelationshipNamedType(ModelWithTypeBelongsTo));
assert.true(modelHasAttributeOrRelationshipNamedType(ModelWithTypeHasMany));
assert.true(modelHasAttributeOrRelationshipNamedType(store.modelFor('with-attr')));
assert.true(modelHasAttributeOrRelationshipNamedType(store.modelFor('with-belongs-to')));
assert.true(modelHasAttributeOrRelationshipNamedType(store.modelFor('with-has-many')));
});

testInDebug('assertPolymorphicType works for mixins', function (assert) {
17 changes: 17 additions & 0 deletions packages/model/addon/-private/model.js
Original file line number Diff line number Diff line change
@@ -2278,6 +2278,23 @@ class Model extends EmberObject {
@static
*/
static toString() {
if (DEPRECATE_EARLY_STATIC) {
deprecate(
`Accessing schema information on Models without looking up the model via the store is deprecated. Use store.modelFor (or better Snapshots or the store.getSchemaDefinitionService() apis) instead.`,
this.modelName,
{
id: 'ember-data:deprecate-early-static',
for: 'ember-data',
until: '5.0',
since: { available: '4.8', enabled: '4.8' },
}
);
} else {
assert(
`Accessing schema information on Models without looking up the model via the store is disallowed.`,
this.modelName
);
}
return `model:${get(this, 'modelName')}`;
}
}