Skip to content

Commit

Permalink
fix(deserializer): unable to create polymorphic relationship digitals…
Browse files Browse the repository at this point in the history
  • Loading branch information
grahambates committed Jan 16, 2019
1 parent 40f9282 commit 94c439d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/deserializer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var _ = require('lodash')
var utils = require('./utils')

function defaultBeforeDeserialize (options, cb) {
cb(null, options)
Expand Down Expand Up @@ -79,6 +80,20 @@ function belongsToRelationships (options) {
options.result[fkName] = null
} else {
options.result[fkName] = relationship.data.id
if (serverRelation.polymorphic) {
// Find the model which has a plural matching 'data.type'
// Allow case insensitive match
var relatedType = relationship.data.type.toLowerCase()
var modelName = _.findKey(model.app.models, function (model) {
var plural = utils.pluralForModel(model)
return plural.toLowerCase() === relatedType
})
if (!modelName) {
return false
}
var discriminator = serverRelation.polymorphic.discriminator
options.result[discriminator] = modelName
}
}
})
}

0 comments on commit 94c439d

Please sign in to comment.