Skip to content

Instantly share code, notes, and snippets.

@mostlylikeable
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save mostlylikeable/637bda7e2172104f1f3b to your computer and use it in GitHub Desktop.

Select an option

Save mostlylikeable/637bda7e2172104f1f3b to your computer and use it in GitHub Desktop.
Suppress auto-timestamping in Groovy
def withAutoTimestampSuppression(entity, closure) {
toggleAutoTimestamp(entity, false)
try {
def result = closure()
return result
} finally {
toggleAutoTimestamp(entity, true)
}
}
void toggleAutoTimestamp(def domainObjectInstance, boolean shouldTimestamp) {
ApplicationContext ctx = ServletContextHolder.servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
ClosureEventTriggeringInterceptor closureInterceptor = ctx.eventTriggeringInterceptor
Datastore datastore = closureInterceptor.datastores.values().first()
EventTriggeringInterceptor interceptor = datastore.eventTriggeringInterceptor
ClosureEventListener listener = interceptor.findEventListener(domainObjectInstance)
listener.shouldTimestamp = shouldTimestamp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment