Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the default blacklist via properties #394

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, Input
}

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.Input) {
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.Input? 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<String> blackList = null) {
getProperties(domainClass, new ArrayList<>(blackList ?: ['version', 'dateCreated', 'lastUpdated']))
}

/**
Expand Down
Loading