Skip to content

Commit a23b088

Browse files
author
Lee Richmond
committed
Support multi-word relation names
1 parent 049d2d3 commit a23b088

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/util/deserialize.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import Config from '../configuration';
44
import Model from '../model';
5+
import { camelize } from './string';
56

67
export default function deserialize(resource : japiResource, payload: japiDoc) : Model {
78
let deserializer = new Deserializer(payload);
@@ -33,7 +34,8 @@ class Deserializer {
3334
let record = this.findModel(resource);
3435
if (!record) {
3536
if (isRelation) {
36-
resource = this.findResource(resource);
37+
let hydrated = this.findResource(resource);
38+
if (hydrated) resource = hydrated;
3739
}
3840
record = this._deserialize(resource);
3941
}
@@ -68,10 +70,11 @@ class Deserializer {
6870

6971
_iterateValidRelationships(instance, relationships, callback) {
7072
for (let key in relationships) {
71-
if (instance.klass.attributeList.indexOf(key) >= 0) {
73+
let relationName = camelize(key);
74+
if (instance.klass.attributeList.indexOf(relationName) >= 0) {
7275
let relationData = relationships[key].data;
7376
if(!relationData) continue; // only links, empty, etc
74-
callback(key, relationData);
77+
callback(relationName, relationData);
7578
}
7679
}
7780
}

test/fixtures.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ let Author = Person.extend({
1818
jsonapiType: 'authors'
1919
},
2020

21-
books: hasMany(),
22-
tags: hasMany(),
23-
genre: belongsTo('genres'),
24-
bio: hasOne('bios')
21+
multiWords: hasMany('multi_words'),
22+
books: hasMany(),
23+
tags: hasMany(),
24+
genre: belongsTo('genres'),
25+
bio: hasOne('bios')
2526
});
2627

2728
class Book extends Model {
@@ -50,6 +51,10 @@ class Tag extends Model {
5051
name: string = attr()
5152
}
5253

54+
class MultiWord extends Model {
55+
static jsonapiType = 'multi_words';
56+
}
57+
5358
Config.setup();
5459

5560
export { Author, Person, Book, Genre, Bio, Tag };

test/unit/model-test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ describe('Model', function() {
2323
type: 'unknowns'
2424
}
2525
},
26+
multi_words: {
27+
data: [{
28+
id: '1',
29+
type: 'multi_words'
30+
}]
31+
},
2632
tags: {},
2733
genre: {
2834
data: {
@@ -105,6 +111,11 @@ describe('Model', function() {
105111
})
106112
});
107113

114+
it('camelizes relationship names', function() {
115+
let instance = Model.fromJsonapi(doc.data, doc);
116+
expect(instance.multiWords.length).to.eq(1);
117+
});
118+
108119
it('does not assign unknown attributes', function() {
109120
let instance = Model.fromJsonapi(doc.data, doc);
110121
expect(instance).to.not.have.property('unknown');

0 commit comments

Comments
 (0)