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

Commit

Permalink
add SetUserAndFrom func (#2)
Browse files Browse the repository at this point in the history
* add SetUserAndFrom func

* change form to where, update readme

* fix comments
  • Loading branch information
vetcher authored and sas1024 committed Sep 29, 2017
1 parent 0ecc0ea commit cdd1f4f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ It creates `change_logs` table in your database and writes to all loggable model
## Usage
1. Register plugin using `loggable.Register(db)`
2. Add `loggable.LoggableModel` to your GORM model
3. If you want to set user, who makes changes, use `loggable.SetUser("username")`.
3. If you want to set user, who makes changes, and place, where it happened, use `loggable.SetUserAndFrom("username", "London")`.
35 changes: 23 additions & 12 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
)

type LoggablePlugin interface {
// Deprecated: Use SetUserAndWhere instead.
SetUser(user string) *gorm.DB
SetFrom(from string) *gorm.DB
// Deprecated: Use SetUserAndWhere instead.
SetWhere(from string) *gorm.DB
SetUserAndWhere(user, where string) *gorm.DB
GetRecords(objectId string) ([]*ChangeLog, error)
}

Expand Down Expand Up @@ -42,20 +45,28 @@ func (r *loggablePlugin) GetRecords(objectId string) ([]*ChangeLog, error) {
return changes, nil
}

// Deprecated: Use SetUserAndWhere instead.
func (r *loggablePlugin) SetUser(user string) *gorm.DB {
r.mu.Lock()
db := r.db.Set("loggable:user", user)
r.mu.Unlock()
return db
}

func (r *loggablePlugin) SetFrom(from string) *gorm.DB {
// Deprecated: Use SetUserAndWhere instead.
func (r *loggablePlugin) SetWhere(where string) *gorm.DB {
r.mu.Lock()
db := r.db.Set("loggable:from", from)
db := r.db.Set("loggable:where", where)
r.mu.Unlock()
return db
}

func (r *loggablePlugin) SetUserAndWhere(user, where string) *gorm.DB {
r.mu.Lock()
defer r.mu.Unlock()
return r.db.Set("loggable:user", user).Set("loggable:where", where)
}

func (r *loggablePlugin) addRecord(scope *gorm.Scope, action string) error {
var jsonObject JSONB
j, err := json.Marshal(scope.Value)
Expand All @@ -70,19 +81,19 @@ func (r *loggablePlugin) addRecord(scope *gorm.Scope, action string) error {
if !ok {
user = ""
}
from, ok := scope.DB().Get("loggable:from")
where, ok := scope.DB().Get("loggable:where")
if !ok {
from = ""
where = ""
}

cl := ChangeLog{
ID: uuid.NewV4().String(),
ChangedBy: user.(string),
ChangedFrom: from.(string),
Action: action,
ObjectID: scope.PrimaryKeyValue().(string),
ObjectType: scope.GetModelStruct().ModelType.Name(),
Object: jsonObject,
ID: uuid.NewV4().String(),
ChangedBy: user.(string),
ChangedWhere: where.(string),
Action: action,
ObjectID: scope.PrimaryKeyValue().(string),
ObjectType: scope.GetModelStruct().ModelType.Name(),
Object: jsonObject,
}
err = scope.DB().Create(&cl).Error
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions loggable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

type ChangeLog struct {
ID string `gorm:"type:uuid;primary_key;"`
CreatedAt time.Time `sql:"DEFAULT:current_timestamp"`
ChangedBy string `gorm:"index"`
ChangedFrom string `gorm:"index"`
Action string
ObjectID string `gorm:"index"`
ObjectType string `gorm:"index"`
Object JSONB `sql:"type:JSONB"`
ID string `gorm:"type:uuid;primary_key;"`
CreatedAt time.Time `sql:"DEFAULT:current_timestamp"`
ChangedBy string `gorm:"index"`
ChangedWhere string `gorm:"index"`
Action string
ObjectID string `gorm:"index"`
ObjectType string `gorm:"index"`
Object JSONB `sql:"type:JSONB"`
}

type loggableInterface interface {
Expand Down

0 comments on commit cdd1f4f

Please sign in to comment.