Skip to content

Commit

Permalink
fixing json api data type field for polymorphic models
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspaniel committed Aug 22, 2016
1 parent 0d6b4d8 commit b015ef2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 4 additions & 8 deletions addon/converter/jsonapi-fixture-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JSONAPIFixtureConverter extends Converter {

constructor(store, options = { transformKeys: true, serializeMode: false }) {
super(store, options);
this.typeTransformFn = this.serializeMode ? this.typeTransformViaSerializer : this.noTransformFn;
this.typeTransformFn = this.serializeMode ? this.typeTransformViaSerializer : dasherize;
this.defaultKeyTransformFn = dasherize;
this.polymorphicTypeTransformFn = dasherize;
this.included = [];
Expand Down Expand Up @@ -58,13 +58,9 @@ class JSONAPIFixtureConverter extends Converter {
@param {Object or DS.Model instance} record
@param {Object} relationship
*/
normalizeAssociation(record, relationship) {
normalizeAssociation(record) {
if (Ember.typeOf(record) === 'object') {
if (relationship.options.polymorphic) {
return { type: this.typeTransformFn(dasherize(record.type)), id: record.id };
} else {
return { type: this.typeTransformFn(record.type), id: record.id };
}
return { type: this.typeTransformFn(record.type), id: record.id };
} else {
return { type: this.typeTransformFn(record.constructor.modelName), id: record.id };
}
Expand All @@ -86,7 +82,7 @@ class JSONAPIFixtureConverter extends Converter {
*/
convertSingle(modelName, fixture) {
let data = {
type: this.typeTransformFn(modelName),
type: this.typeTransformFn(fixture.type || modelName),
attributes: this.extractAttributes(modelName, fixture),
};

Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/tests/factories/hat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import FactoryGuy from 'ember-data-factory-guy';

FactoryGuy.define('hat', {

traits: {
big: {type: "SmallHat"},
small: {type: "BigHat"},
round: {shape: "round"},
square: {shape: "square"},
with_user: { user: {} },
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/jsonapi-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ test("returns a relationship with an index and key", function() {
deepEqual(user.get(1).company, { id: 2, type: 'company' });
});

module(title(adapter, 'FactoryGuy#buildList custom'), inlineSetup(App, serializerType));

test("mock returns inherited models with proper types", function(assert) {
let list = buildList('hat', 'big', 'small');
let data = list.data;
equal(data[0].type, 'small-hat');
equal(data[1].type, 'big-hat');
});

module(title(adapter, 'FactoryGuy#build custom'), inlineSetup(App, serializerType));

test("with traits defining model attributes", function() {
Expand Down

0 comments on commit b015ef2

Please sign in to comment.