activity.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package basics
  2. import (
  3. "github.com/qor5/docs/docsrc/generated"
  4. . "github.com/theplant/docgo"
  5. "github.com/theplant/docgo/ch"
  6. )
  7. var Activity = Doc(
  8. Markdown(`
  9. QOR5 provides a built-in activity module for recording model operations that may be important for admin users of CMS. These records are designed to be easily queried and audited, and the activity module supports the following features:
  10. * Detailed change logging functionality for model data modifications.
  11. * Allow certain fields to be ignored when comparing modified data, such as the update time.
  12. * Customization of the diffing process for complex field types, like time.Time.
  13. * Customization of the keys used to identify model data.
  14. * Support both automatic and manual CRUD operation recording.
  15. * Provide flexibility to customize the actions other than default CRUD.
  16. * An page for querying the activity log via QOR5 admin
  17. ## Initialize the activity package
  18. To initialize activity package with the default configuration, you need to pass a ~presets.Builder~ instance and a database instance.
  19. `),
  20. ch.Code(generated.NewActivitySample).Language("go"),
  21. Markdown(`
  22. By default, the activity package uses QOR5 login package's ~~login.UserKey~~ as the default key to fetch the current user from the context. If you want to use your own key, you can use the ~~SetCreatorContextKey~~ function.
  23. Same with above, the activity package uses the db instance that passed in during initialization to perform db operations. If you need another db to do the work, you can use ~~SetDBContextKey~~ method.
  24. ## Register the models that require activity tracking
  25. This example demonstrates how to register ~~Product~~ into the activity. The activities on the product model will be automatically recorded when it is created, updated, or deleted.
  26. `),
  27. ch.Code(generated.ActivityRegisterPresetsModelsSample).Language("go"),
  28. Markdown(`
  29. By default, the activity package will use the primary key as the key to indentify the current model data. You can use ~~SetKeys~~ and ~~AddKeys~~ methods to customize it.
  30. When diffing the modified data, the activity package will ignore the ~~ID~~, ~~CreatedAt~~, ~~UpdatedAt~~, ~~DeletedAt~~ fields. You can either use ~~AddIgnoredFields~~ to append your own fields to the default ignored fields. Or ~~SetIgnoredFields~~ method to replace the default ignored fields.
  31. For special fields like ~~time.Time~~ or media files handled by QOR5 media_library, activity package already handled them. You can use ~~AddTypeHanders~~ method to handle your own field types.
  32. If you want to skip the automatic recording, you can use ~~SkipCreate~~, ~~SkipUpdate~~ and ~~SkipDelete~~ methods.
  33. The Activity package allows for displaying the activities of a record on its editing page. Simply use the ~~EnableActivityInfoTab~~ method to enable this feature. Once enabled, you can customize the format of each activity's display text using the ~~SetTabHeading~~ method. Additionally, you can make each activity a link to the corresponding record using the ~~SetLink~~ method.
  34. ## Record the activity log manually
  35. If you register a preset model into the activity, the activity package will automatically record the activity log for CRUD operations. However, if you need to manually record the activity log for other operations or if you want to register a non-preset model, you can use the following sample code.`),
  36. ch.Code(generated.ActivityRecordLogSample).Language("go"),
  37. ).Title("Activity Log")