Skip to content

Commit

Permalink
Add V1 postfix to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme committed Feb 28, 2024
1 parent 219d979 commit 0132f67
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func init() {
// beforeCreateCallback will invoke `BeforeSave`, `BeforeCreate` method before creating
func beforeCreateCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeSaveV1")
}
if !scope.HasError() {
scope.CallMethod("BeforeCreate")
scope.CallMethod("BeforeCreateV2")
}
}

Expand Down Expand Up @@ -196,9 +196,9 @@ func forceReloadAfterCreateCallback(scope *Scope) {
// afterCreateCallback will invoke `AfterCreate`, `AfterSave` method after creating
func afterCreateCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("AfterCreate")
scope.CallMethod("AfterCreateV1")
}
if !scope.HasError() {
scope.CallMethod("AfterSave")
scope.CallMethod("AfterSaveV1")
}
}
4 changes: 2 additions & 2 deletions callback_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func beforeDeleteCallback(scope *Scope) {
return
}
if !scope.HasError() {
scope.CallMethod("BeforeDelete")
scope.CallMethod("BeforeDeleteV1")
}
}

Expand Down Expand Up @@ -58,6 +58,6 @@ func deleteCallback(scope *Scope) {
// afterDeleteCallback will invoke `AfterDelete` method after deleting
func afterDeleteCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("AfterDelete")
scope.CallMethod("AfterDeleteV1")
}
}
2 changes: 1 addition & 1 deletion callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ func queryCallback(scope *Scope) {
// afterQueryCallback will invoke `AfterFind` method after querying
func afterQueryCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("AfterFind")
scope.CallMethod("AfterFindV1")
}
}
8 changes: 4 additions & 4 deletions callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func beforeUpdateCallback(scope *Scope) {
}
if _, ok := scope.Get("gorm:update_column"); !ok {
if !scope.HasError() {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeSaveV1")
}
if !scope.HasError() {
scope.CallMethod("BeforeUpdate")
scope.CallMethod("BeforeUpdateV1")
}
}
}
Expand Down Expand Up @@ -112,10 +112,10 @@ func updateCallback(scope *Scope) {
func afterUpdateCallback(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok {
if !scope.HasError() {
scope.CallMethod("AfterUpdate")
scope.CallMethod("AfterUpdateV1")
}
if !scope.HasError() {
scope.CallMethod("AfterSave")
scope.CallMethod("AfterSaveV1")
}
}
}
18 changes: 9 additions & 9 deletions callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,59 @@ import (
"github.com/jinzhu/gorm"
)

func (s *Product) BeforeCreate() (err error) {
func (s *Product) BeforeCreateV1() (err error) {
if s.Code == "Invalid" {
err = errors.New("invalid product")
}
s.BeforeCreateCallTimes = s.BeforeCreateCallTimes + 1
return
}

func (s *Product) BeforeUpdate() (err error) {
func (s *Product) BeforeUpdateV1() (err error) {
if s.Code == "dont_update" {
err = errors.New("can't update")
}
s.BeforeUpdateCallTimes = s.BeforeUpdateCallTimes + 1
return
}

func (s *Product) BeforeSave() (err error) {
func (s *Product) BeforeSaveV1() (err error) {
if s.Code == "dont_save" {
err = errors.New("can't save")
}
s.BeforeSaveCallTimes = s.BeforeSaveCallTimes + 1
return
}

func (s *Product) AfterFind() {
func (s *Product) AfterFindV1() {
s.AfterFindCallTimes = s.AfterFindCallTimes + 1
}

func (s *Product) AfterCreate(tx *gorm.DB) {
func (s *Product) AfterCreateV1(tx *gorm.DB) {
tx.Model(s).UpdateColumn(Product{AfterCreateCallTimes: s.AfterCreateCallTimes + 1})
}

func (s *Product) AfterUpdate() {
func (s *Product) AfterUpdateV1() {
s.AfterUpdateCallTimes = s.AfterUpdateCallTimes + 1
}

func (s *Product) AfterSave() (err error) {
func (s *Product) AfterSaveV1() (err error) {
if s.Code == "after_save_error" {
err = errors.New("can't save")
}
s.AfterSaveCallTimes = s.AfterSaveCallTimes + 1
return
}

func (s *Product) BeforeDelete() (err error) {
func (s *Product) BeforeDeleteV1() (err error) {
if s.Code == "dont_delete" {
err = errors.New("can't delete")
}
s.BeforeDeleteCallTimes = s.BeforeDeleteCallTimes + 1
return
}

func (s *Product) AfterDelete() (err error) {
func (s *Product) AfterDeleteV1() (err error) {
if s.Code == "after_delete_error" {
err = errors.New("can't delete")
}
Expand Down

0 comments on commit 0132f67

Please sign in to comment.