Skip to content

Commit

Permalink
Allow setting the default blacklist via properties
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Jan 13, 2025
1 parent 8ad65b0 commit aa7abb3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
23 changes: 18 additions & 5 deletions grails-app/taglib/grails/plugin/formfields/FormFieldsTagLib.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ class FormFieldsTagLib {
@Value('${grails.plugin.fields.localizeNumbers:true}')
Boolean localizeNumbers

@Value('${grails.plugin.fields.exclusions.list:#{T(java.util.Arrays).asList("id", "dateCreated", "lastUpdated")}}')
List<String> exclusionsList
@Value('${grails.plugin.fields.exclusions.input:#{T(java.util.Arrays).asList("version", "dateCreated", "lastUpdated")}}')
List<String> exclusionsInput
@Value('${grails.plugin.fields.exclusions.display:#{T(java.util.Arrays).asList("version", "dateCreated", "lastUpdated")}}')
List<String> exclusionsDisplay

enum ExclusionType {
List, Display, Inout
}

FormFieldsTemplateService formFieldsTemplateService
BeanPropertyAccessorFactory beanPropertyAccessorFactory
DomainPropertyFactory fieldsDomainPropertyFactory
Expand Down Expand Up @@ -355,7 +366,7 @@ class FormFieldsTagLib {
if (domainClass) {
String template = attrs.remove('template') ?: 'list'

List properties = resolvePersistentProperties(domainClass, attrs)
List properties = resolvePersistentProperties(domainClass, attrs, ExclusionType.Display)
out << render(template: "/templates/_fields/$template", model: attrs + [domainClass: domainClass, domainProperties: properties]) { prop ->
BeanPropertyAccessor propertyAccessor = resolveProperty(bean, prop.name)
Map model = buildModel(propertyAccessor, attrs, 'HTML')
Expand Down Expand Up @@ -448,7 +459,7 @@ class FormFieldsTagLib {
} else if (attrs.containsKey('properties')) {
return getList(attrs.remove('properties'))
} else {
List<String> properties = resolvePersistentProperties(domainClass, attrs, true)*.name
List<String> properties = resolvePersistentProperties(domainClass, attrs, ExclusionType.List)*.name
int maxProperties = attrs.containsKey('maxProperties') ? attrs.remove('maxProperties').toInteger() : 7
if (maxProperties && properties.size() > maxProperties) {
properties = properties[0..<maxProperties]
Expand Down Expand Up @@ -578,9 +589,10 @@ class FormFieldsTagLib {
}
}

private List<PersistentProperty> resolvePersistentProperties(PersistentEntity domainClass, Map attrs, boolean list = false) {
private List<PersistentProperty> resolvePersistentProperties(PersistentEntity domainClass, Map attrs, ExclusionType exclusionType = ExclusionType.Inout) {
List<PersistentProperty> properties

boolean list = exclusionType == ExclusionType.List
if (attrs.order) {
def orderBy = getList(attrs.order)
if (attrs.except) {
Expand All @@ -590,9 +602,10 @@ class FormFieldsTagLib {
fieldsDomainPropertyFactory.build(domainClass.getPropertyByName(propertyName))
}
} else {
properties = list ? domainModelService.getListOutputProperties(domainClass) : domainModelService.getInputProperties(domainClass)
properties = list ? domainModelService.getListOutputProperties(domainClass) : domainModelService.getInputProperties(domainClass,
exclusionType == ExclusionType.Inout? exclusionsInput : exclusionsDisplay)
// If 'except' is not set, but 'list' is, exclude 'id', 'dateCreated' and 'lastUpdated' by default
List<String> blacklist = attrs.containsKey('except') ? getList(attrs.except) : (list ? ['id', 'dateCreated', 'lastUpdated'] : [])
List<String> blacklist = attrs.containsKey('except') ? getList(attrs.except) : (list ? exclusionsList : [])

properties.removeAll { it.name in blacklist }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface DomainModelService {
* @param domainClass The persistent entity
*/
List<DomainProperty> getInputProperties(PersistentEntity domainClass)
List<DomainProperty> getInputProperties(PersistentEntity domainClass, List blackList)

/**
* The list of {@link DomainProperty} instances that are to be visible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ class DomainModelServiceImpl implements DomainModelService {
* @see {@link DomainModelServiceImpl#getProperties}
* @param domainClass The persistent entity
*/
List<DomainProperty> getInputProperties(PersistentEntity domainClass) {
getProperties(domainClass, ['version', 'dateCreated', 'lastUpdated'])
List<DomainProperty> getInputProperties(PersistentEntity domainClass, List blackList = ['version', 'dateCreated', 'lastUpdated']) {
getProperties(domainClass, blackList)
}

/**
Expand Down

0 comments on commit aa7abb3

Please sign in to comment.