Skip to content

Commit

Permalink
removed injected context and changed syntax so request,auth must be p…
Browse files Browse the repository at this point in the history
…assed manually
  • Loading branch information
Simon Tong committed Nov 11, 2017
1 parent ab629bd commit dedc226
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ This you can start using as follows:

```js
// create
const model = await MyModel.createWithAudit({name: 'John'})
const model = await MyModel.audit({request,auth}).create({name: 'John'})

// update
const model = MyModel.find(1)
await model.updateWithAudit({name: 'Simon'})
await model.audit({request, auth}).update({name: 'Simon'})

// delete
const model = MyModel.find(1)
await model.deleteWithAudit()
await model.audit({request, auth}).delete()
```

## Built With
Expand All @@ -62,3 +62,14 @@ await model.deleteWithAudit()
## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.


## Changelog

- v2.0.0
- Removed ctx injection on boot. `ctx` parameters need to be passed in manually now.
- Updated README to reflect new changes.
- Added this changelog.

- v1.0.1
- Initial release.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "adonis-auditable",
"version": "1.0.2",
"description": "",
"version": "2.0.0",
"description": "Audit AdonisJS models",
"main": " ",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
14 changes: 7 additions & 7 deletions providers/AuditableProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class AuditableProvider extends ServiceProvider {
}

boot () {
const Context = this.app.use('Adonis/Src/HttpContext')
const Auditable = this.app.use('Auditable')

// add ctx to datagrid
Context.onReady(ctx => {
Auditable.ctx = ctx
})
// const Context = this.app.use('Adonis/Src/HttpContext')
// const Auditable = this.app.use('Auditable')
//
// // add ctx to datagrid
// Context.onReady(ctx => {
// Auditable.ctx = ctx
// })
}
}

Expand Down
26 changes: 19 additions & 7 deletions src/Traits/Auditable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ const Audit = use('App/Models/Audit')

class Auditable {
register (Model) {
Model.createWithAudit = createWithAudit(this.ctx)
Model.prototype.updateWithAudit = updateWithAudit(this.ctx)
Model.prototype.deleteWithAudit = deleteWithAudit(this.ctx)
// create methods
Model.audit = function (ctx) {
return {
create: createWithAudit(ctx).bind(this)
}
}

// update/delete methods
Model.prototype.audit = function (ctx) {
return {
update: updateWithAudit(ctx).bind(this),
delete: deleteWithAudit(ctx).bind(this)
}
}
}
}

Expand All @@ -20,16 +31,17 @@ class Auditable {
*/
function createWithAudit ({request, auth}) {
return async function (data) {
const result = await this.create(data)
const newModel = (await this.find(result.primaryKeyValue))
const model = await this.create(data)
const newModel = (await this.find(model.primaryKeyValue))
const auditable = newModel.constructor.name
const auditableId = newModel.id
const newData = newModel.$attributes
const event = Audit.events.CREATE

// save audit
await createAudit(Audit.events.CREATE, {request, auth}, auditable, auditableId, null, newData)
await createAudit(event, {request, auth}, auditable, auditableId, null, newData)

return result
return model
}
}

Expand Down

0 comments on commit dedc226

Please sign in to comment.