Skip to content

Commit

Permalink
Backport modelName fix from #5406 to 3.1 (#5601)
Browse files Browse the repository at this point in the history
* [BUGFIX release] Fix `Model.modelName` inheritance with Ember 3.2+.

On Ember 3.2 and higher, Ember uses "real" inheritance when an
`Ember.Object` is `.extend()`ed. This means that in 3.2 and higher the
`ModelClassHere.modelName` property is now being inherited (where
previously each `.extend()` reset the value to `null`).

The fix here ensures that each `ModelClass.extend()` gets its own
`modelName`...

* [BUGFIX release] Fix toString test to work with Ember 3.2+.

This test was attempting to ensure that the records guid was included in
the `record.toString()` output, but changes in Ember 3.2+ changed the
default output of the class / constructor name portion (therefore
failing the assertion in a useless way).

This updates the test to add a custom `toString` to the model class so
that we can be decoupled from how Ember `.toString()`'s classes when
there is no custom `.toString` setup (because this is actually fairly
unstable version over version).

* fix branch
  • Loading branch information
runspired authored Aug 28, 2018
1 parent 9466a51 commit 6740407
Show file tree
Hide file tree
Showing 3 changed files with 2,700 additions and 1,654 deletions.
5 changes: 4 additions & 1 deletion addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,10 @@ Store = Service.extend({
assert(`'${inspect(klass)}' does not appear to be an ember-data model`, klass.isModel);

// TODO: deprecate this
klass.modelName = klass.modelName || modelName;
let hasOwnModelNameSet = klass.modelName && klass.hasOwnProperty('modelName');
if (!hasOwnModelNameSet) {
klass.modelName = modelName;
}

this._modelFactoryCache[modelName] = factory;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module('unit/model - DS.Model', {
name: DS.attr('string'),
isDrugAddict: DS.attr('boolean')
});
Person.toString = () => 'person';

env = setupStore({
person: Person
Expand Down Expand Up @@ -269,7 +270,7 @@ test("a record's id is included in its toString representation", function(assert
});

return store.findRecord('person', 1).then(record => {
assert.equal(record.toString(), `<(subclass of DS.Model):${guidFor(record)}:1>`, 'reports id in toString');
assert.equal(record.toString(), `<person:${guidFor(record)}:1>`, 'reports id in toString');
});
});
});
Expand Down
Loading

0 comments on commit 6740407

Please sign in to comment.