Skip to content

Commit 73a726b

Browse files
authored
deprecation: Model.reopen/reopenClass and eager static fields lookups (#8092)
* remove lots of reopen/reopenClass usage * deprecate the things * fix reopen in json serializer tests * fix more tests * more test cleanup * down to 72 reopens * remove reopen from belongsTo test * refactory hasMany tests * add dep tests * all the deprecations * fix prod test
1 parent a991b6f commit 73a726b

24 files changed

+2934
-1485
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { module } from 'qunit';
2+
3+
import { setupTest } from 'ember-qunit';
4+
5+
import Model from '@ember-data/model';
6+
import { deprecatedTest } from '@ember-data/unpublished-test-infra/test-support/deprecated-test';
7+
8+
module('Deprecations', function (hooks) {
9+
setupTest(hooks);
10+
11+
const StaticModelMethods = [
12+
{ name: 'typeForRelationship', count: 3 },
13+
{ name: 'inverseFor', count: 6 },
14+
{ name: '_findInverseFor', count: 4 },
15+
{ name: 'eachRelationship', count: 3 },
16+
{ name: 'eachRelatedType', count: 3 },
17+
{ name: 'determineRelationshipType', count: 1 },
18+
{ name: 'eachAttribute', count: 2 },
19+
{ name: 'eachTransformedAttribute', count: 4 },
20+
{ name: 'toString', count: 1 },
21+
];
22+
const StaticModelGetters = [
23+
{ name: 'inverseMap', count: 1 },
24+
{ name: 'relationships', count: 3 },
25+
{ name: 'relationshipNames', count: 1 },
26+
{ name: 'relatedTypes', count: 2 },
27+
{ name: 'relationshipsByName', count: 2 },
28+
{ name: 'relationshipsObject', count: 1 },
29+
{ name: 'fields', count: 1 },
30+
{ name: 'attributes', count: 1 },
31+
{ name: 'transformedAttributes', count: 3 },
32+
];
33+
34+
function checkDeprecationForProp(prop) {
35+
deprecatedTest(
36+
`Accessing static prop ${prop.name} is deprecated`,
37+
{ id: 'ember-data:deprecate-early-static', until: '5.0', count: prop.count },
38+
function (assert) {
39+
class Post extends Model {}
40+
Post[prop.name];
41+
assert.ok(true);
42+
}
43+
);
44+
}
45+
function checkDeprecationForMethod(method) {
46+
deprecatedTest(
47+
`Accessing static method ${method.name} is deprecated`,
48+
{ id: 'ember-data:deprecate-early-static', until: '5.0', count: method.count },
49+
function (assert) {
50+
class Post extends Model {}
51+
try {
52+
Post[method.name]();
53+
} catch {
54+
// do nothing
55+
}
56+
assert.ok(true);
57+
}
58+
);
59+
}
60+
61+
StaticModelGetters.forEach(checkDeprecationForProp);
62+
StaticModelMethods.forEach(checkDeprecationForMethod);
63+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { module } from 'qunit';
2+
3+
import { setupTest } from 'ember-qunit';
4+
5+
import Model from '@ember-data/model';
6+
import { deprecatedTest } from '@ember-data/unpublished-test-infra/test-support/deprecated-test';
7+
8+
module('Deprecations', function (hooks) {
9+
setupTest(hooks);
10+
11+
deprecatedTest(
12+
`Calling on natively extended class`,
13+
{ id: 'ember-data:deprecate-model-reopenclass', until: '5.0', count: 1 },
14+
function (assert) {
15+
class Post extends Model {}
16+
Post.reopenClass({});
17+
assert.ok(true);
18+
}
19+
);
20+
21+
deprecatedTest(
22+
`Calling on classic extended class`,
23+
{ id: 'ember-data:deprecate-model-reopenclass', until: '5.0', count: 1 },
24+
function (assert) {
25+
const Post = Model.extend();
26+
Post.reopenClass({});
27+
assert.ok(true);
28+
}
29+
);
30+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { module } from 'qunit';
2+
3+
import { setupTest } from 'ember-qunit';
4+
5+
import Model from '@ember-data/model';
6+
import { deprecatedTest } from '@ember-data/unpublished-test-infra/test-support/deprecated-test';
7+
8+
module('Deprecations', function (hooks) {
9+
setupTest(hooks);
10+
11+
deprecatedTest(
12+
`Calling on natively extended class`,
13+
{ id: 'ember-data:deprecate-model-reopen', until: '5.0', count: 1 },
14+
function (assert) {
15+
class Post extends Model {}
16+
Post.reopen({});
17+
assert.ok(true);
18+
}
19+
);
20+
21+
deprecatedTest(
22+
`Calling on classic extended class`,
23+
{ id: 'ember-data:deprecate-model-reopen', until: '5.0', count: 1 },
24+
function (assert) {
25+
const Post = Model.extend();
26+
Post.reopen({});
27+
assert.ok(true);
28+
}
29+
);
30+
});

packages/-ember-data/tests/integration/debug-adapter-test.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@ import Model, { attr } from '@ember-data/model';
1515
if (has('@ember-data/debug')) {
1616
const DebugAdapter = require('@ember-data/debug').default;
1717

18-
class Post extends Model {
19-
@attr()
20-
title;
21-
}
22-
23-
module('integration/debug-adapter - DS.DebugAdapter', function (hooks) {
18+
module('integration/debug-adapter - DebugAdapter', function (hooks) {
2419
setupTest(hooks);
2520

2621
let store;
2722

2823
hooks.beforeEach(function () {
2924
let { owner } = this;
25+
class Post extends Model {
26+
@attr title;
27+
}
3028

3129
owner.register('model:post', Post);
3230
store = owner.lookup('service:store');
3331
let _adapter = DebugAdapter.extend({
3432
getModelTypes() {
35-
return A([{ klass: Post, name: 'post' }]);
33+
return A([{ klass: store.modelFor('post'), name: 'post' }]);
3634
},
3735
});
3836
owner.register('data-adapter:main', _adapter);
@@ -205,14 +203,13 @@ if (has('@ember-data/debug')) {
205203
let { owner } = this;
206204
let debugAdapter = owner.lookup('data-adapter:main');
207205
class Person extends Model {
208-
@attr()
209-
title;
210-
211-
@attr()
212-
firstOrLastName;
206+
@attr title;
207+
@attr firstOrLastName;
213208
}
209+
owner.register('model:person', Person);
210+
const store = owner.lookup('service:store');
214211

215-
const columns = debugAdapter.columnsForType(Person);
212+
const columns = debugAdapter.columnsForType(store.modelFor('person'));
216213

217214
assert.strictEqual(columns[0].desc, 'Id');
218215
assert.strictEqual(columns[1].desc, 'Title');

packages/-ember-data/tests/integration/records/error-test.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,20 @@ module('integration/records/error', function (hooks) {
197197
let store = this.owner.lookup('service:store');
198198
let adapter = store.adapterFor('application');
199199

200-
adapter.reopen({
201-
createRecord() {
202-
return RSVP.reject(
203-
new InvalidError([
204-
{
205-
detail: 'Must be unique',
206-
source: { pointer: '/data/attributes/first-name' },
207-
},
208-
{
209-
detail: 'Must not be blank',
210-
source: { pointer: '/data/attributes/last-name' },
211-
},
212-
])
213-
);
214-
},
215-
});
200+
adapter.createRecord = () => {
201+
return RSVP.reject(
202+
new InvalidError([
203+
{
204+
detail: 'Must be unique',
205+
source: { pointer: '/data/attributes/first-name' },
206+
},
207+
{
208+
detail: 'Must not be blank',
209+
source: { pointer: '/data/attributes/last-name' },
210+
},
211+
])
212+
);
213+
};
216214

217215
let person = store.createRecord('person');
218216

packages/-ember-data/tests/integration/records/unload-test.js

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,71 @@ function idsFromArr(arr) {
1616
return arr.map((i) => i.id);
1717
}
1818

19-
const Person = Model.extend({
20-
name: attr('string'),
19+
class Person extends Model {
20+
@attr('string') name;
2121
// 1:many sync
22-
cars: hasMany('car', { async: false }),
22+
@hasMany('car', { async: false }) cars;
2323
// 1:many async
24-
boats: hasMany('boat', { async: true }),
24+
@hasMany('boat', { async: true }) boats;
2525
// many:many sync
26-
groups: hasMany('group', { async: false }),
26+
@hasMany('group', { async: false }) groups;
2727
// many:many async
28-
friends: hasMany('people', { async: true }),
28+
@hasMany('people', { async: true }) friends;
2929
// 1:1 sync inverse null
30-
bike: belongsTo('bike', { async: false, inverse: null }),
30+
@belongsTo('bike', { async: false, inverse: null }) bike;
3131
// 1:1 sync
32-
house: belongsTo('house', { async: false }),
32+
@belongsTo('house', { async: false }) house;
3333
// 1:1 async
34-
mortgage: belongsTo('mortgage', { async: true }),
34+
@belongsTo('mortgage', { async: true }) mortgage;
3535
// 1 async : 1 sync
36-
favoriteBook: belongsTo('book', { async: false }),
36+
@belongsTo('book', { async: false }) favoriteBook;
3737
// 1 async : many sync
38-
favoriteSpoons: hasMany('spoon', { async: false }),
38+
@hasMany('spoon', { async: false }) favoriteSpoons;
3939
// 1 sync: many async
40-
favoriteShows: hasMany('show', { async: true }),
40+
@hasMany('show', { async: true }) favoriteShows;
4141
// many sync : many async
42-
favoriteFriends: hasMany('people', { async: true, inverse: 'favoriteAsyncFriends' }),
42+
@hasMany('people', { async: true, inverse: 'favoriteAsyncFriends' }) favoriteFriends;
4343
// many async : many sync
44-
favoriteAsyncFriends: hasMany('people', { async: false, inverse: 'favoriteFriends' }),
45-
});
46-
Person.reopenClass({
47-
toString() {
44+
@hasMany('people', { async: false, inverse: 'favoriteFriends' }) favoriteAsyncFriends;
45+
46+
static toString() {
4847
return 'Person';
49-
},
50-
});
48+
}
49+
}
5150

52-
const House = Model.extend({
53-
person: belongsTo('person', { async: false }),
54-
});
55-
House.reopenClass({
56-
toString() {
51+
class House extends Model {
52+
@belongsTo('person', { async: false }) person;
53+
54+
static toString() {
5755
return 'House';
58-
},
59-
});
56+
}
57+
}
6058

61-
const Mortgage = Model.extend({
62-
person: belongsTo('person', { async: true }),
63-
});
64-
Mortgage.reopenClass({
65-
toString() {
59+
class Mortgage extends Model {
60+
@belongsTo('person', { async: true }) person;
61+
62+
static toString() {
6663
return 'Mortgage';
67-
},
68-
});
64+
}
65+
}
6966

70-
const Group = Model.extend({
71-
people: hasMany('person', { async: false }),
72-
});
73-
Group.reopenClass({
74-
toString() {
67+
class Group extends Model {
68+
@hasMany('person', { async: false }) people;
69+
70+
static toString() {
7571
return 'Group';
76-
},
77-
});
72+
}
73+
}
7874

79-
const Car = Model.extend({
80-
make: attr('string'),
81-
model: attr('string'),
82-
person: belongsTo('person', { async: false }),
83-
});
84-
Car.reopenClass({
85-
toString() {
75+
class Car extends Model {
76+
@attr('string') make;
77+
@attr('string') model;
78+
@belongsTo('person', { async: false }) person;
79+
80+
static toString() {
8681
return 'Car';
87-
},
88-
});
82+
}
83+
}
8984

9085
const Boat = Model.extend({
9186
name: attr('string'),

0 commit comments

Comments
 (0)