Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gobuffalo/pop
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Mar 13, 2018
2 parents d85be8b + 7980e4a commit 2cbfb90
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
56 changes: 37 additions & 19 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"golang.org/x/sync/errgroup"
)

type afterFindable interface {
// AfterFindable callback will be called after a record, or records,
// has been retrieved from the database.
type AfterFindable interface {
AfterFind(*Connection) error
}

func (m *Model) afterFind(c *Connection) error {
if x, ok := m.Value.(afterFindable); ok {
if x, ok := m.Value.(AfterFindable); ok {
if err := x.AfterFind(c); err != nil {
return errors.WithStack(err)
}
Expand All @@ -33,7 +35,7 @@ func (m *Model) afterFind(c *Connection) error {
wg.Go(func() error {
y := rv.Index(i)
y = y.Addr()
if x, ok := y.Interface().(afterFindable); ok {
if x, ok := y.Interface().(AfterFindable); ok {
return x.AfterFind(c)
}
return nil
Expand All @@ -44,89 +46,105 @@ func (m *Model) afterFind(c *Connection) error {
return wg.Wait()
}

type beforeSaveable interface {
// BeforeSaveable callback will be called before a record is
// either created or updated in the database.
type BeforeSaveable interface {
BeforeSave(*Connection) error
}

func (m *Model) beforeSave(c *Connection) error {
if x, ok := m.Value.(beforeSaveable); ok {
if x, ok := m.Value.(BeforeSaveable); ok {
return x.BeforeSave(c)
}
return nil
}

type beforeCreateable interface {
// BeforeCreateable callback will be called before a record is
// created in the database.
type BeforeCreateable interface {
BeforeCreate(*Connection) error
}

func (m *Model) beforeCreate(c *Connection) error {
if x, ok := m.Value.(beforeCreateable); ok {
if x, ok := m.Value.(BeforeCreateable); ok {
return x.BeforeCreate(c)
}
return nil
}

type beforeUpdateable interface {
// BeforeUpdateable callback will be called before a record is
// updated in the database.
type BeforeUpdateable interface {
BeforeUpdate(*Connection) error
}

func (m *Model) beforeUpdate(c *Connection) error {
if x, ok := m.Value.(beforeUpdateable); ok {
if x, ok := m.Value.(BeforeUpdateable); ok {
return x.BeforeUpdate(c)
}
return nil
}

type beforeDestroyable interface {
// BeforeDestroyable callback will be called before a record is
// destroyed in the database.
type BeforeDestroyable interface {
BeforeDestroy(*Connection) error
}

func (m *Model) beforeDestroy(c *Connection) error {
if x, ok := m.Value.(beforeDestroyable); ok {
if x, ok := m.Value.(BeforeDestroyable); ok {
return x.BeforeDestroy(c)
}
return nil
}

type afterDestroyable interface {
// AfterDestroyable callback will be called after a record is
// destroyed in the database.
type AfterDestroyable interface {
AfterDestroy(*Connection) error
}

func (m *Model) afterDestroy(c *Connection) error {
if x, ok := m.Value.(afterDestroyable); ok {
if x, ok := m.Value.(AfterDestroyable); ok {
return x.AfterDestroy(c)
}
return nil
}

type afterUpdateable interface {
// AfterUpdateable callback will be called after a record is
// updated in the database.
type AfterUpdateable interface {
AfterUpdate(*Connection) error
}

func (m *Model) afterUpdate(c *Connection) error {
if x, ok := m.Value.(afterUpdateable); ok {
if x, ok := m.Value.(AfterUpdateable); ok {
return x.AfterUpdate(c)
}
return nil
}

type afterCreateable interface {
// AfterCreateable callback will be called after a record is
// created in the database.
type AfterCreateable interface {
AfterCreate(*Connection) error
}

func (m *Model) afterCreate(c *Connection) error {
if x, ok := m.Value.(afterCreateable); ok {
if x, ok := m.Value.(AfterCreateable); ok {
return x.AfterCreate(c)
}
return nil
}

type afterSaveable interface {
// AfterSaveable callback will be called after a record is
// either created or updated in the database.
type AfterSaveable interface {
AfterSave(*Connection) error
}

func (m *Model) afterSave(c *Connection) error {
if x, ok := m.Value.(afterSaveable); ok {
if x, ok := m.Value.(AfterSaveable); ok {
return x.AfterSave(c)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ScopeFunc func(q *Query) *Query
// return q.Where("deleted_at is null")
// }
//
// c.Scope(ByName("mark)).Scope(NotDeleted).First(&User{})
// c.Scope(ByName("mark)).Scope(WithDeleted).First(&User{})
func (q *Query) Scope(sf ScopeFunc) *Query {
return sf(q)
}
Expand All @@ -32,7 +32,7 @@ func (q *Query) Scope(sf ScopeFunc) *Query {
// return q.Where("deleted_at is null")
// }
//
// c.Scope(ByName("mark)).Scope(NotDeleted).First(&User{})
// c.Scope(ByName("mark)).Scope(WithDeleted).First(&User{})
func (c *Connection) Scope(sf ScopeFunc) *Query {
return Q(c).Scope(sf)
}

0 comments on commit 2cbfb90

Please sign in to comment.