Currently migrating an application from Grails 2.4.5 to Grails 3.2.6. We need the ability to modify the template path before rendering and have done this in Grails 2 BootStrap.groovy:
def init = { servletContext ->
log.debug "Executing BootStrap init"
//Modify all controllers to work for multiple tenants.
grailsApplication.controllerClasses.each() { controllerClass ->
log.debug "Modifying render method on controller ${controllerClass.name}"
def oldRender = controllerClass.metaClass.pickMethod("render", [Map] as Class[])
controllerClass.metaClass.render = { Map params ->
log.debug "In bootstrap overridden method: "
if (params.template && params.template.startsWith("/common")) {
params.template = "/tenants" + params.template
log.debug "Common template found " + params.template
}
else if (params.view && params.view.startsWith("/common")) {
params.view = "/tenants" + params.view
log.debug "Common view found " + params.view
}
else if (session.tenant) {
if (params.template && params.template[0] == '/') {
log.debug "Template was " + params.template
params.template = "/tenants/" + session.tenant + params.template
log.debug "Template is " + params.template
}
if (params.view && params.view[0] == '/') {
log.debug "View was " + params.view
params.view = "/tenants/" + session.tenant + params.view
log.debug "View is " + params.view
}
}
oldRender.invoke(delegate, [params] as Object[])
}
}
}
However, the modified render method never seems to be called in Grails 3.2.6. Any suggestions? I'm open to overriding the render method some other way if there is a better solution . . .