Skip to content

Commit

Permalink
fix(relations): relation parser set nested relations on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 16, 2017
1 parent 713b55f commit 48bdbba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Lucid/Relations/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class RelationParser {
*/
if (existingRelation) {
existingRelation.callback = parsedRelation.callback
_.each(parsedRelation.nested, (v, k) => (existingRelation.nested[k] = v))
_.each(parsedRelation.nested, (v, k) => {
existingRelation.nested = existingRelation.nested || {}
existingRelation.nested[k] = v
})
return result
}

Expand Down
5 changes: 5 additions & 0 deletions test/unit/relations-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ test.group('Relations Parser', () => {
assert.deepEqual(parsed, { posts: { name: 'posts', callback: null, nested: { comments: null, likes: null } } })
})

test('parse multiple nested relations when first relation doesn\t have nesting', (assert) => {
const parsed = RelationsParser.parseRelations({ 'posts': null, 'posts.comments': null, 'posts.likes': null })
assert.deepEqual(parsed, { posts: { name: 'posts', callback: null, nested: { comments: null, likes: null } } })
})

test('parse multiple nested relations with callback', (assert) => {
const fn = function () {}
const parsed = RelationsParser.parseRelations({ 'posts.comments': null, 'posts.likes': null, posts: fn })
Expand Down

0 comments on commit 48bdbba

Please sign in to comment.