Skip to content

Commit

Permalink
Make ActiveModelSerializer work with the new Serializer API
Browse files Browse the repository at this point in the history
  • Loading branch information
wecc committed Jun 5, 2015
1 parent 8963c4a commit 009fbda
Show file tree
Hide file tree
Showing 3 changed files with 600 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/activemodel-adapter/lib/system/active-model-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ var ActiveModelSerializer = RESTSerializer.extend({
}
},

/**
`keyForLink` can be used to define a custom key when deserializing link
properties. The `ActiveModelSerializer` camelizes link keys by default.
@method keyForLink
@param {String} key
@param {String} kind `belongsTo` or `hasMany`
@return {String} normalized key
*/
keyForLink: function(key, relationshipKind) {
return camelize(key);
},

/*
Does not serialize hasMany relationships by default.
*/
Expand Down Expand Up @@ -205,10 +218,13 @@ var ActiveModelSerializer = RESTSerializer.extend({
@param {String} prop
@return Object
*/

normalize: function(typeClass, hash, prop) {
this.normalizeLinks(hash);
if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) {
_newNormalize.apply(this, arguments);
return this._super(...arguments);
}

this.normalizeLinks(hash);
return this._super(typeClass, hash, prop);
},

Expand Down Expand Up @@ -296,3 +312,14 @@ var ActiveModelSerializer = RESTSerializer.extend({
});

export default ActiveModelSerializer;

/**
@method _newNormalize
@param {DS.Model} modelClass
@param {Object} hash
@return {Object}
@private
*/
function _newNormalize(modelClass, hash, prop) {
this.normalizeLinks(hash);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
var SuperVillain, EvilMinion, YellowMinion, DoomsdayDevice, MediocreVillain, TestSerializer, env;
var run = Ember.run;

module("integration/active_model - AMS-namespaced-model-names (new API)", {
setup: function() {
SuperVillain = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
evilMinions: DS.hasMany("evilMinion")
});

EvilMinion = DS.Model.extend({
superVillain: DS.belongsTo('superVillain'),
name: DS.attr('string')
});
YellowMinion = EvilMinion.extend();
DoomsdayDevice = DS.Model.extend({
name: DS.attr('string'),
evilMinion: DS.belongsTo('evilMinion', { polymorphic: true })
});
MediocreVillain = DS.Model.extend({
name: DS.attr('string'),
evilMinions: DS.hasMany('evilMinion', { polymorphic: true })
});
TestSerializer = DS.ActiveModelSerializer.extend({
isNewSerializerAPI: true
});
env = setupStore({
superVillain: SuperVillain,
evilMinion: EvilMinion,
'evilMinions/yellowMinion': YellowMinion,
doomsdayDevice: DoomsdayDevice,
mediocreVillain: MediocreVillain
});
env.store.modelFor('superVillain');
env.store.modelFor('evilMinion');
env.store.modelFor('evilMinions/yellowMinion');
env.store.modelFor('doomsdayDevice');
env.store.modelFor('mediocreVillain');
env.registry.register('serializer:application', TestSerializer);
env.registry.register('serializer:-active-model', TestSerializer);
env.registry.register('adapter:-active-model', TestSerializer);
env.amsSerializer = env.container.lookup("serializer:-active-model");
env.amsAdapter = env.container.lookup("adapter:-active-model");
},

teardown: function() {
run(env.store, 'destroy');
}
});

if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) {

test("serialize polymorphic", function() {
var tom, ray;
run(function() {
tom = env.store.createRecord('evilMinions/yellowMinion', { name: "Alex", id: "124" });
ray = env.store.createRecord('doomsday-device', { evilMinion: tom, name: "DeathRay" });
});

var json = env.amsSerializer.serialize(ray._createSnapshot());

deepEqual(json, {
name: "DeathRay",
evil_minion_type: "EvilMinions::YellowMinion",
evil_minion_id: "124"
});
});

test("serialize polymorphic when type key is not camelized", function() {
YellowMinion.modelName = 'evil-minions/yellow-minion';
var tom, ray;
run(function() {
tom = env.store.createRecord('evil-minions/yellow-minion', { name: "Alex", id: "124" });
ray = env.store.createRecord('doomsday-device', { evilMinion: tom, name: "DeathRay" });
});

var json = env.amsSerializer.serialize(ray._createSnapshot());

deepEqual(json["evil_minion_type"], "EvilMinions::YellowMinion");
});

test("extractPolymorphic hasMany", function() {
var json_hash = {
mediocre_villain: { id: 1, name: "Dr Horrible", evil_minion_ids: [{ type: "EvilMinions::YellowMinion", id: 12 }] },
"evil-minions/yellow-minion": [{ id: 12, name: "Alex", doomsday_device_ids: [1] }]
};
var json;

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, MediocreVillain, json_hash, '1', 'find');
});

deepEqual(json, {
"data": {
"id": "1",
"type": "mediocre-villain",
"attributes": {
"name": "Dr Horrible"
},
"relationships": {
"evilMinions": {
"data": [
{ "id": "12", "type": "evil-minions/yellow-minion" }
]
}
}
},
"included": [{
"id": "12",
"type": "evil-minions/yellow-minion",
"attributes": {
"name": "Alex"
},
"relationships": {}
}]
});
});

test("extractPolymorphic", function() {
var json_hash = {
doomsday_device: { id: 1, name: "DeathRay", evil_minion_id: { type: "EvilMinions::YellowMinion", id: 12 } },
"evil-minions/yellow-minion": [{ id: 12, name: "Alex", doomsday_device_ids: [1] }]
};
var json;

run(function() {
json = env.amsSerializer.normalizeResponse(env.store, DoomsdayDevice, json_hash, '1', 'find');
});

deepEqual(json, {
"data": {
"id": "1",
"type": "doomsday-device",
"attributes": {
"name": "DeathRay"
},
"relationships": {
"evilMinion": {
"data": { "id": "12", "type": "evil-minions/yellow-minion" }
}
}
},
"included": [{
"id": "12",
"type": "evil-minions/yellow-minion",
"attributes": {
"name": "Alex"
},
"relationships": {}
}]
});
});

}
Loading

0 comments on commit 009fbda

Please sign in to comment.