Skip to content

Commit

Permalink
Follow rails conventions for serializing embedded objects
Browse files Browse the repository at this point in the history
  • Loading branch information
opsb committed Aug 22, 2014
1 parent 4c27801 commit 63f6ad8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ var ActiveModelSerializer = RESTSerializer.extend({
return decamelize(attr);
},

formatEmbeddedKey: function(key){
return key + "_attributes";
},

/**
Underscores relationship names and appends "_id" or "_ids" when serializing
relationship keys.
Expand Down
9 changes: 7 additions & 2 deletions packages/ember-data/lib/serializers/embedded_records_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
}
},

keyForEmbeddedAttribute: function(attr){
var key = this.keyForAttribute(attr);
return this.formatEmbeddedKey ? this.formatEmbeddedKey(key) : key;
},

/**
Serialize `belongsTo` relationship when it is configured as an embedded object.
Expand Down Expand Up @@ -189,7 +194,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
json[key] = get(embeddedRecord, 'id');
}
} else if (includeRecords) {
key = this.keyForAttribute(attr);
key = this.keyForEmbeddedAttribute(attr);
if (!embeddedRecord) {
json[key] = null;
} else {
Expand Down Expand Up @@ -294,7 +299,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
key = this.keyForRelationship(attr, relationship.kind);
json[key] = get(record, attr).mapBy('id');
} else if (includeRecords) {
key = this.keyForAttribute(attr);
key = this.keyForEmbeddedAttribute(attr);
json[key] = get(record, attr).map(function(embeddedRecord) {
var serializedEmbeddedRecord = embeddedRecord.serialize({includeId: true});
this.removeEmbeddedForeignKey(record, embeddedRecord, relationship, serializedEmbeddedRecord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ test("serialize with embedded objects (hasMany relationship)", function() {

deepEqual(json, {
name: "Villain League",
villains: [{
villains_attributes: [{
id: get(tom, "id"),
first_name: "Tom",
last_name: "Dale",
Expand Down Expand Up @@ -579,7 +579,7 @@ test("serialize with (new) embedded objects (hasMany relationship)", function()
var json = serializer.serialize(league);
deepEqual(json, {
name: "Villain League",
villains: [{
villains_attributes: [{
first_name: "Tom",
last_name: "Dale",
home_planet_id: get(league, "id"),
Expand Down Expand Up @@ -608,7 +608,7 @@ test("serialize with embedded objects (hasMany relationships, including related
first_name: get(superVillain, "firstName"),
last_name: get(superVillain, "lastName"),
home_planet_id: null,
evil_minions: [{
evil_minions_attributes: [{
id: get(evilMinion, "id"),
name: get(evilMinion, "name"),
super_villain_id: "1"
Expand Down Expand Up @@ -688,7 +688,7 @@ test("serialize with embedded object (belongsTo relationship)", function() {
first_name: get(tom, "firstName"),
last_name: get(tom, "lastName"),
home_planet_id: get(tom, "homePlanet").get("id"),
secret_lab: {
secret_lab_attributes: {
id: get(tom, "secretLab").get("id"),
minion_capacity: get(tom, "secretLab").get("minionCapacity"),
vicinity: get(tom, "secretLab").get("vicinity")
Expand Down Expand Up @@ -725,7 +725,7 @@ test("serialize with embedded object (belongsTo relationship) works with differe
first_name: get(tom, "firstName"),
last_name: get(tom, "lastName"),
home_planet_id: get(tom, "homePlanet").get("id"),
secret_lab: {
secret_lab_attributes: {
crazy_id: get(tom, "secretLab").get("id"),
minion_capacity: get(tom, "secretLab").get("minionCapacity"),
vicinity: get(tom, "secretLab").get("vicinity")
Expand Down Expand Up @@ -758,7 +758,7 @@ test("serialize with embedded object (belongsTo relationship, new no id)", funct
first_name: get(tom, "firstName"),
last_name: get(tom, "lastName"),
home_planet_id: get(tom, "homePlanet").get("id"),
secret_lab: {
secret_lab_attributes: {
minion_capacity: get(tom, "secretLab").get("minionCapacity"),
vicinity: get(tom, "secretLab").get("vicinity")
}
Expand Down Expand Up @@ -894,7 +894,7 @@ test("when related record is not present, serialize embedded record (with a belo
first_name: get(tom, "firstName"),
last_name: get(tom, "lastName"),
home_planet_id: get(tom, "homePlanet").get("id"),
secret_lab: null
secret_lab_attributes: null
});
});

Expand Down

0 comments on commit 63f6ad8

Please sign in to comment.