Skip to content

Commit

Permalink
feat(traits): allow user to pass options to addTrait
Browse files Browse the repository at this point in the history
* Add settings to trait registration

* settings -> options
  • Loading branch information
benallfree authored and thetutlage committed Nov 7, 2017
1 parent 26cca0e commit be987ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class Model extends BaseModel {
*
* @param {Function|String} trait - A plain function or reference to IoC container string
*/
static addTrait (trait) {
static addTrait (trait, options = {}) {
if (typeof (trait) !== 'function' && typeof (trait) !== 'string') {
throw GE
.InvalidArgumentException
Expand All @@ -390,7 +390,7 @@ class Model extends BaseModel {
*/
trait = typeof (trait) === 'string' ? `${trait}.register` : trait
const { method } = resolver.forDir('modelTraits').resolveFunc(trait)
method(this)
method(this, options)
}

/**
Expand Down
36 changes: 36 additions & 0 deletions test/unit/traits.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,40 @@ test.group('Traits', (group) => {

assert.deepEqual(stack, ['on user', 'on profile'])
})

test('pass options to trait via ioc container', (assert) => {
class FooTrait {
register (ctx, options) {
assert.deepEqual(ctx, User)
assert.deepEqual(options, {foo: 1})
}
}

ioc.fake('FooTrait', () => {
return new FooTrait()
})

class User extends Model {
static boot () {
super.boot()
this.addTrait('@provider:FooTrait', {foo: 1})
}
}

User._bootIfNotBooted()
})

test('pass options to bound function', (assert) => {
class User extends Model {
static boot () {
super.boot()
this.addTrait((ctx, options) => {
assert.deepEqual(ctx, User)
assert.deepEqual(options, {foo: 1})
}, {foo: 1})
}
}

User._bootIfNotBooted()
})
})

0 comments on commit be987ca

Please sign in to comment.