Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changes to resources plugin integration

The decision was to integrate g:javascript library and inline with r:script if present, and show the user if they miss a call to r:layoutResources. This implements that and moves away from a resourceService to grailsResourceProcessor bean. Requires use of resources 1.1 at runtime. Relates to GRAILS-7703
  • Loading branch information...
commit 64fef9230e33077c8cd3cf1f6d4ec0bdee939810 1 parent ca0ca87
@marcpalmer marcpalmer authored
View
10 grails-plugin-gsp/src/main/groovy/org/codehaus/groovy/grails/plugins/web/taglib/ApplicationTagLib.groovy
@@ -48,8 +48,6 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
@Autowired
LinkGenerator linkGenerator
- def resourceService
-
static final SCOPES = [page: 'pageScope',
application: 'servletContext',
request:'request',
@@ -58,6 +56,10 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
boolean useJsessionId = false
+ private boolean hasResourceProcessor() {
+ grailsApplication.mainContext.containsBean('grailsResourceProcessor')
+ }
+
void afterPropertiesSet() {
def config = applicationContext.getBean(GrailsApplication.APPLICATION_ID).config
if (config.grails.views.enable.jsessionid instanceof Boolean) {
@@ -148,7 +150,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
}
// Use resources plugin if present, but only if file is specified - resources require files
// But users often need to link to a folder just using dir
- out << ((resourceService && attrs.file) ? r.resource(attrs) : linkGenerator.resource(attrs))
+ out << ((hasResourceProcessor() && attrs.file) ? r.resource(attrs) : linkGenerator.resource(attrs))
}
/**
@@ -162,7 +164,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
if (!attrs.uri && !attrs.dir) {
attrs.dir = "images"
}
- if (resourceService) {
+ if (hasResourceProcessor()) {
out << r.img(attrs)
} else {
def uri = attrs.uri ?: resource(attrs)
View
19 grails-plugin-gsp/src/main/groovy/org/codehaus/groovy/grails/plugins/web/taglib/JavascriptTagLib.groovy
@@ -39,8 +39,6 @@ class JavascriptTagLib {
GrailsPluginManager pluginManager
- def resourceService
-
Class<JavascriptProvider> defaultProvider
JavascriptTagLib() {
@@ -54,6 +52,11 @@ class JavascriptTagLib {
}
}
}
+
+ private boolean hasResourcesProcessor() {
+ grailsApplication.mainContext.containsBean('grailsResourceProcessor')
+ }
+
/**
* Includes a javascript src file, library or inline script
* if the tag has no 'src' or 'library' attributes its assumed to be an inline script:<br/>
@@ -84,7 +87,7 @@ class JavascriptTagLib {
javascriptInclude(attrs)
}
else if (attrs.library) {
- if (resourceService) {
+ if (hasResourcesProcessor()) {
out << r.require(module:attrs.library)
includedLibrary(attrs.library)
} else {
@@ -111,9 +114,13 @@ class JavascriptTagLib {
}
}
else {
- out.println '<script type="text/javascript">'
- out.println body()
- out.println '</script>'
+ if (hasResourcesProcessor()) {
+ out << r.script(Collections.EMPTY_MAP, body)
+ } else {
+ out.println '<script type="text/javascript">'
+ out.println body()
+ out.println '</script>'
+ }
}
}
View
3  grails-test-suite-base/src/main/groovy/org/codehaus/groovy/grails/web/taglib/AbstractGrailsTagTests.groovy
@@ -180,6 +180,8 @@ abstract class AbstractGrailsTagTests extends GroovyTestCase {
ctx.registerMockBean("grailsApplication",grailsApplication)
ctx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService())
+ onInitMockBeans()
+
def dependantPluginClasses = []
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.CoreGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.CodecsGrailsPlugin")
@@ -250,6 +252,7 @@ abstract class AbstractGrailsTagTests extends GroovyTestCase {
protected void onInit() {}
protected void onDestroy() {}
+ protected void onInitMockBeans() {}
protected MockServletContext createMockServletContext() { new MockServletContext() }
View
72 ...t-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/ApplicationTagLibResourcesTests.groovy
@@ -0,0 +1,72 @@
+package org.codehaus.groovy.grails.web.taglib
+
+import grails.util.GrailsUtil
+
+import javax.servlet.http.Cookie
+
+import groovy.mock.interceptor.StubFor
+
+import org.codehaus.groovy.grails.commons.ConfigurationHolder
+import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
+import org.codehaus.groovy.grails.web.pages.GroovyPageBinding
+import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
+import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
+
+import org.codehaus.groovy.grails.commons.TagLibArtefactHandler
+import org.springframework.mock.web.MockHttpServletResponse
+
+class ApplicationTagLibResourcesTests extends AbstractGrailsTagTests {
+
+ void onInitMockBeans() {
+ grailsApplication.mainContext.registerMockBean('grailsResourceProcessor', [something:'value'])
+ }
+
+ def replaceMetaClass(Object o) {
+ def old = o.metaClass
+
+ // Create a new EMC for the class and attach it.
+ def emc = new ExpandoMetaClass(o.class, true, true)
+ emc.initialize()
+ o.metaClass = emc
+
+ return old
+ }
+
+ void testResourceTagDirOnlyWithResourcesHooks() {
+ request.contextPath = '/test'
+ def template = '${resource(dir:"jquery")}'
+
+ def taglib = appCtx.getBean(ApplicationTagLib.class.name)
+ def oldMC = replaceMetaClass(taglib)
+
+ // Dummy r.resource impl
+ def mockRes = [
+ resource: { attrs -> "WRONG"}
+ ]
+ taglib.metaClass.getR = { -> mockRes }
+ try {
+ assertOutputEquals '/test/jquery', template
+ } finally {
+ taglib.metaClass = oldMC
+ }
+ }
+
+ void testResourceTagDirAndFileWithResourcesHooks() {
+ request.contextPath = '/test'
+ def template = '${resource(dir:"jquery", file:"jqtest.js")}'
+
+ def taglib = appCtx.getBean(ApplicationTagLib.class.name)
+ def oldMC = replaceMetaClass(taglib)
+
+ // Dummy r.resource impl
+ def mockRes = [
+ resource: { attrs -> "RESOURCES:${attrs.dir}/${attrs.file}" }
+ ]
+ taglib.metaClass.getR = { -> mockRes }
+ try {
+ assertOutputEquals 'RESOURCES:jquery/jqtest.js', template
+ } finally {
+ taglib.metaClass = oldMC
+ }
+ }
+}
View
53 grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/ApplicationTagLibTests.groovy
@@ -54,63 +54,12 @@ class ApplicationTagLibTests extends AbstractGrailsTagTests {
assertOutputEquals '/test/images/foo.jpg', template
}
- def replaceMetaClass(Object o) {
- def old = o.metaClass
-
- // Create a new EMC for the class and attach it.
- def emc = new ExpandoMetaClass(o.class, true, true)
- emc.initialize()
- o.metaClass = emc
-
- return old
- }
-
void testResourceTagDirOnly() {
request.contextPath = '/test'
def template = '${resource(dir:"jquery")}'
assertOutputEquals '/test/jquery', template
}
- void testResourceTagDirOnlyWithResourcesHooks() {
- request.contextPath = '/test'
- def template = '${resource(dir:"jquery")}'
-
- def taglib = appCtx.getBean(ApplicationTagLib.class.name)
- def oldMC = replaceMetaClass(taglib)
-
- // Dummy r.resource impl
- def mockRes = [
- resource: { attrs -> "WRONG"}
- ]
- taglib.metaClass.getR = { -> mockRes }
- taglib.metaClass.getResourceService = { -> [something:'value'] }
- try {
- assertOutputEquals '/test/jquery', template
- } finally {
- taglib.metaClass = oldMC
- }
- }
-
- void testResourceTagDirAndFileWithResourcesHooks() {
- request.contextPath = '/test'
- def template = '${resource(dir:"jquery", file:"jqtest.js")}'
-
- def taglib = appCtx.getBean(ApplicationTagLib.class.name)
- def oldMC = replaceMetaClass(taglib)
-
- // Dummy r.resource impl
- def mockRes = [
- resource: { attrs -> "RESOURCES:${attrs.dir}/${attrs.file}" }
- ]
- taglib.metaClass.getR = { -> mockRes }
- taglib.metaClass.getResourceService = { -> [something:'value'] }
- try {
- assertOutputEquals 'RESOURCES:jquery/jqtest.js', template
- } finally {
- taglib.metaClass = oldMC
- }
- }
-
void testUseJessionIdWithCreateLink() {
def response = new JsessionIdMockHttpServletResponse()
ApplicationTagLib.metaClass.getResponse = {-> response}
@@ -128,7 +77,7 @@ class ApplicationTagLibTests extends AbstractGrailsTagTests {
assertOutputEquals "/foo/test", template
}
- void testObtainCookieValue() {
+ void testObtainCookieValue() {
def cookie = new Cookie("foo", "bar")
request.cookies = [cookie] as Cookie[]
View
89 ...st-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/JavascriptTagLibResourcesTests.groovy
@@ -0,0 +1,89 @@
+package org.codehaus.groovy.grails.web.taglib
+
+import grails.util.GrailsUtil
+
+import org.codehaus.groovy.grails.commons.UrlMappingsArtefactHandler
+import org.codehaus.groovy.grails.plugins.web.taglib.JavascriptProvider
+import org.codehaus.groovy.grails.plugins.web.taglib.JavascriptTagLib
+import org.codehaus.groovy.grails.support.MockStringResourceLoader
+import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
+import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
+import org.springframework.web.util.WebUtils
+import org.codehaus.groovy.grails.web.pages.GroovyPageBinding
+import org.codehaus.groovy.grails.commons.TagLibArtefactHandler
+
+class JavascriptTagLibResourcesTests extends AbstractGrailsTagTests {
+
+ def replaceMetaClass(Object o) {
+ def old = o.metaClass
+
+ // Create a new EMC for the class and attach it.
+ def emc = new ExpandoMetaClass(o.class, true, true)
+ emc.initialize()
+ o.metaClass = emc
+
+ return old
+ }
+
+ void onInitMockBeans() {
+ grailsApplication.mainContext.registerMockBean('grailsResourceProcessor', [something:'value'])
+ }
+
+ /**
+ * Tests that the INCLUDED_JS_LIBRARIES attribute is set correctly without resources plugin
+ */
+ void testLibraryAttributeSetWhenResourcesPluginInstalled() {
+ def template = '<g:javascript library="testing"/>'
+
+ def taglib = appCtx.getBean(JavascriptTagLib.class.name)
+ def oldMC = replaceMetaClass(taglib)
+
+ def requiredModule
+
+ // Dummy r.resource impl
+ def mockRes = [
+ require: { attrs -> requiredModule = attrs.module; return '' }
+ ]
+ taglib.metaClass.getR = { -> mockRes }
+ try {
+
+ def result = applyTemplate(template, [:])
+
+ println "Result: $result"
+ assertEquals(['testing'], request.getAttribute("org.codehaus.grails.INCLUDED_JS_LIBRARIES"))
+ assertEquals 0, result.trim().size()
+ assertEquals 'testing', requiredModule
+
+ } finally {
+ taglib.metaClass = oldMC
+ }
+ }
+
+ /**
+ * Tests that inline script blocks delegate to r:script
+ */
+ void testScriptTagIsUsedWhenResourcesPluginInstalled() {
+ def template = '<g:javascript>var i = 999;</g:javascript>'
+
+ def taglib = appCtx.getBean(JavascriptTagLib.class.name)
+ def oldMC = replaceMetaClass(taglib)
+
+ def scriptFrag
+
+ // Dummy r.script impl
+ def mockRes = [
+ script: { attrs, body -> scriptFrag = body(); return '' }
+ ]
+ taglib.metaClass.getR = { -> mockRes }
+ try {
+
+ def result = applyTemplate(template, [:])
+
+ assertEquals 'var i = 999;', scriptFrag
+
+ } finally {
+ taglib.metaClass = oldMC
+ }
+ }
+
+}
View
32 grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/JavascriptTagLibTests.groovy
@@ -102,38 +102,6 @@ class TestUrlMappings {
}
/**
- * Tests that the INCLUDED_JS_LIBRARIES attribute is set correctly without resources plugin
- */
- void testLibraryAttributeSetWhenResourcesInstalled() {
- def template = '<g:javascript library="testing"/>'
-
- def taglib = appCtx.getBean(JavascriptTagLib.class.name)
- def oldMC = replaceMetaClass(taglib)
-
- def requiredModule
-
- // Dummy r.resource impl
- def mockRes = [
- require: { attrs -> requiredModule = attrs.module; return '' }
- ]
- taglib.metaClass.getR = { -> mockRes }
- taglib.metaClass.getResourceService = { -> [something:'value'] }
- try {
-
- def result = applyTemplate(template, [:])
-
- println "Result: $result"
- assertEquals(['testing'], request.getAttribute("org.codehaus.grails.INCLUDED_JS_LIBRARIES"))
- assertEquals 0, result.trim().size()
- assertEquals 'testing', requiredModule
-
- } finally {
- taglib.metaClass = oldMC
- }
- }
-
-
- /**
* Tests that the 'formRemote' tag complains if a 'params' attribute
* is given.
*/
Please sign in to comment.
Something went wrong with that request. Please try again.