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

Custom primaryKey for embedded polymorphic relations work #3138

Merged
merged 1 commit into from
Jun 1, 2015
Merged
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
7 changes: 2 additions & 5 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var get = Ember.get;
var forEach = Ember.EnumerableUtils.forEach;
var camelize = Ember.String.camelize;

Expand Down Expand Up @@ -442,11 +441,10 @@ function extractEmbeddedHasManyPolymorphic(store, key, hash) {
var modelName = data.type;
var embeddedSerializer = store.serializerFor(modelName);
var embeddedTypeClass = store.modelFor(modelName);
var primaryKey = get(embeddedSerializer, 'primaryKey');

var embeddedRecord = embeddedSerializer.normalize(embeddedTypeClass, data, null);
store.push(embeddedTypeClass, embeddedRecord);
ids.push({ id: embeddedRecord[primaryKey], type: modelName });
ids.push({ id: embeddedRecord.id, type: modelName });
});

hash[key] = ids;
Expand Down Expand Up @@ -476,12 +474,11 @@ function extractEmbeddedBelongsToPolymorphic(store, key, hash) {
var modelName = data.type;
var embeddedSerializer = store.serializerFor(modelName);
var embeddedTypeClass = store.modelFor(modelName);
var primaryKey = get(embeddedSerializer, 'primaryKey');

var embeddedRecord = embeddedSerializer.normalize(embeddedTypeClass, data, null);
store.push(embeddedTypeClass, embeddedRecord);

hash[key] = embeddedRecord[primaryKey];
hash[key] = embeddedRecord.id;
hash[key + 'Type'] = modelName;
return hash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,63 @@ test("extractSingle with polymorphic hasMany", function() {

});

test("extractSingle with polymorphic hasMany and custom primary key", function() {
SuperVillain.reopen({
secretWeapons: DS.hasMany("secretWeapon", { polymorphic: true })
});

env.registry.register('adapter:super-villain', DS.ActiveModelAdapter);
env.registry.register('serializer:light-saber', DS.ActiveModelSerializer.extend({
primaryKey: 'custom'
}));
env.registry.register('serializer:super-villain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretWeapons: { embedded: 'always' }
}
}));
var serializer = env.container.lookup("serializer:super-villain");

var json_hash = {
super_villain: {
id: "1",
first_name: "Tom",
last_name: "Dale",
secret_weapons: [
{
custom: "1",
type: "LightSaber",
name: "Tom's LightSaber",
color: "Red"
},
{
id: "1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be custom?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😯 – The primaryKey is only defined for the serializer:light-saber. If both embedded records should have custom as primaryKey, it would/could be defined in serializer:secret-weapon.

Am I missing something? 😔 💭

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could add a comment since I guess this would make reading the test easier ... c/d?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NM, I scrwed up while reading

type: "SecretWeapon",
name: "The Death Star"
}
]
}
};
var json;

run(function() {
json = serializer.extractSingle(env.store, SuperVillain, json_hash);
});

deepEqual(json, {
id: "1",
firstName: "Tom",
lastName: "Dale",
secretWeapons: [
{ id: "1", type: "light-saber" },
{ id: "1", type: "secret-weapon" }
]
}, "Custom primary key of embedded hasMany is correctly normalized");

equal(env.store.recordForId("lightSaber", "1").get("name"), "Tom's LightSaber", "Embedded polymorphic LightSaber with custom primary key is found");
equal(env.store.recordForId("secretWeapon", "1").get("name"), "The Death Star", "Embedded polymorphic SecretWeapon found");

});

test("extractSingle with polymorphic belongsTo", function() {
expect(2);

Expand Down Expand Up @@ -1211,6 +1268,55 @@ test("extractSingle with polymorphic belongsTo", function() {

});

test("extractSingle with polymorphic belongsTo and custom primary key", function() {
expect(2);

SuperVillain.reopen({
secretLab: DS.belongsTo("secretLab", { polymorphic: true })
});

env.registry.register('adapter:super-villain', DS.ActiveModelAdapter);
env.registry.register('serializer:super-villain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { embedded: 'always' }
}
}));
env.registry.register('serializer:bat-cave', DS.ActiveModelSerializer.extend({
primaryKey: 'custom'
}));
var serializer = env.container.lookup("serializer:super-villain");

var json_hash = {
super_villain: {
id: "1",
first_name: "Tom",
last_name: "Dale",
secret_lab: {
custom: "1",
type: "bat-cave",
infiltrated: true
}
}
};

var json;

run(function() {
json = serializer.extractSingle(env.store, SuperVillain, json_hash);
});

deepEqual(json, {
id: "1",
firstName: "Tom",
lastName: "Dale",
secretLab: "1",
secretLabType: "bat-cave"
}, "Custom primary key is correctly normalized");

equal(env.store.recordForId("batCave", "1").get("infiltrated"), true, "Embedded polymorphic BatCave with custom primary key is found");

});

test("Mixin can be used with RESTSerializer which does not define keyForAttribute", function() {
run(function() {
homePlanet = env.store.createRecord(HomePlanet, { name: "Villain League", id: "123" });
Expand Down