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] deprecate belongsToReference.push accepting a model #6839

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
148 changes: 66 additions & 82 deletions packages/-ember-data/tests/integration/references/belongs-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module('integration/references/belongs-to', function(hooks) {
assert.equal(familyReference.parent, personReference);
});

test('BelongsToReference#meta() returns the most recent meta for the relationship', function(assert) {
test('BelongsToReference#meta() returns the most recent meta for the relationship', async function(assert) {
let store = this.owner.lookup('service:store');

var person;
Expand Down Expand Up @@ -215,47 +215,41 @@ module('integration/references/belongs-to', function(hooks) {
});
});

testInDebug('push(record)', function(assert) {
var done = assert.async();

testInDebug('push(record)', async function(assert) {
let store = this.owner.lookup('service:store');

var person, family;
run(function() {
person = store.push({
data: {
type: 'person',
id: 1,
relationships: {
family: {
data: { type: 'family', id: 1 },
},
let person = store.push({
data: {
type: 'person',
id: 1,
relationships: {
family: {
data: { type: 'family', id: 1 },
},
},
});
family = store.push({
data: {
type: 'family',
id: 1,
attributes: {
name: 'Coreleone',
},
},
});
},
});
let family = store.push({
data: {
type: 'family',
id: 1,
attributes: {
name: 'Coreleone',
},
},
});

var familyReference = person.belongsTo('family');
let familyReference = person.belongsTo('family');
let Family = store.modelFor('family');

run(function() {
familyReference.push(family).then(function(record) {
assert.ok(Family.detectInstance(record), 'push resolves with the referenced record');
assert.equal(get(record, 'name'), 'Coreleone', 'name is set');
assert.equal(record, family);
let record;
await assert.expectDeprecation(async function() {
record = await familyReference.push(family);
}, /Pushing a record into a BelongsToReference is deprecated/);

done();
});
});
assert.ok(Family.detectInstance(record), 'push resolves with the referenced record');
assert.equal(get(record, 'name'), 'Coreleone', 'name is set');
assert.equal(record, family);
});

test('push(promise)', function(assert) {
Expand Down Expand Up @@ -307,72 +301,62 @@ module('integration/references/belongs-to', function(hooks) {
});
});

testInDebug('push(record) asserts for invalid modelClass', function(assert) {
testInDebug('push(record) asserts for invalid modelClass', async function(assert) {
let store = this.owner.lookup('service:store');

var person, anotherPerson;
run(function() {
person = store.push({
data: {
type: 'person',
id: 1,
relationships: {
family: {
data: { type: 'family', id: 1 },
},
let person = store.push({
data: {
type: 'person',
id: 1,
relationships: {
family: {
data: { type: 'family', id: 1 },
},
},
});
anotherPerson = store.push({
data: {
type: 'person',
id: 2,
},
});
},
});
let anotherPerson = store.push({
data: {
type: 'person',
id: 2,
},
});

var familyReference = person.belongsTo('family');
let familyReference = person.belongsTo('family');

assert.expectAssertion(function() {
run(function() {
familyReference.push(anotherPerson);
});
}, "The 'person' type does not implement 'family' and thus cannot be assigned to the 'family' relationship in 'person'. Make it a descendant of 'family' or use a mixin of the same name.");
await assert.expectDeprecation(async function() {
await assert.expectAssertion(async function() {
await familyReference.push(anotherPerson);
}, "The 'person' type does not implement 'family' and thus cannot be assigned to the 'family' relationship in 'person'. Make it a descendant of 'family' or use a mixin of the same name.");
}, /Pushing a record into a BelongsToReference is deprecated/);
});

testInDebug('push(record) works with polymorphic modelClass', function(assert) {
var done = assert.async();

testInDebug('push(record) works with polymorphic modelClass', async function(assert) {
let store = this.owner.lookup('service:store');
let Family = store.modelFor('family');

var person, mafiaFamily;

this.owner.register('model:mafia-family', Family.extend());

run(function() {
person = store.push({
data: {
type: 'person',
id: 1,
},
});
mafiaFamily = store.push({
data: {
type: 'mafia-family',
id: 1,
},
});
let person = store.push({
data: {
type: 'person',
id: 1,
},
});
let mafiaFamily = store.push({
data: {
type: 'mafia-family',
id: 1,
},
});

var familyReference = person.belongsTo('family');
run(function() {
familyReference.push(mafiaFamily).then(function(family) {
assert.equal(family, mafiaFamily);
let familyReference = person.belongsTo('family');
let family;
await assert.expectDeprecation(async function() {
family = await familyReference.push(mafiaFamily);
}, /Pushing a record into a BelongsToReference is deprecated/);

done();
});
});
assert.equal(family, mafiaFamily);
});

test('value() is null when reference is not yet loaded', function(assert) {
Expand Down
1 change: 1 addition & 0 deletions packages/private-build-infra/addon/current-deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ export default {
DEPRECATE_METHOD_CALLS_ON_DESTROY_STORE: '3.15',
DEPRECATE_MISMATCHED_INVERSE_RELATIONSHIP_DATA: '3.12',
DEPRECATE_SERIALIZER_QUERY_RECORD_ARRAY_RESPONSE: '3.4',
DEPRECATE_BELONGS_TO_REFERENCE_PUSH: '3.16',
};
17 changes: 13 additions & 4 deletions packages/store/addon/-private/system/references/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { assertPolymorphicType } from '@ember-data/store/-debug';
import Reference from './reference';
import recordDataFor from '../record-data-for';
import { peekRecordIdentifier } from '../store/internal-model-factory';
import { deprecate } from '@ember/debug';
import { DEPRECATE_BELONGS_TO_REFERENCE_PUSH } from '@ember-data/private-build-infra/deprecations';

/**
@module @ember-data/store
Expand Down Expand Up @@ -128,10 +130,17 @@ export default class BelongsToReference extends Reference {
return resolve(objectOrPromise).then(data => {
let record;

// TODO deprecate data as Model
if (peekRecordIdentifier(data)) {
record = data;
} else {
if (DEPRECATE_BELONGS_TO_REFERENCE_PUSH) {
if (peekRecordIdentifier(data)) {
runspired marked this conversation as resolved.
Show resolved Hide resolved
deprecate('Pushing a record into a BelongsToReference is deprecated', false, {
id: 'ember-data:belongs-to-reference-push-record',
until: '4.0',
});
record = data;
}
}

if (typeof record === 'undefined') {
record = this.store.push(data);
}

Expand Down