Felix Sun 7750359880 Merge pull request #106 from qor5/refactor-activity-log | 1 год назад | |
---|---|---|
.. | ||
README.md | 2 лет назад | |
activity.go | 1 год назад | |
activity_log.go | 1 год назад | |
activity_model.go | 1 год назад | |
activity_test.go | 1 год назад | |
admin.go | 1 год назад | |
diff.go | 2 лет назад | |
diff_test.go | 2 лет назад | |
helper.go | 1 год назад | |
message.go | 2 лет назад | |
perm_policy.go | 1 год назад |
Firstly, you should create an activity instance in your project.
activity := activity.New(presetsBuilder, db, logModel)
presets.Builder
to register activity into presets.ActivityLogModel
interface.Register normal model or a presets.ModelBuilder
into activity
activity.RegisterModel(normalModel) // It need you to record the activity log manually
activity.RegisterModel(presetModel) // It will record the activity log automatically when you create, update or delete the model data via preset admin
Skip recording activity log for preset model if you don't want to record the activity log automatically
activity.RegisterModel(presetModel).SkipCreate().SkipUpdate().SkipDelete()
Configure more options for the presets.ModelBuilder
to record more custom information
activity.RegisterModel(presetModel).UseDefaultTab() //use activity tab on the admin model edit page
activity.RegisterModel(presetModel).AddKeys("ID", "Version") // will record value of the ID and Version field as the keyword of a model table
activity.RegisterModel(presetModel).AddIgnoredFields("UpdateAt") // will ignore the UpdateAt field when recording activity log for update operation
activity.RegisterModel(presetModel).AddTypeHanders(
time.Time{},
func(old, now interface{}, prefixField string) []Diff {
oldString := old.(time.Time).Format(time.RFC3339)
nowString := now.(time.Time).Format(time.RFC3339)
if oldString != nowString {
return []Diff{
{Field: prefixField, Old: oldString, Now: nowString},
}
}
return []Diff{}
}
) // you define your own type handler to record some custom type for update operation
Record log manually when you use a normal model or save the model data via db directly
When a struct type only have one activity.ModelBuilder
, you can use activity
to record the log directly.
activity.AddRecords(ActivityEdit, ctx, record)
activity.AddRecords(ActivityCreate, ctx, record)
When a struct type have multiple activity.ModelBuilder
, you need to get the corresponding activity.ModelBuilder
and then use it to record the log.
activity.MustGetModelBuilder(presetModel1).AddRecords(ActivityEdit, ctx, record)
activity.MustGetModelBuilder(presetModel2).AddRecords(ActivityEdit, ctx, record)