Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskleeh committed Feb 19, 2019
2 parents d0f0b1a + b7b7de2 commit cfd6e98
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {

provided "org.grails:grails-web-boot"
provided "org.grails:grails-dependencies"
provided 'javax.servlet:javax.servlet-api:3.1.0'
provided "javax.servlet:javax.servlet-api:$servletApiVersion"

compile "org.grails:scaffolding-core:$scaffoldingVersion"

Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ projectVersion=3.0.0.BUILD-SNAPSHOT
#projectVersion=2.2.7
grailsVersion=4.0.0.M1
scaffoldingVersion=2.0.0.RC1
groovyVersion=2.5.4
groovyVersion=2.5.6
cglibNodepVersion=3.2.9
joddWotVersion=3.3.8
servletApiVersion=4.0.1
asciidoc=true
githubSlug=grails-fields-plugin/grails-fields
githubBranch=master
Expand Down
19 changes: 15 additions & 4 deletions grails-app/taglib/grails/plugin/formfields/FormFieldsTagLib.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package grails.plugin.formfields

import grails.core.GrailsApplication
import groovy.transform.CompileStatic
import groovy.xml.MarkupBuilder
import org.apache.commons.lang.StringUtils
Expand All @@ -30,6 +31,7 @@ import org.grails.scaffolding.model.DomainModelService
import org.grails.scaffolding.model.property.Constrained
import org.grails.scaffolding.model.property.DomainPropertyFactory
import org.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.servlet.LocaleResolver

Expand Down Expand Up @@ -59,7 +61,10 @@ class FormFieldsTagLib {
FormFieldsTemplateService formFieldsTemplateService
BeanPropertyAccessorFactory beanPropertyAccessorFactory
DomainPropertyFactory fieldsDomainPropertyFactory
MappingContext grailsDomainClassMappingContext

@Autowired(required = false)
Collection<MappingContext> mappingContexts

DomainModelService domainModelService
LocaleResolver localeResolver
CodecLookup codecLookup
Expand Down Expand Up @@ -221,7 +226,7 @@ class FormFieldsTagLib {
def collection = resolveBean(attrs.remove('collection'))
PersistentEntity domainClass
if (attrs.containsKey('domainClass')) {
domainClass = grailsDomainClassMappingContext.getPersistentEntity((String) attrs.remove('domainClass'))
domainClass = resolveDomainClass((String) attrs.remove('domainClass'))
} else {
domainClass = (collection instanceof Collection) && collection ? resolveDomainClass(collection.iterator().next()) : null
}
Expand All @@ -239,7 +244,7 @@ class FormFieldsTagLib {

out << render(
template: "/templates/_fields/$template",
model: [domainClass: domainClass,
model: attrs + [domainClass: domainClass,
domainProperties: columnProperties,
columnProperties: columnProperties,
collection: collection,
Expand Down Expand Up @@ -523,7 +528,13 @@ class FormFieldsTagLib {
}

private PersistentEntity resolveDomainClass(Class beanClass) {
grailsDomainClassMappingContext.getPersistentEntity(beanClass.name)
resolveDomainClass(beanClass.name)
}

private PersistentEntity resolveDomainClass(String beanClassName) {
mappingContexts.findResult { MappingContext mappingContext ->
mappingContext.getPersistentEntity(beanClassName)
}
}

private List<PersistentProperty> resolvePersistentProperties(PersistentEntity domainClass, Map attrs, boolean list = false) {
Expand Down
17 changes: 15 additions & 2 deletions src/main/docs/ref/Tags/table.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ The template for `<f:table/>` should be in
grails-app/views/templates/_fields/_table.gsp
----

When *theme* is specified, the _\_display_ template will be searched first in *theme*.
but you can have multiple table templated, if you specify the template property.
All templates should still be located in `view/templates/_fields/`, the example below uses 4 different templates for table.

[source]
----
<f:table collection="myList" myProperty="Template: view/templates/_fields/_table.gsp" />
<f:table collection="${demoList}" template="table3" myProperty="Template: view/templates/_fields/_table3.gsp" />
<f:table collection="${demoList}" template="tables/table2" myProperty="Template: in view/templates/_fields/tables/_table2.gsp" />
<f:table collection="${demoList}" template="tables/table" myProperty="Template: view/templates/_fields/tables/_table.gsp" />
----

When *theme* is specified, the _\_display_ template will be searched first in *theme*, but the theme property does *not* directly apply to table.

===== Attributes

Expand All @@ -56,7 +67,9 @@ When *theme* is specified, the _\_display_ template will be searched first in *t

===== Modifying the `_table.gsp` template.

To make you own version of a `f:table` template, the file should be located in `grails-app/views/templates/_fields/_table.gsp`. You can find a starting point for a new file link:https://github.com/grails-fields-plugin/grails-fields/blob/master/grails-app/views/templates/_fields/_table.gsp[on GitHub]
To make you own version of a `f:table` template, the file should be located in `grails-app/views/templates/_fields/_table.gsp`
unless a different location is specified with the template property.
You can find a starting point for a new file link:https://github.com/grails-fields-plugin/grails-fields/blob/master/grails-app/views/templates/_fields/_table.gsp[on GitHub]

====== Model in the template

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ package grails.plugin.formfields

import grails.plugins.Plugin


import grails.validation.ConstraintsEvaluator
import org.grails.scaffolding.model.DomainModelServiceImpl
import org.grails.scaffolding.model.property.DomainPropertyFactoryImpl

class FieldsGrailsPlugin extends Plugin {

static final String CONSTRAINTS_EVALULATOR_BEAN_NAME = 'validateableConstraintsEvaluator'

def grailsVersion = '3.0 > *'

def loadAfter = ['domainClass']

@Override
Closure doWithSpring() {{->
beanPropertyAccessorFactory(BeanPropertyAccessorFactory) {
constraintsEvaluator = ref(ConstraintsEvaluator.BEAN_NAME)
constraintsEvaluator = ref(CONSTRAINTS_EVALULATOR_BEAN_NAME)
proxyHandler = ref('proxyHandler')
fieldsDomainPropertyFactory = ref('fieldsDomainPropertyFactory')
grailsDomainClassMappingContext = ref('grailsDomainClassMappingContext')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class BuildsAccessorFactory extends Specification implements GrailsWebU
def dpf = new DomainPropertyFactoryImpl(grailsDomainClassMappingContext: applicationContext.getBean("grailsDomainClassMappingContext", MappingContext), trimStrings: true, convertEmptyStringsToNull: true)

beanPropertyAccessorFactory(BeanPropertyAccessorFactory) {
constraintsEvaluator = ref('org.grails.beans.ConstraintsEvaluator')
constraintsEvaluator = ref(FieldsGrailsPlugin.CONSTRAINTS_EVALULATOR_BEAN_NAME)
proxyHandler = new DefaultProxyHandler()
grailsDomainClassMappingContext = ref("grailsDomainClassMappingContext")
fieldsDomainPropertyFactory = dpf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grails.plugin.formfields.taglib

import grails.core.support.proxy.DefaultProxyHandler
import grails.plugin.formfields.BeanPropertyAccessorFactory
import grails.plugin.formfields.FieldsGrailsPlugin
import grails.testing.gorm.DataTest
import grails.testing.web.GrailsWebUnitTest
import org.grails.datastore.mapping.model.MappingContext
Expand Down Expand Up @@ -44,7 +45,7 @@ abstract class AbstractFormFieldsTagLibSpec extends Specification implements Gra
domainPropertyFactory = ref('fieldsDomainPropertyFactory')
}
beanPropertyAccessorFactory(BeanPropertyAccessorFactory) {
constraintsEvaluator = ref('org.grails.beans.ConstraintsEvaluator')
constraintsEvaluator = ref(FieldsGrailsPlugin.CONSTRAINTS_EVALULATOR_BEAN_NAME)
proxyHandler = new DefaultProxyHandler()
grailsDomainClassMappingContext = ref("grailsDomainClassMappingContext")
fieldsDomainPropertyFactory = ref('fieldsDomainPropertyFactory')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,13 @@ class TableSpec extends AbstractFormFieldsTagLibSpec implements TagLibUnitTest<F
then:
table.thead.tr.th.collect { it.text().trim() } == columns
}

@Issue('https://github.com/grails-fields-plugin/grails-fields/issues/286')
void "table should pass extra properties to template"() {
given:
views["/templates/_fields/_table.gsp"] = '<table>${foo}</table>'

expect:
applyTemplate('<f:table collection="personList" foo="bar"/>', [personList: personList]) == '<table>bar</table>'
}
}

0 comments on commit cfd6e98

Please sign in to comment.