More Than Five
More Than Five

Reputation: 10419

Add properties and methods to all GORM objects

I want to be able to add the following property updatedBy

to most of my GORM objects. I was thinking of using an AST Transformation. Something like what is described here: http://www.zorched.net/tag/grails/

But I need to set this property on the gorm hooks beforeUpdate() and beforeInsert() something like

def beforeUpdate() {
    updatedBy = springSecurityService.currentUser;
}

This is where I am struggling. Any help appreciated?

Upvotes: 0

Views: 423

Answers (1)

Dónal
Dónal

Reputation: 187529

Encapsulate this as a trait or abstract class that's implemented or extended by each domain class. Preferably a trait, if you're using a version of Groovy that's sufficiently recent to support traits. If not, an abstract class like this

class Audited {
  def springSecurityService
  def updatedBy

  def beforeUpdate() {
    updatedBy = springSecurityService.currentUser
  }
}

Upvotes: 4

Related Questions