Permalink
Browse files

GRAILS-5087: exclude `display:false` properties from scaffolding

Don't include properties in list / show scaffolding if they have a `display:false` constraint. Create & edit forms already exclude them so this makes scaffolding more consistent.
  • Loading branch information...
1 parent 7680138 commit 683df42ebac3d81bb26dbef0240a5742f3d83f6e robfletcher committed Dec 7, 2012
@@ -27,13 +27,21 @@
allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated'
props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && it.type != null && !Collection.isAssignableFrom(it.type) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
- props.eachWithIndex { p, i ->
- if (i < 6) {
- if (p.isAssociation()) { %>
+
+ props.retainAll {
+ def cp = domainClass.constrainedProperties[it.name]
+ (cp ? cp.display : true)
+ }
+ if (props.size() > 6) {
+ props = props[0..<6]
+ }
+
+ props.each { p ->
+ if (p.isAssociation()) { %>
<th><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></th>
- <% } else { %>
+ <% } else { %>
<g:sortableColumn property="${p.name}" title="\${message(code: '${domainClass.propertyName}.${p.name}.label', default: '${p.naturalName}')}" />
- <% } } } %>
+ <% } } %>
</tr>
</thead>
<tbody>
@@ -42,12 +50,12 @@
<% props.eachWithIndex { p, i ->
if (i == 0) { %>
<td><g:link action="show" id="\${${propertyName}.id}">\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</g:link></td>
- <% } else if (i < 6) {
+ <% } else {
if (p.type == Boolean || p.type == boolean) { %>
<td><g:formatBoolean boolean="\${${propertyName}.${p.name}}" /></td>
- <% } else if (p.type == Date || p.type == java.sql.Date || p.type == java.sql.Time || p.type == Calendar) { %>
+ <% } else if (p.type == Date || p.type == java.sql.Date || p.type == java.sql.Time || p.type == Calendar) { %>
<td><g:formatDate date="\${${propertyName}.${p.name}}" /></td>
- <% } else { %>
+ <% } else { %>
<td>\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</td>
<% } } } %>
</tr>
@@ -26,7 +26,9 @@
allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated'
props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
- props.each { p -> %>
+ props.each { p ->
+ def cp = domainClass.constrainedProperties[p.name]
+ if (cp ? cp.display : true) { %>
<g:if test="\${${propertyName}?.${p.name}}">
<li class="fieldcontain">
<span id="${p.name}-label" class="property-label"><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></span>
@@ -47,7 +49,7 @@
<% } %>
</li>
</g:if>
- <% } %>
+ <% } } %>
</ol>
<g:form>
<fieldset class="buttons">
@@ -1,5 +1,7 @@
package org.codehaus.groovy.grails.scaffolding
+import org.codehaus.groovy.grails.commons.GrailsDomainClass
+
import static org.junit.Assert.assertThat
import static org.junit.matchers.JUnitMatchers.containsString
@@ -11,7 +13,6 @@ import org.codehaus.groovy.grails.commons.GrailsDomainConfigurationUtil
import org.codehaus.groovy.grails.plugins.GrailsPlugin
import org.codehaus.groovy.grails.plugins.MockGrailsPluginManager
-import org.codehaus.groovy.grails.plugins.PluginManagerHolder
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder
/**
@@ -44,23 +45,20 @@ class ScaffoldingTest {
Integer status
Date regularDate
java.sql.Date sqlDate
+ String secret
static constraints = {
status inList:[1,2,3,4]
+ secret display: false
}
}
'''
void testGenerateDateSelect() {
def templateGenerator = new DefaultGrailsTemplateGenerator(basedir:"../grails-resources")
templateGenerator.pluginManager = pluginManager
- gcl.parseClass(testDomain)
- def testClass = gcl.loadClass("ScaffoldingTest")
- def constrainedProperties = GrailsDomainConfigurationUtil.evaluateConstraints(testClass)
- testClass.metaClass.getConstraints = {-> constrainedProperties }
-
- def domainClass = new DefaultGrailsDomainClass(testClass)
+ def domainClass = loadDomainClass(testDomain)
StringWriter sw = new StringWriter()
templateGenerator.generateView domainClass, "_form", sw
@@ -72,13 +70,8 @@ class ScaffoldingTest {
void testGenerateNumberSelect() {
def templateGenerator = new DefaultGrailsTemplateGenerator(basedir:"../grails-resources")
templateGenerator.pluginManager = pluginManager
- gcl.parseClass(testDomain)
- def testClass = gcl.loadClass("ScaffoldingTest")
- def constrainedProperties = GrailsDomainConfigurationUtil.evaluateConstraints(testClass)
- testClass.metaClass.getConstraints = {-> constrainedProperties }
-
- def domainClass = new DefaultGrailsDomainClass(testClass)
+ def domainClass = loadDomainClass(testDomain)
StringWriter sw = new StringWriter()
templateGenerator.generateView domainClass, "_form", sw
@@ -91,11 +84,8 @@ class ScaffoldingTest {
void testDoesNotGenerateInputForId() {
def templateGenerator = new DefaultGrailsTemplateGenerator(basedir:"../grails-resources")
templateGenerator.pluginManager = pluginManager
- gcl.parseClass(testDomain)
- def testClass = gcl.loadClass("ScaffoldingTest")
- def domainClass = new DefaultGrailsDomainClass(testClass)
- def constrainedProperties = GrailsDomainConfigurationUtil.evaluateConstraints(testClass)
- testClass.metaClass.getConstraints = {-> constrainedProperties }
+
+ def domainClass = loadDomainClass(testDomain)
def sw = new StringWriter()
templateGenerator.generateView domainClass, "_form", sw
@@ -106,7 +96,8 @@ class ScaffoldingTest {
void testGeneratesInputForAssignedId() {
def templateGenerator = new DefaultGrailsTemplateGenerator(basedir:"../grails-resources")
templateGenerator.pluginManager = pluginManager
- gcl.parseClass('''
+
+ def domainClass = loadDomainClass '''
import grails.persistence.*
@Entity
@@ -116,13 +107,9 @@ class ScaffoldingTest {
id generator: "assigned"
}
}
- ''')
- def testClass = gcl.loadClass("ScaffoldingTest")
- def domainClass = new DefaultGrailsDomainClass(testClass)
- def constrainedProperties = GrailsDomainConfigurationUtil.evaluateConstraints(testClass)
- testClass.metaClass.getConstraints = {-> constrainedProperties }
- GrailsDomainBinder.evaluateMapping(domainClass)
+ '''
+ GrailsDomainBinder.evaluateMapping(domainClass)
assert GrailsDomainBinder.getMapping(domainClass)?.identity?.generator == 'assigned'
def sw = new StringWriter()
@@ -132,4 +119,49 @@ class ScaffoldingTest {
sw.toString(),
containsString('g:textField name="id" value="${scaffoldingTestInstance?.id}"')
}
+
+ void testDisplayFalsePropertyIsNotRenderedInForm() {
+ def templateGenerator = new DefaultGrailsTemplateGenerator(basedir: "../grails-resources")
+ templateGenerator.pluginManager = pluginManager
+
+ def domainClass = loadDomainClass(testDomain)
+
+ def sw = new StringWriter()
+ templateGenerator.generateView domainClass, "_form", sw
+
+ assert !sw.toString().contains('name="secret"'), "Should not have rendered an input for the field with display:false constraint"
+ }
+
+ void testDisplayFalsePropertyIsNotRenderedInListPage() {
+ def templateGenerator = new DefaultGrailsTemplateGenerator(basedir: "../grails-resources")
+ templateGenerator.pluginManager = pluginManager
+
+ def domainClass = loadDomainClass(testDomain)
+
+ def sw = new StringWriter()
+ templateGenerator.generateView domainClass, "list", sw
+
+ assert !sw.toString().contains('Secret'), "Should not have rendered a column for the field with display:false constraint"
+ }
+
+ void testDisplayFalsePropertyIsNotRenderedInShowPage() {
+ def templateGenerator = new DefaultGrailsTemplateGenerator(basedir: "../grails-resources")
+ templateGenerator.pluginManager = pluginManager
+
+ def domainClass = loadDomainClass(testDomain)
+
+ def sw = new StringWriter()
+ templateGenerator.generateView domainClass, "show", sw
+
+ assert !sw.toString().contains('Secret'), "Should not have rendered the field with display:false constraint"
+ }
+
+ private GrailsDomainClass loadDomainClass(String testDomain) {
+ gcl.parseClass(testDomain)
+ def testClass = gcl.loadClass("ScaffoldingTest")
+ def domainClass = new DefaultGrailsDomainClass(testClass)
+ def constrainedProperties = GrailsDomainConfigurationUtil.evaluateConstraints(testClass)
+ testClass.metaClass.getConstraints = {-> constrainedProperties }
+ domainClass
+ }
}

0 comments on commit 683df42

Please sign in to comment.