Skip to content

Commit

Permalink
fix: Schema.eachTransformedAttribute API should match Model.eachTrans…
Browse files Browse the repository at this point in the history
…formedAttribute (#8346)
  • Loading branch information
runspired authored Dec 8, 2022
1 parent 52b66f5 commit 6f90282
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
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');
});
});

0 comments on commit 6f90282

Please sign in to comment.