Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow rails conventions for serializing embedded objects #2138

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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