Skip to content

Commit

Permalink
remove isPrimaryType’s reliance on modelName (#4832)
Browse files Browse the repository at this point in the history
* when interacting with a user-provided modelName, always be sure to normalize it.
  • Loading branch information
stefanpenner authored Mar 1, 2017
1 parent a6f14ec commit 3bd0c5d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,7 @@ Store = Service.extend({

_hasModelFor(modelName) {
let owner = getOwner(this);
modelName = normalizeModelName(modelName);

if (owner.factoryFor) {
return !!owner.factoryFor(`model:${modelName}`);
Expand Down
5 changes: 2 additions & 3 deletions addon/serializers/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ let RESTSerializer = JSONSerializer.extend({
included: []
};

let modelClass = store._modelFor(modelName);
let modelClass = store.modelFor(modelName);
let serializer = store.serializerFor(modelName);

Ember.makeArray(arrayHash).forEach((hash) => {
Expand Down Expand Up @@ -373,8 +373,7 @@ let RESTSerializer = JSONSerializer.extend({
},

isPrimaryType(store, typeName, primaryTypeClass) {
let typeClass = store.modelFor(typeName);
return typeClass.modelName === primaryTypeClass.modelName;
return store.modelFor(typeName) === primaryTypeClass;
},

/**
Expand Down
15 changes: 11 additions & 4 deletions tests/integration/serializers/rest-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,17 @@ test("normalizeResponse with custom modelNameFromPayloadKey", function(assert) {
};

var jsonHash = {
home_planets: [{ id: "1", name: "Umber", superVillains: [1] }],
super_villains: [{ id: "1", firstName: "Tom", lastName: "Dale", homePlanet: "1" }]
home_planets: [{
id: "1",
name: "Umber",
superVillains: [1]
}],
super_villains: [{
id: "1",
firstName: "Tom",
lastName: "Dale",
homePlanet: "1"
}]
};
var array;

Expand Down Expand Up @@ -277,7 +286,6 @@ test("serialize polymorphicType", function(assert) {
});

test("serialize polymorphicType with decamelized modelName", function(assert) {
YellowMinion.modelName = 'yellow-minion';
var tom, ray;
run(function() {
tom = env.store.createRecord('yellow-minion', { name: "Alex", id: "124" });
Expand Down Expand Up @@ -529,7 +537,6 @@ test("serializeIntoHash", function(assert) {
});

test("serializeIntoHash with decamelized modelName", function(assert) {
HomePlanet.modelName = 'home-planet';
run(function() {
league = env.store.createRecord('home-planet', { name: "Umber", id: "123" });
});
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/store/has-model-for-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {createStore} from 'dummy/tests/helpers/store';
import {module, test} from 'qunit';
import DS from 'ember-data';

let store;

module('unit/store/has-model-For', {
beforeEach() {
store = createStore({
adapter: DS.Adapter.extend(),
'one-foo': DS.Model.extend({}),
'two-foo': DS.Model.extend({})
});
}
});

test(`hasModelFor correctly normalizes`, function(assert) {
assert.equal(store._hasModelFor('oneFoo'), true);
assert.equal(store._hasModelFor('twoFoo'). true);
});

0 comments on commit 3bd0c5d

Please sign in to comment.