Permalink
Browse files

merge g.inject tag with g.set tag

  • Loading branch information...
1 parent 1ae8b8c commit 8342a7f206b064c72350839dea0d8245504bfeed @houbie houbie committed Jun 13, 2012
View
44 ...sp/src/main/groovy/org/codehaus/groovy/grails/plugins/web/taglib/ApplicationTagLib.groovy
@@ -99,9 +99,11 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
/**
* Sets a variable in the pageContext or the specified scope.
+ * The value can be specified directly or can be a bean retrieved from the applicationContext.
*
* @attr var REQUIRED the variable name
* @attr value the variable value; if not specified uses the rendered body
+ * @attr bean the name or the type of a bean in the applicationContext; the type can be an interface or superclass
* @attr scope the scope name; defaults to pageScope
*/
Closure set = { attrs, body ->
@@ -111,44 +113,16 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
def scope = attrs.scope ? SCOPES[attrs.scope] : 'pageScope'
if (!scope) throw new IllegalArgumentException("Invalid [scope] attribute for tag <g:set>!")
- def value = attrs.value
- def containsValue = attrs.containsKey('value')
-
- if (!containsValue && body) value = body()
-
- this."$scope"."$var" = value
- null
- }
-
- /**
- * Injects a dependency into a variable in the pageContext or the specified scope.
- *
- * @attr beanName the bean name; either beanName or beanType must be specified
- * @attr beanType type the bean must match; can be an interface or superclass; either beanName or beanType must be specified
- * @attr var the variable name; mandatory if beanType is specified, otherwise defaults to beanName
- * @attr scope the scope name; defaults to pageScope
- */
- Closure inject = { attrs, body ->
- def bean
- def var = attrs.var
- if (attrs.beanName) {
- bean = applicationContext.getBean(attrs.beanName)
- if (!var) {
- var = attrs.beanName
- }
- } else if (attrs.beanType) {
- bean = applicationContext.getBean(attrs.beanType)
- if (!var) {
- throw new IllegalArgumentException("[var] attribute must be specified to for <g:inject> when the [beanType] attribute is also specified!")
- }
+ def value
+ if (attrs.bean) {
+ value = applicationContext.getBean(attrs.bean)
} else {
- throw new IllegalArgumentException("either the [beanName] or [beanType] attribute must be specified to for <g:inject>!")
+ value = attrs.value
+ def containsValue = attrs.containsKey('value')
+ if (!containsValue && body) value = body()
}
- def scope = attrs.scope ? ApplicationTagLib.SCOPES[attrs.scope] : 'pageScope'
- if (!scope) throw new IllegalArgumentException("Invalid [scope] attribute for tag <g:inject>!")
-
- this."$scope"."$var" = bean
+ this."$scope"."$var" = value
null
}
View
16 ...e-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/ApplicationTagLibTests.groovy
@@ -188,24 +188,18 @@ class ApplicationTagLibTests extends AbstractGrailsTagTests {
def template = '<g:set var="var1" value="1"/>${var1}<g:set var="var1" value="2"/> ${var1}<g:set var="var2" value="3" scope="request"/> ${var2}<g:set var="var2" value="4" scope="request"/> ${var2}'
assertOutputEquals('1 2 3 4', template)
}
-
- void testInjectTagByName() {
- def template = '<g:inject beanName="grailsApplication"/>${grailsApplication.initialised}'
- assertOutputEquals('true', template)
- template = '<g:inject beanName="grailsApplication" var="myVar"/>${myVar.initialised}'
+ void testSetTagWithBeanName() {
+ def template = '<g:set bean="grailsApplication" var="myVar"/>${myVar.initialised}'
assertOutputEquals('true', template)
- template = '<g:inject beanName="grailsApplication" var="myRequestVar" scope="request"/>${request.myRequestVar.initialised}'
+ template = '<g:set bean="grailsApplication" var="myRequestVar" scope="request"/>${request.myRequestVar.initialised}'
assertOutputEquals('true', template)
}
- void testInjectTagByType() {
- def template = '<%@ page import="org.codehaus.groovy.grails.commons.*" %><g:inject beanType="${GrailsApplication}" var="myVar"/>${myVar.initialised}'
- assertOutputEquals('true', template)
-
- template = '<%@ page import="org.codehaus.groovy.grails.commons.*" %><g:inject beanType="${GrailsApplication}" var="myRequestVar" scope="request"/>${request.myRequestVar.initialised}'
+ void testSetTagWithBeanType() {
+ def template = '<%@ page import="org.codehaus.groovy.grails.commons.*" %><g:set bean="${GrailsApplication}" var="myRequestVar" scope="request"/>${request.myRequestVar.initialised}'
assertOutputEquals('true', template)
}

0 comments on commit 8342a7f

Please sign in to comment.