Skip to content

Commit

Permalink
embedded records mixin should use the correct serialization key when …
Browse files Browse the repository at this point in the history
…deserialize configuration is set

When extending the `DS.EmbeddedRecordsMixin`, if you configure a relationship as `{ serialize: 'id', deserialize: 'records' }`, it won't actually pay attention to the `serialize: 'id'` option, but use the `deserialize: 'records'` option.
  • Loading branch information
agrobbin committed Mar 1, 2015
1 parent b8aff09 commit 5644d84
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/ember-data/lib/serializers/embedded_records_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
return extractEmbeddedRecords(this, this.store, type, normalizedHash);
},

keyForRelationship: function(key, type) {
if (this.hasDeserializeRecordsOption(key)) {
keyForRelationship: function(key, type, method) {
if (method === undefined) {
method = 'deserialize';
}

if ((method === 'serialize' && this.hasSerializeRecordsOption(key)) || (method === 'deserialize' && this.hasDeserializeRecordsOption(key))) {
return this.keyForAttribute(key);
} else {
return this._super(key, type) || key;
Expand Down Expand Up @@ -191,7 +195,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
var embeddedSnapshot = snapshot.belongsTo(attr);
var key;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind);
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
if (!embeddedSnapshot) {
json[key] = null;
} else {
Expand Down Expand Up @@ -299,7 +303,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
var includeRecords = this.hasSerializeRecordsOption(attr);
var key;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind);
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
json[key] = snapshot.hasMany(attr, { ids: true });
} else if (includeRecords) {
key = this.keyForAttribute(attr);
Expand Down Expand Up @@ -335,7 +339,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
if (parentRecord) {
var name = parentRecord.name;
var embeddedSerializer = this.store.serializerFor(embeddedSnapshot.type);
var parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind);
var parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind, 'deserialize');
if (parentKey) {
delete json[parentKey];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,41 @@ test("serialize with embedded object (belongsTo relationship) supports serialize
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:id in conjunction with deserialize:records", function() {
env.registry.register('adapter:superVillain', DS.ActiveModelAdapter);
env.registry.register('serializer:superVillain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'id', deserialize: 'records' }
}
}));

var serializer = env.container.lookup("serializer:superVillain");

// records with an id, persisted
var tom, json;

run(function() {
tom = env.store.createRecord(
SuperVillain,
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord(SecretLab, { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord(HomePlanet, { name: "Villain League", id: "123" })
}
);
});

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

deepEqual(json, {
first_name: get(tom, "firstName"),
last_name: get(tom, "lastName"),
home_planet_id: get(tom, "homePlanet").get("id"),
secret_lab_id: get(tom, "secretLab").get("id")
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:false", function() {
env.registry.register('adapter:superVillain', DS.ActiveModelAdapter);
env.registry.register('serializer:superVillain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
Expand Down

0 comments on commit 5644d84

Please sign in to comment.