GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / web / org / codehaus / groovy / grails / web / metaclass / ChainMethod.groovy
blobf2ad98afb857063a9d959a6c556a95ad634201b3
1 package org.codehaus.groovy.grails.web.metaclass
3 import org.springframework.web.context.request.RequestContextHolder
4 import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
5 import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
6 import grails.util.GrailsWebUtil
7 import org.codehaus.groovy.grails.commons.*
8 import org.codehaus.groovy.grails.web.mapping.UrlMappingsHolder
9 import org.codehaus.groovy.grails.web.mapping.UrlCreator
11 /**
12 * Implementation of the chain() method for controllers
14 * @author Graeme Rocher
15 * @since 1.0
17 * Created: Jun 2, 2008
19 class ChainMethod {
22 static invoke(target, Map args = [:]) {
23 def controller = args.controller ?: GCU.getLogicalPropertyName(target.class.name,ControllerArtefactHandler.TYPE )
24 def action = args.action
25 def id = args.id
26 def params = args.params ?: [:]
27 def model = args.model ?: [:]
29 def actionParams = params.findAll { it.key?.startsWith('_action_') }
30 actionParams?.each { params.remove(it.key) }
33 GrailsWebRequest webRequest = RequestContextHolder.currentRequestAttributes()
34 def flash = webRequest.getFlashScope()
36 def chainModel = flash.chainModel
37 if(chainModel instanceof Map) {
38 chainModel.putAll(model)
39 model = chainModel
41 flash.chainModel = model
43 if(action instanceof Closure) {
44 def prop = GCU.getPropertyDescriptorForValue(target, action)
45 if(prop) action = prop.name
46 else {
47 def scaffolder = GrailsWebUtil.getScaffolderForController(controller, webRequest)
48 action = scaffolder?.getActionName(action)
51 else {
52 action = action?.toString()
55 def appCtx = webRequest.getApplicationContext()
57 UrlMappingsHolder mappings = appCtx.getBean(UrlMappingsHolder.BEAN_ID)
59 UrlCreator creator = mappings.getReverseMapping(controller, action, params)
60 def response = webRequest.getCurrentResponse()
62 def url = response.encodeRedirectURL(creator.createURL(controller,action, params, 'utf-8'))
63 response.sendRedirect url