Skip to content

Commit

Permalink
Split out validation and pre save hooks into PreSave so we can use th…
Browse files Browse the repository at this point in the history
…at separately for "fake save"
  • Loading branch information
Jason Raede committed Mar 31, 2015
1 parent b88abb2 commit 94230d4
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,7 @@ func (c *Collection) collectionOnSession(sess *mgo.Session) *mgo.Collection {
return sess.DB(c.Connection.Config.Database).C(c.Name)
}

func (c *Collection) Save(doc Document) error {
var err error
sess := c.Connection.Session.Clone()
defer sess.Close()

// Per mgo's recommendation, create a clone of the session so there is no blocking
col := c.collectionOnSession(sess)

func (c *Collection) PreSave(doc Document) error {
// Validate?
if validator, ok := doc.(ValidateHook); ok {
errs := validator.Validate(c)
Expand All @@ -104,6 +97,21 @@ func (c *Collection) Save(doc Document) error {
}
}

return nil
}

func (c *Collection) Save(doc Document) error {
var err error
sess := c.Connection.Session.Clone()
defer sess.Close()

// Per mgo's recommendation, create a clone of the session so there is no blocking
col := c.collectionOnSession(sess)

err = c.PreSave(doc)
if err != nil {
return err
}
// If the model implements the NewTracker interface, we'll use that to determine newness. Otherwise always assume it's new

isNew := true
Expand Down

0 comments on commit 94230d4

Please sign in to comment.