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

fix: Schema.eachTransformedAttribute API should match Model.eachTransformedAttribute #8346

Merged
merged 1 commit into from
Dec 8, 2022
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
5 changes: 1 addition & 4 deletions ember-data-types/q/ds-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ export interface ModelSchema {
relationshipsByName: Map<string, RelationshipSchema>;
eachAttribute<T>(callback: (this: T, key: string, attribute: AttributeSchema) => void, binding?: T): void;
eachRelationship<T>(callback: (this: T, key: string, relationship: RelationshipSchema) => void, binding?: T): void;
eachTransformedAttribute<T>(
callback: (this: T, key: string, relationship: RelationshipSchema) => void,
binding?: T
): void;
eachTransformedAttribute<T>(callback: (this: T, key: string, type: string) => void, binding?: T): void;
toString(): string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,11 @@ export default class ShimModelClass implements ModelSchema {
});
}

eachTransformedAttribute<T>(
callback: (this: T | undefined, key: string, relationship: RelationshipSchema) => void,
binding?: T
) {
let relationshipDefs = this.__store
.getSchemaDefinitionService()
.relationshipsDefinitionFor({ type: this.modelName });
Object.keys(relationshipDefs).forEach((key) => {
if (relationshipDefs[key]!.type) {
callback.call(binding, key, relationshipDefs[key] as RelationshipSchema);
eachTransformedAttribute<T>(callback: (this: T | undefined, key: string, type: string) => void, binding?: T) {
const attrDefs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
Object.keys(attrDefs).forEach((key) => {
if (attrDefs[key]!.type) {
callback.call(binding, key, attrDefs[key]!.type);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/store/addon/-private/store-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ class Store extends Service {
@return {subclass of Model | ShimModelClass}
*/
// TODO @deprecate in favor of schema APIs, requires adapter/serializer overhaul or replacement
declare _forceShim: boolean;
modelFor(modelName: string): ShimModelClass | DSModelClass {
if (DEBUG) {
assertDestroyedStoreOnly(this, 'modelFor');
Expand All @@ -498,7 +499,7 @@ class Store extends Service {

// for factorFor factory/class split
let klass = maybeFactory && maybeFactory.class ? maybeFactory.class : maybeFactory;
if (!klass || !klass.isModel) {
if (!klass || !klass.isModel || this._forceShim) {
assert(
`No model was found for '${modelName}' and no schema handles the type`,
this.getSchemaDefinitionService().doesTypeExist(modelName)
Expand Down
67 changes: 67 additions & 0 deletions tests/main/tests/integration/store/shim-model-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { module, test } from 'qunit';

import { setupTest } from 'ember-qunit';

import Model, { attr } from '@ember-data/model';

module('@ember-data/store | Schemas', function (hooks) {
setupTest(hooks);

test('Schema.eachTransformedAttribute works as expected on legacy Model', async function (assert) {
class User extends Model {
@attr firstName;
@attr lastName;
@attr('string') nickname;
@attr('email') email;
@attr('enum') role;
@attr('number') age;
}
const { owner } = this;
owner.register('model:user', User);
const store = owner.lookup('service:store');

const schema = store.modelFor('user');
const expected = [
['nickname', 'string'],
['email', 'email'],
['role', 'enum'],
['age', 'number'],
];
const actual = [];
schema.eachTransformedAttribute(function (name, type) {
actual.push([name, type]);
});
assert.strictEqual(actual.length, expected.length, 'we have the correct number of attributes');
assert.deepEqual(actual, expected, 'We have the correct attributes');
});

test('Schema.eachTransformedAttribute works as expected on Shim Model', async function (assert) {
class User extends Model {
@attr firstName;
@attr lastName;
@attr('string') nickname;
@attr('email') email;
@attr('enum') role;
@attr('number') age;
}
const { owner } = this;
owner.register('model:user', User);
const store = owner.lookup('service:store');

store._forceShim = true;
const schema = store.modelFor('user');
store._forceShim = false;
const expected = [
['nickname', 'string'],
['email', 'email'],
['role', 'enum'],
['age', 'number'],
];
const actual = [];
schema.eachTransformedAttribute(function (name, type) {
actual.push([name, type]);
});
assert.strictEqual(actual.length, expected.length, 'we have the correct number of attributes');
assert.deepEqual(actual, expected, 'We have the correct attributes');
});
});