Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

I refactored the grails-resources/src/templates/scaffolding/Controller.groovy to enhance per GRAILS-7187 #205

Closed
wants to merge 2 commits into from

2 participants

@leadVisionary

Converted with${className} to private method. Converted example code to methods from closures.

@leadVisionary leadVisionary reopened this
@jeffbrown
Owner

There are a number of things in this pull request that look like a good idea to me. In any case, if this is going to be pursued, it should be moved to https://github.com/grails-plugins/grails-scaffolding.

@jeffbrown jeffbrown closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 50 deletions.
  1. +39 −50 grails-resources/src/grails/templates/scaffolding/Controller.groovy
View
89 grails-resources/src/grails/templates/scaffolding/Controller.groovy
@@ -29,72 +29,61 @@ class ${className}Controller {
}
def show(Long id) {
- def ${propertyName} = ${className}.get(id)
- if (!${propertyName}) {
- flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
- redirect(action: "list")
- return
+ def aShow = {
+ ${propertyName} -> [${propertyName}: ${propertyName}]
}
-
- [${propertyName}: ${propertyName}]
+ with${className} ( id, aShow)
}
def edit(Long id) {
- def ${propertyName} = ${className}.get(id)
- if (!${propertyName}) {
- flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
- redirect(action: "list")
- return
+ def anEdit = {
+ ${propertyName} -> [${propertyName}: ${propertyName}]
}
-
- [${propertyName}: ${propertyName}]
+ with${className} ( id, anEdit )
}
def update(Long id, Long version) {
- def ${propertyName} = ${className}.get(id)
- if (!${propertyName}) {
- flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
- redirect(action: "list")
- return
- }
-
- if (version != null) {
- if (${propertyName}.version > version) {<% def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className) %>
- ${propertyName}.errors.rejectValue("version", "default.optimistic.locking.failure",
- [message(code: '${domainClass.propertyName}.label', default: '${className}')] as Object[],
- "Another user has updated this ${className} while you were editing")
+ def anUpdate = { ${propertyName} ->
+ if (${propertyName}.version > version) {
+ <% def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className) %>
+ ${propertyName}.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: '${domainClass.propertyName}.label', default: '${className}')] as Object[], "Another user has updated this ${className} while you were editing")
render(view: "edit", model: [${propertyName}: ${propertyName}])
return
}
+ ${propertyName}.properties = params
+ if (!${propertyName}.hasErrors() && ${propertyName}.save(flush: true)) {
+ flash.message = "\${message(code: 'default.updated.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])}"
+ redirect(action: "show", id: ${propertyName}.id)
+ }
+ else {
+ render(view: "edit", model: [${propertyName}: ${propertyName}])
+ }
}
-
- ${propertyName}.properties = params
-
- if (!${propertyName}.save(flush: true)) {
- render(view: "edit", model: [${propertyName}: ${propertyName}])
- return
- }
-
- flash.message = message(code: 'default.updated.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])
- redirect(action: "show", id: ${propertyName}.id)
+ with${className} ( id, anUpdate )
}
def delete(Long id) {
- def ${propertyName} = ${className}.get(id)
- if (!${propertyName}) {
- flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
- redirect(action: "list")
- return
+ def aDelete = { ${propertyName} ->
+ try {
+ ${propertyName}.delete(flush: true)
+ flash.message = "\${message(code: 'default.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
+ redirect(action: "list")
+ }
+ catch (org.springframework.dao.DataIntegrityViolationException e) {
+ flash.message = "\${message(code: 'default.not.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
+ redirect(action: "show", id: id)
+ }
}
-
- try {
- ${propertyName}.delete(flush: true)
- flash.message = message(code: 'default.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
+ with${className} ( id, aDelete )
+ }
+
+ private def with${className}(Long id, Closure c) {
+ def ${propertyName} = ${className}.get(id)
+ if(${propertyName}) {
+ c.call ${propertyName}
+ } else {
+ flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
redirect(action: "list")
}
- catch (DataIntegrityViolationException e) {
- flash.message = message(code: 'default.not.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
- redirect(action: "show", id: id)
- }
}
-}
+}
Something went wrong with that request. Please try again.