Skip to content

Instantly share code, notes, and snippets.

@tednaleid
Created May 27, 2009 01:28
Show Gist options
  • Save tednaleid/118407 to your computer and use it in GitHub Desktop.
Save tednaleid/118407 to your computer and use it in GitHub Desktop.
adding arguments to GORM save through metaClass
// do this in the plugins doWithDynamic methods on each domain class:
def domainClass = grailsApplication.getDomainClass("Foo")
def clazz = domainClass.clazz
// prime the pump to get the GORM methods actually decorated and available on the meta class
clazz.count()
clazz.metaClass.invokeMethod = { String name, args ->
if (name == "save" && args[0] instanceof Map) {
Map saveArgs = args[0]
if (saveArgs.bleat) {
println "Bleating out $saveArgs"
}
}
return clazz.metaClass.getMetaMethod(name, args).invoke(delegate, args)
}
def f = new Foo(name: "bar")
f.save(bleat: true) // prints out "Bleating out [bleat: true]" and then saves the object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment