Skip to content

Commit

Permalink
fix(serializer): resolve serializer return string via ioc container
Browse files Browse the repository at this point in the history
serializer can be a class or a string to be resolved via IoC container

fix #268
  • Loading branch information
thetutlage committed Dec 23, 2017
1 parent d6fa6aa commit 484a6c1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
19 changes: 18 additions & 1 deletion src/Lucid/Model/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const _ = require('lodash')
const moment = require('moment')
const VanillaSerializer = require('../Serializers/Vanilla')
const { ioc } = require('../../../lib/iocResolver')
const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss'

/**
Expand Down Expand Up @@ -120,6 +121,21 @@ class BaseModel {
return moment(value).format(DATE_FORMAT)
}

/**
* Resolves the serializer for the current model.
*
* If serializer is a string, then it is resolved using
* the Ioc container, otherwise it is assumed that
* a `class` is returned.
*
* @method resolveSerializer
*
* @returns {Class}
*/
static resolveSerializer () {
return typeof (this.Serializer) === 'string' ? ioc.use(this.Serializer) : this.Serializer
}

/**
* This method is executed when toJSON is called on a
* model or collection of models. The value received
Expand Down Expand Up @@ -264,7 +280,8 @@ class BaseModel {
* @return {Object}
*/
toJSON () {
return new this.constructor.Serializer(this, null, true).toJSON()
const Serializer = this.constructor.resolveSerializer()
return new Serializer(this, null, true).toJSON()
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Lucid/QueryBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,8 @@ class QueryBuilder {
await this.Model.$hooks.after.exec('fetch', modelInstances)
}

/**
* Return an instance of active model serializer
*/
return new this.Model.Serializer(modelInstances)
const Serializer = this.Model.resolveSerializer()
return new Serializer(modelInstances)
}

/**
Expand Down Expand Up @@ -490,7 +488,8 @@ class QueryBuilder {
/**
* Return an instance of active model serializer
*/
return new this.Model.Serializer(modelInstances, pages)
const Serializer = this.Model.resolveSerializer()
return new Serializer(modelInstances, pages)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Lucid/Relations/BelongsToMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ class BelongsToMany extends BaseRelation {
* @return {Object} @multiple([key=String, values=Array, defaultValue=Null])
*/
group (relatedInstances) {
const Serializer = this.RelatedModel.Serializer
const Serializer = this.RelatedModel.resolveSerializer()

const transformedValues = _.transform(relatedInstances, (result, relatedInstance) => {
const foreignKeyValue = relatedInstance.$sideLoaded[`pivot_${this.foreignKey}`]
Expand Down
2 changes: 1 addition & 1 deletion src/Lucid/Relations/HasMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class HasMany extends BaseRelation {
* @return {Object} @multiple([key=String, values=Array, defaultValue=Null])
*/
group (relatedInstances) {
const Serializer = this.RelatedModel.Serializer
const Serializer = this.RelatedModel.resolveSerializer()

const transformedValues = _.transform(relatedInstances, (result, relatedInstance) => {
const foreignKeyValue = relatedInstance[this.foreignKey]
Expand Down
2 changes: 1 addition & 1 deletion src/Lucid/Relations/HasManyThrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class HasManyThrough extends BaseRelation {
* @return {Object} @multiple([key=String, values=Array, defaultValue=Null])
*/
group (relatedInstances) {
const Serializer = this.RelatedModel.Serializer
const Serializer = this.RelatedModel.resolveSerializer()

const transformedValues = _.transform(relatedInstances, (result, relatedInstance) => {
const foreignKeyValue = relatedInstance.$sideLoaded[`through_${this.foreignKey}`]
Expand Down

0 comments on commit 484a6c1

Please sign in to comment.