Skip to content

Commit

Permalink
AMS: payload key should be type, not attr name
Browse files Browse the repository at this point in the history
Fixes embedded object extraction when the attribute name is not
the plural form of the relationship's target type.
  • Loading branch information
tstirrat committed Sep 24, 2013
1 parent 7991107 commit 246bd1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,33 @@ function updatePayloadWithEmbedded(store, serializer, type, partial, payload) {
}

type.eachRelationship(function(key, relationship) {
var payloadKey, attribute, ids,
attr = attrs[key],
var expandedKey, embeddedTypeKey, attribute, ids,
config = attrs[key],
serializer = store.serializerFor(relationship.type.typeKey),
primaryKey = get(serializer, "primaryKey");

if (relationship.kind !== "hasMany") {
return;
}

if (attr && (attr.embedded === 'always' || attr.embedded === 'load')) {
payloadKey = this.keyForRelationship(key, relationship.kind);
if (config && (config.embedded === 'always' || config.embedded === 'load')) {
embeddedTypeKey = Ember.String.pluralize(relationship.type.typeKey);
expandedKey = this.keyForRelationship(key, relationship.kind);
attribute = this.keyForAttribute(key);
ids = [];

if (!partial[attribute]) {
return;
}

payload[attribute] = payload[attribute] || [];
payload[embeddedTypeKey] = payload[embeddedTypeKey] || [];

forEach(partial[attribute], function(data) {
ids.push(data[primaryKey]);
payload[attribute].push(data);
payload[embeddedTypeKey].push(data);
});

partial[payloadKey] = ids;
partial[expandedKey] = ids;
delete partial[attribute];
}
}, serializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module("integration/active_model - ActiveModelSerializer", {
});
HomePlanet = DS.Model.extend({
name: DS.attr('string'),
superVillains: DS.hasMany('superVillain')
villains: DS.hasMany('superVillain')
});
EvilMinion = DS.Model.extend({
superVillain: DS.belongsTo('superVillain'),
Expand Down Expand Up @@ -94,7 +94,7 @@ test("extractSingle", function() {
env.container.register('adapter:superVillain', DS.ActiveModelAdapter);

var json_hash = {
home_planet: {id: "1", name: "Umber", super_villain_ids: [1]},
home_planet: {id: "1", name: "Umber", villain_ids: [1]},
super_villains: [{id: "1", first_name: "Tom", last_name: "Dale", home_planet_id: "1"}]
};

Expand All @@ -103,7 +103,7 @@ test("extractSingle", function() {
deepEqual(json, {
"id": "1",
"name": "Umber",
"superVillains": [1]
"villains": [1]
});

env.store.find("superVillain", 1).then(async(function(minion){
Expand All @@ -115,7 +115,7 @@ test("extractSingle with embedded objects", function() {
env.container.register('adapter:superVillain', DS.ActiveModelAdapter);
env.container.register('serializer:homePlanet', DS.ActiveModelSerializer.extend({
attrs: {
superVillains: {embedded: 'always'}
villains: {embedded: 'always'}
}
}));

Expand All @@ -124,7 +124,7 @@ test("extractSingle with embedded objects", function() {
home_planet: {
id: "1",
name: "Umber",
super_villains: [{
villains: [{
id: "1",
first_name: "Tom",
last_name: "Dale"
Expand All @@ -136,7 +136,7 @@ test("extractSingle with embedded objects", function() {
deepEqual(json, {
id: "1",
name: "Umber",
superVillains: ["1"]
villains: ["1"]
});
env.store.find("superVillain", 1).then(async(function(minion) {
equal(minion.get('firstName'), "Tom");
Expand All @@ -147,7 +147,7 @@ test("extractArray", function() {
env.container.register('adapter:superVillain', DS.ActiveModelAdapter);

var json_hash = {
home_planets: [{id: "1", name: "Umber", super_villain_ids: [1]}],
home_planets: [{id: "1", name: "Umber", villain_ids: [1]}],
super_villains: [{id: "1", first_name: "Tom", last_name: "Dale", home_planet_id: "1"}]
};

Expand All @@ -156,20 +156,19 @@ test("extractArray", function() {
deepEqual(array, [{
"id": "1",
"name": "Umber",
"superVillains": [1]
"villains": [1]
}]);

env.store.find("superVillain", 1).then(async(function(minion){
equal(minion.get('firstName'), "Tom");
}));
});

// TODO
test("extractArray with embedded objects", function() {
env.container.register('adapter:superVillain', DS.ActiveModelAdapter);
env.container.register('serializer:homePlanet', DS.ActiveModelSerializer.extend({
attrs: {
superVillains: {embedded: 'always'}
villains: {embedded: 'always'}
}
}));

Expand All @@ -179,7 +178,7 @@ test("extractArray with embedded objects", function() {
home_planets: [{
id: "1",
name: "Umber",
super_villains: [{
villains: [{
id: "1",
first_name: "Tom",
last_name: "Dale"
Expand All @@ -192,7 +191,7 @@ test("extractArray with embedded objects", function() {
deepEqual(array, [{
id: "1",
name: "Umber",
superVillains: ["1"]
villains: ["1"]
}]);

env.store.find("superVillain", 1).then(async(function(minion){
Expand All @@ -219,7 +218,7 @@ test("serialize with embedded objects", function() {

env.container.register('serializer:homePlanet', DS.ActiveModelSerializer.extend({
attrs: {
superVillains: {embedded: 'always'}
villains: {embedded: 'always'}
}
}));
var serializer = env.container.lookup("serializer:homePlanet");
Expand All @@ -228,7 +227,7 @@ test("serialize with embedded objects", function() {

deepEqual(json, {
name: "Villain League",
super_villains: [{
villains: [{
id: get(tom, "id"),
firstName: "Tom",
lastName: "Dale",
Expand Down

0 comments on commit 246bd1c

Please sign in to comment.