-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandoGrailsPlugin.groovy
92 lines (78 loc) · 3.25 KB
/
CommandoGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import org.springframework.web.context.request.RequestContextHolder as RCH
import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
import org.springframework.validation.Errors
import org.codehaus.groovy.grails.validation.ConstrainedPropertyBuilder
import org.springframework.validation.BeanPropertyBindingResult
import org.springframework.validation.BindException
import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.codehaus.groovy.grails.web.metaclass.*
import org.codehaus.groovy.grails.web.binding.DataBindingUtils;
import org.codehaus.groovy.grails.plugins.DomainClassPluginSupport
class CommandoGrailsPlugin {
def version = 0.1
def grailsVersion = grails.util.GrailsUtil.getGrailsVersion()
def dependsOn = [core:1.1]
def loadAfter = ['hibernate', 'controllers']
def observe = ["controllers"]
def author = "Zan Thrash"
def authorEmail = "[email protected]"
def title = "Commando"
def description = '''\
Provies a factory to create and properly wire up command objects so you don't have to
always passs as a parameter to your action.
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/Commando+Plugin"
def doWithSpring = {
// TODO Implement runtime spring config (optional)
}
def doWithApplicationContext = { applicationContext ->
// TODO Implement post initialization spring config (optional)
}
def doWithWebDescriptor = { xml ->
// TODO Implement additions to web.xml (optional)
}
def doWithDynamicMethods = { ctx ->
application.controllerClasses.each { controller ->
addCreateCommandToController(controller, ctx)
}
}
def onChange = { event ->
if (application.isControllerClass(event.source)){
addCreateCommandToController(event.source, event.ctx)
}
}
def addCreateCommandToController(controller, ctx){
controller.metaClass.createCommand = { commandObjectClass,params ->
def commandObject = commandObjectClass.newInstance()
def commandObjectMetaClass = commandObjectClass.metaClass
commandObjectMetaClass.setErrors = {Errors errors ->
RCH.currentRequestAttributes().setAttribute("${commandObjectClass.name}_errors", errors, 0)
}
commandObjectMetaClass.getErrors = {->
RCH.currentRequestAttributes().getAttribute("${commandObjectClass.name}_errors", 0)
}
commandObjectMetaClass.hasErrors = {->
delegate.errors?.hasErrors()
}
commandObjectMetaClass.validate = {->
DomainClassPluginSupport.validateInstance(delegate, ctx)
}
def validationClosure = GCU.getStaticPropertyValue(commandObjectClass, 'constraints')
if (validationClosure) {
def constrainedPropertyBuilder = new ConstrainedPropertyBuilder(commandObject)
validationClosure.setDelegate(constrainedPropertyBuilder)
validationClosure()
commandObjectMetaClass.constraints = constrainedPropertyBuilder.constrainedProperties
} else {
commandObjectMetaClass.constraints = [:]
}
DataBindingUtils.bindObjectToInstance(commandObject, params)
return commandObject
}
}
def onConfigChange = { event ->
// TODO Implement code that is executed when the project configuration changes.
// The event is the same as for 'onChange'.
}
}