Skip to content

Commit

Permalink
feat(relations): fix coverage decreased and remove unnecessary if sta…
Browse files Browse the repository at this point in the history
…tement.
  • Loading branch information
Italo Izaac committed Apr 12, 2019
1 parent 9257efd commit 8b95787
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/Lucid/Relations/BelongsToMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,6 @@ class BelongsToMany extends BaseRelation {
*/
let hidden = _.get(pivotModel, '$hidden', [])

if (hidden.length === 0 && _.hasIn(pivotModel, 'constructor.hidden')) {
hidden = pivotModel.constructor.hidden
}

return (
(_.isArray(hidden) && hidden.includes('$pivot') === true) ||
this.$pivotAttribute === false
Expand Down
22 changes: 22 additions & 0 deletions test/unit/lucid-belongs-to-many.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2961,4 +2961,26 @@ test.group('Relations | Belongs To Many', (group) => {
const userPosts = user.posts()
assert.equal(userPosts.$pivotAttribute, 'pivot')
})

test('throw exception when pivotModel is defined and calling pivotPrimaryKey', (assert) => {
class Post extends Model {
}

class PostUser extends Model {
}

class User extends Model {
posts () {
return this.belongsToMany(Post).pivotModel(PostUser).pivotPrimaryKey('key')
}
}

User._bootIfNotBooted()
Post._bootIfNotBooted()
PostUser._bootIfNotBooted()

const user = new User()
const fn = () => user.posts()
assert.throw(fn, 'E_INVALID_RELATION_METHOD: Cannot call pivotPrimaryKey since pivotModel has been defined')
})
})

0 comments on commit 8b95787

Please sign in to comment.