Skip to content

Commit

Permalink
fix(hooks): improve defineHooks signature
Browse files Browse the repository at this point in the history
defineHooks method accepts an array of hooks as 2nd argument or you can pass multiple arguments,

where every argument expects the first one will be treated as an hook

Closes #94
  • Loading branch information
thetutlage committed Feb 25, 2017
1 parent a21d6fc commit ca614e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,9 @@ class Model {
*
* @public
*/
static defineHooks () {
static defineHooks (type, hooks) {
this.$modelHooks = {}
const args = _.toArray(arguments)
const type = args[0]
const hooks = _.tail(args)
hooks = _.isArray(hooks) ? hooks : _.tail(arguments)
_.each(hooks, (hook) => {
this.addHook(type, hook)
})
Expand Down
12 changes: 12 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,18 @@ describe('Lucid', function () {
expect(User.$modelHooks['beforeCreate'][1].handler).to.have.property('method')
})

it('should be able to define multiple hooks as an array', function () {
class User extends Model {}
User.bootIfNotBooted()
User.defineHooks('beforeCreate', ['Users.validate', 'Users.log'])
expect(User.$modelHooks['beforeCreate']).to.be.an('array')
expect(User.$modelHooks['beforeCreate'].length).to.equal(2)
expect(User.$modelHooks['beforeCreate'][0].handler).to.have.property('instance')
expect(User.$modelHooks['beforeCreate'][0].handler).to.have.property('method')
expect(User.$modelHooks['beforeCreate'][1].handler).to.have.property('instance')
expect(User.$modelHooks['beforeCreate'][1].handler).to.have.property('method')
})

it('should override existing hooks when calling defineHooks', function () {
class User extends Model {}
User.bootIfNotBooted()
Expand Down

0 comments on commit ca614e0

Please sign in to comment.