Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
uuid package change, add enable/disable logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sas1024 committed Sep 3, 2018
1 parent c415b13 commit 6d2e41f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
12 changes: 0 additions & 12 deletions .editorconfig

This file was deleted.

8 changes: 4 additions & 4 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"encoding/json"

"github.com/jinzhu/gorm"
"github.com/satori/go.uuid"
"github.com/gofrs/uuid"
)

func (p *Plugin) addCreated(scope *gorm.Scope) {
if isLoggable(scope) {
if isLoggable(scope) && isEnabled(scope) {
addRecord(scope, "create")
}
}

func (p *Plugin) addUpdated(scope *gorm.Scope) {
if isLoggable(scope) {
if isLoggable(scope) && isEnabled(scope) {
if p.opts.lazyUpdate {
record, err := p.GetLastRecord(interfaceToString(scope.PrimaryKeyValue()), false)
if err == nil {
Expand All @@ -28,7 +28,7 @@ func (p *Plugin) addUpdated(scope *gorm.Scope) {
}

func (p *Plugin) addDeleted(scope *gorm.Scope) {
if isLoggable(scope) {
if isLoggable(scope) && isEnabled(scope) {
addRecord(scope, "delete")
}
}
Expand Down
15 changes: 14 additions & 1 deletion loggable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ type Interface interface {
Meta() interface{}
// lock makes available only embedding structures.
lock()
// check if callback enabled
isEnabled() bool
// enable/disable loggable
Enable(v bool)
}

// LoggableModel is a root structure, which implement Interface.
// Embed LoggableModel to your model so that Plugin starts tracking changes.
type LoggableModel struct{}
type LoggableModel struct {
Disabled bool `sql:"-"`
}

func (LoggableModel) Meta() interface{} { return nil }
func (LoggableModel) lock() {}
func (l LoggableModel) isEnabled() bool { return !l.Disabled }
func (l LoggableModel) Enable(v bool) { l.Disabled = !v }

// ChangeLog is a main entity, which used to log changes.
type ChangeLog struct {
Expand Down Expand Up @@ -72,3 +80,8 @@ func isLoggable(scope *gorm.Scope) bool {
_, ok := scope.Value.(Interface)
return ok
}

func isEnabled(scope *gorm.Scope) bool {
v, ok := scope.Value.(Interface)
return ok && v.isEnabled()
}

0 comments on commit 6d2e41f

Please sign in to comment.