Skip to content

Commit

Permalink
Serializer rework
Browse files Browse the repository at this point in the history
  • Loading branch information
samselikoff committed Sep 13, 2016
1 parent f961fc9 commit e7e041f
Show file tree
Hide file tree
Showing 36 changed files with 1,458 additions and 1,657 deletions.
2 changes: 1 addition & 1 deletion addon/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function assert(bool, text) {
}

if (!bool) {
throw new MirageError(text || 'Assertion failed');
throw new MirageError(text.replace(/^ +/gm, '') || 'Assertion failed');
}
}

Expand Down
6 changes: 3 additions & 3 deletions addon/orm/associations/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ class BelongsTo extends Association {

/*
object.createParent
- creates an associated parent, persists directly to db,
and updates the owner's foreign key
- creates a new saved associated parent, and immediately persists both models
*/
modelPrototype[`create${capitalize(key)}`] = function(attrs) {
let parent = schema[toCollectionName(association.modelName)].create(attrs);

this[foreignKey] = parent.id;
this[key] = parent;
this.save();

return parent;
};
Expand Down
14 changes: 12 additions & 2 deletions addon/orm/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class Schema {
ModelClass.prototype.associationKeys = []; // ex: address.user, user.addresses
ModelClass.prototype.associationIdKeys = []; // ex: address.user_id, user.address_ids. may or may not be a fk.

let fksAddedFromThisModel = {};
for (let associationProperty in ModelClass.prototype) {
if (ModelClass.prototype[associationProperty] instanceof Association) {
let association = ModelClass.prototype[associationProperty];
Expand All @@ -64,8 +65,17 @@ export default class Schema {

// Update the registry with this association's foreign keys. This is
// essentially our "db migration", since we must know about the fks.
let result = association.getForeignKeyArray();
let [ fkHolder, fk ] = result;
let [ fkHolder, fk ] = association.getForeignKeyArray();

fksAddedFromThisModel[fkHolder] = fksAddedFromThisModel[fkHolder] || [];
assert(
!_includes(fksAddedFromThisModel[fkHolder], fk),
`Your '${type}' model definition has multiple possible inverse relationships of type '${fkHolder}'.
Please read the associations guide and specify explicit inverses: http://www.ember-cli-mirage.com/docs/v0.2.x/models/#associations`
);
fksAddedFromThisModel[fkHolder].push(fk);

this._addForeignKeyToRegistry(fkHolder, fk);

// Augment the Model's class with any methods added by this association
Expand Down
9 changes: 7 additions & 2 deletions addon/route-handlers/shorthands/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export default class BaseShorthandRouteHandler extends BaseRouteHandler {
}
}

handleStringShorthand() { }
handleArrayShorthand() { }
// handleStringShorthand() {
//
// }
//
// handleArrayShorthand() {
//
// }

}
10 changes: 6 additions & 4 deletions addon/serializer-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default class SerializerRegistry {
}

serialize(response, request) {
this.request = request;

if (this._isModelOrCollection(response)) {
let serializer = this.serializerFor(response.modelName);

Expand All @@ -29,9 +31,9 @@ export default class SerializerRegistry {
let serializer = this.serializerFor(collection.modelName);

if (serializer.embed) {
json[pluralize(collection.modelName)] = serializer._serializeModelOrCollection(collection, request);
json[pluralize(collection.modelName)] = serializer.serialize(collection, request);
} else {
json = _assign(json, serializer._serializeSideloadedModelOrCollection(collection, request));
json = _assign(json, serializer.serialize(collection, request));
}

return json;
Expand All @@ -42,7 +44,7 @@ export default class SerializerRegistry {
}
}

serializerFor(type, { explicit = false, included = [], alreadySerialized = {} } = {}) {
serializerFor(type, { explicit = false } = {}) {
let SerializerForResponse = this._serializerMap && (this._serializerMap[camelize(type)]);

if (explicit) {
Expand All @@ -59,7 +61,7 @@ export default class SerializerRegistry {
);
}

return new SerializerForResponse(this, type, included, alreadySerialized);
return new SerializerForResponse(this, type, this.request);
}

_isModel(object) {
Expand Down
Loading

0 comments on commit e7e041f

Please sign in to comment.