Skip to content

Commit

Permalink
Merge pull request #3875 from pangratz/fix-serialize-embedded-polymor…
Browse files Browse the repository at this point in the history
…phic-belongsTo

[BUGFIX beta] serialize type for embedded, polymorphic belongsTo
  • Loading branch information
bmac committed Nov 11, 2015
2 parents bf7a759 + c476207 commit d4a5bf1
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export default Ember.Mixin.create({
json[key] = null;
} else {
json[key] = embeddedSnapshot.id;

if (relationship.options.polymorphic) {
this.serializePolymorphicType(snapshot, json, relationship);
}
}
} else if (includeRecords) {
this._serializeEmbeddedBelongsTo(snapshot, json, relationship);
Expand All @@ -216,6 +220,10 @@ export default Ember.Mixin.create({
} else {
json[serializedKey] = embeddedSnapshot.record.serialize({ includeId: true });
this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, json[serializedKey]);

if (relationship.options.polymorphic) {
this.serializePolymorphicType(snapshot, json, relationship);
}
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,57 @@ test("serialize with embedded object (belongsTo relationship)", function() {
});
});

test("serialize with embedded object (polymorphic belongsTo relationship)", function() {
env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { embedded: 'always' }
}
}));

SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

var json, tom;
run(function() {
tom = env.store.createRecord(
'super-villain',
{
id: "1",
firstName: "Tom",
lastName: "Dale",
secretLab: env.store.createRecord('bat-cave', {
id: "101",
minionCapacity: 5000,
vicinity: "California, USA",
infiltrated: true
}),
homePlanet: env.store.createRecord('home-planet', {
id: "123",
name: "Villain League"
})
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLabType: 'batCave',
secretLab: {
id: get(tom, "secretLab").get("id"),
minionCapacity: get(tom, "secretLab").get("minionCapacity"),
vicinity: get(tom, "secretLab").get("vicinity"),
infiltrated: true
}
});
});

test("serialize with embedded object (belongsTo relationship) works with different primaryKeys", function() {
env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
primaryKey: '_id',
Expand Down Expand Up @@ -1236,6 +1287,111 @@ test("serialize with embedded object (belongsTo relationship, new no id)", funct
});
});

test("serialize with embedded object (polymorphic belongsTo relationship) supports serialize:ids", function() {
SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'ids' }
}
}));

var tom, json;
run(function() {
tom = env.store.createRecord(
'super-villain',
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord('bat-cave', { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord('home-planet', { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLab: get(tom, "secretLab").get("id"),
secretLabType: 'batCave'
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:id", function() {
SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'id' }
}
}));

var tom, json;
run(function() {
tom = env.store.createRecord(
'super-villain',
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord('bat-cave', { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord('home-planet', { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLab: get(tom, "secretLab").get("id"),
secretLabType: 'batCave'
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:id in conjunction with deserialize:records", function() {
SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'id', deserialize: 'records' }
}
}));

var tom, json;
run(function() {
tom = env.store.createRecord(
'super-villain',
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord('bat-cave', { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord('home-planet', { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLab: get(tom, "secretLab").get("id"),
secretLabType: 'batCave'
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:ids", function() {
env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
Expand Down

0 comments on commit d4a5bf1

Please sign in to comment.