Skip to content

Commit 89b11f1

Browse files
committed
Keep the templates consistent between script commands and generators in scaffolding plugin
Closes gh-1416
1 parent 9171b37 commit 89b11f1

File tree

14 files changed

+376
-371
lines changed

14 files changed

+376
-371
lines changed

grace-plugin-scaffolding/src/main/groovy/org/grails/cli/generator/ScaffoldGenerator.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 the original author or authors.
2+
* Copyright 2022-2026 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ import grails.cli.generator.AbstractGenerator
2121
* Scaffold generator
2222
*
2323
* @author Michael Yan
24-
* @since 6.2.0
24+
* @since 2024.0.0
2525
*/
2626
class ScaffoldGenerator extends AbstractGenerator {
2727

@@ -72,7 +72,7 @@ class ScaffoldGenerator extends AbstractGenerator {
7272
String controllerFile = 'app/controllers/' + defaultPackagePath + '/' + className + 'Controller.groovy'
7373
String controllerSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'ControllerSpec.groovy'
7474
String serviceFile = 'app/services/' + defaultPackagePath + '/' + className + 'Service.groovy'
75-
String serviceSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'ServiceSpec.groovy'
75+
String serviceSpecFile = 'src/integration-test/groovy/' + defaultPackagePath + '/' + className + 'ServiceSpec.groovy'
7676
String createGspFile = 'app/views/' + propertyName + '/' + 'create.gsp'
7777
String editGspFile = 'app/views/' + propertyName + '/' + 'edit.gsp'
7878
String indexGspFile = 'app/views/' + propertyName + '/' + 'index.gsp'

grace-plugin-scaffolding/src/main/resources/META-INF/templates/generators/scaffold/AsyncController.groovy.tpl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<%=packageName ? "package ${packageName}" : ''%>
22

3-
import static org.springframework.http.HttpStatus.*
43
import org.springframework.transaction.TransactionStatus
54

5+
import static org.springframework.http.HttpStatus.*
6+
67
class ${className}Controller {
78
8-
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
9+
static allowedMethods = [save: 'POST', update: 'PUT', delete: 'DELETE']
910
1011
def index(Integer max) {
1112
params.max = Math.min(max ?: 10, 100)
@@ -34,9 +35,9 @@ class ${className}Controller {
3435
return
3536
}
3637

37-
if(${propertyName}.hasErrors()) {
38+
if (${propertyName}.hasErrors()) {
3839
status.setRollbackOnly()
39-
respond ${propertyName}.errors, view:'create' // STATUS CODE 422
40+
respond ${propertyName}.errors, view: 'create' // STATUS CODE 422
4041
return
4142
}
4243

@@ -67,9 +68,10 @@ class ${className}Controller {
6768
}
6869

6970
${propertyName}.properties = params
70-
if( !${propertyName}.save(flush:true) ) {
71+
72+
if (!${propertyName}.save(flush: true)) {
7173
status.setRollbackOnly()
72-
respond ${propertyName}.errors, view:'edit' // STATUS CODE 422
74+
respond ${propertyName}.errors, view: 'edit' // STATUS CODE 422
7375
return
7476
}
7577

@@ -97,7 +99,7 @@ class ${className}Controller {
9799
request.withFormat {
98100
form multipartForm {
99101
flash.message = message(code: 'default.deleted.message', args: [message(code: '${className}.label', default: '${className}'), ${propertyName}.id])
100-
redirect action:"index", method:"GET"
102+
redirect action: 'index', method: 'GET'
101103
}
102104
'*'{ render status: NO_CONTENT }
103105
}
@@ -108,9 +110,10 @@ class ${className}Controller {
108110
request.withFormat {
109111
form multipartForm {
110112
flash.message = message(code: 'default.not.found.message', args: [message(code: '${propertyName}.label', default: '${className}'), params.id])
111-
redirect action: "index", method: "GET"
113+
redirect action: 'index', method: 'GET'
112114
}
113115
'*'{ render status: NOT_FOUND }
114116
}
115117
}
116-
}
118+
119+
}
Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,149 @@
11
<%=packageName ? "package ${packageName}" : ''%>
22

3+
import spock.lang.*
4+
35
import grails.testing.gorm.DomainUnitTest
46
import grails.testing.web.controllers.ControllerUnitTest
5-
import spock.lang.*
67

78
class ${className}ControllerSpec extends Specification implements ControllerUnitTest<${className}Controller>, DomainUnitTest<${className}> {
89
910
def populateValidParams(params) {
1011
assert params != null
11-
assert false, "TODO: Populate valid params"
12+
assert false, 'TODO: Populate valid params'
1213
}
1314

1415
def populateInvalidParams(params) {
1516
assert params != null
16-
assert false, "TODO: Populate invalid params"
17+
assert false, 'TODO: Populate invalid params'
1718
}
1819

19-
void "Test the index action returns the correct model"() {
20+
void 'Test the index action returns the correct model'() {
21+
when: 'The index action is executed'
22+
controller.index().get()
2023
21-
when:"The index action is executed"
22-
controller.index().get()
23-
24-
then:"The model is correct"
25-
!model.${modelName}List
26-
model.${modelName}Count == 0
24+
then: 'The model is correct'
25+
!model.${modelName}List
26+
model.${modelName}Count == 0
2727
}
2828

29-
void "Test the create action returns the correct model"() {
30-
when:"The create action is executed"
31-
controller.create()
29+
void 'Test the create action returns the correct model'() {
30+
when: 'The create action is executed'
31+
controller.create()
3232
33-
then:"The model is correctly created"
34-
model.${modelName}!= null
33+
then: 'The model is correctly created'
34+
model.${modelName}!= null
3535
}
3636

37-
void "Test the save action correctly persists an instance"() {
38-
39-
when:"The save action is executed with an invalid instance"
40-
request.method = "POST"
41-
def ${propertyName}= new ${className}()
42-
${propertyName}.validate()
43-
controller.save(${propertyName}).get()
37+
void 'Test the save action correctly persists an instance'() {
38+
when: 'The save action is executed with an invalid instance'
39+
request.method = 'POST'
40+
def ${propertyName}= new ${className}()
41+
${propertyName}.validate()
42+
controller.save(${propertyName}).get()
4443

45-
then:"The create view is rendered again with the correct model"
46-
model.${modelName}!= null
47-
view == 'create'
44+
then: 'The create view is rendered again with the correct model'
45+
model.${modelName}!= null
46+
view == 'create'
4847

49-
when:"The save action is executed with a valid instance"
50-
response.reset()
51-
populateValidParams(params)
52-
${propertyName}= new ${className}(params)
48+
when: 'The save action is executed with a valid instance'
49+
response.reset()
50+
populateValidParams(params)
51+
${propertyName}= new ${className}(params)
5352

54-
controller.save(${propertyName}).get()
53+
controller.save(${propertyName}).get()
5554

56-
then:"A redirect is issued to the show action"
57-
response.status == 201
58-
${className}.count() == 1
55+
then: 'A redirect is issued to the show action'
56+
response.status == 201
57+
${className}.count() == 1
5958
}
6059

61-
void "Test that the show action returns the correct model"() {
62-
when:"The show action is execu ted with a null domain"
63-
controller.show(null).get()
60+
void 'Test that the show action returns the correct model'() {
61+
when: 'The show action is execu ted with a null domain'
62+
controller.show(null).get()
6463
65-
then:"A 404 error is returned"
66-
response.status == 404
64+
then: 'A 404 error is returned'
65+
response.status == 404
6766
68-
when:"A domain instance is passed to the show action"
69-
populateValidParams(params)
70-
def ${propertyName}= new ${className}(params).save(flush:true)
67+
when: 'A domain instance is passed to the show action'
68+
populateValidParams(params)
69+
def ${propertyName}= new ${className}(params).save(flush:true)
7170

72-
controller.show(${propertyName}.id).get()
71+
controller.show(${propertyName}.id).get()
7372

74-
then:"A model is populated containing the domain instance"
75-
model.${modelName}.id==${propertyName}.id
73+
then: 'A model is populated containing the domain instance'
74+
model.${modelName}.id==${propertyName}.id
7675
}
7776

78-
void "Test that the edit action returns the correct model"() {
79-
when:"The edit action is executed with a null domain"
80-
controller.edit(null).get()
77+
void 'Test that the edit action returns the correct model'() {
78+
when: 'The edit action is executed with a null domain'
79+
controller.edit(null).get()
8180
82-
then:"A 404 error is returned"
83-
response.status == 404
81+
then: 'A 404 error is returned'
82+
response.status == 404
8483
85-
when:"A domain instance is passed to the edit action"
86-
populateValidParams(params)
87-
def ${propertyName} = new ${className}(params).save(flush:true)
88-
controller.edit(${propertyName}?.id).get()
84+
when: 'A domain instance is passed to the edit action'
85+
populateValidParams(params)
86+
def ${propertyName} = new ${className}(params).save(flush:true)
87+
controller.edit(${propertyName}?.id).get()
8988

90-
then:"A model is populated containing the domain instance"
91-
model.${modelName}.id==${propertyName}.id
89+
then: 'A model is populated containing the domain instance'
90+
model.${modelName}.id==${propertyName}.id
9291
}
9392

94-
void "Test the update action performs an update on a valid domain instance"() {
95-
when:"Update is called for a domain instance that doesn't exist"
96-
request.method = "PUT"
97-
controller.update(null).get()
98-
99-
then:"A 404 error is returned"
100-
status == 404
101-
102-
when:"An invalid domain instance is passed to the update action"
103-
response.reset()
104-
populateValidParams(params)
105-
def ${propertyName}= new ${className}(params).save(flush:true)
106-
params.clear()
107-
populateInvalidParams(params)
108-
controller.update(${propertyName}.id).get()
109-
110-
then:"The edit view is rendered again with the invalid instance"
111-
view == 'edit'
112-
model.${modelName}.id==${propertyName}.id
113-
114-
when:"A valid domain instance is passed to the update action"
115-
response.reset()
116-
params.clear()
117-
populateValidParams(params)
118-
controller.update(${propertyName}.id).get()
119-
120-
then:"A redirect is issued to the show action"
121-
${propertyName} != null
122-
response.status == 200
123-
!book.isDirty()
93+
void 'Test the update action performs an update on a valid domain instance'() {
94+
when: "Update is called for a domain instance that doesn't exist"
95+
request.method = 'PUT'
96+
controller.update(null).get()
97+
98+
then: 'A 404 error is returned'
99+
status == 404
100+
101+
when: 'An invalid domain instance is passed to the update action'
102+
response.reset()
103+
populateValidParams(params)
104+
def ${propertyName}= new ${className}(params).save(flush:true)
105+
params.clear()
106+
populateInvalidParams(params)
107+
controller.update(${propertyName}.id).get()
108+
109+
then: 'The edit view is rendered again with the invalid instance'
110+
view == 'edit'
111+
model.${modelName}.id==${propertyName}.id
112+
113+
when: 'A valid domain instance is passed to the update action'
114+
response.reset()
115+
params.clear()
116+
populateValidParams(params)
117+
controller.update(${propertyName}.id).get()
118+
119+
then: 'A redirect is issued to the show action'
120+
${propertyName} != null
121+
response.status == 200
122+
!book.isDirty()
124123
}
125124

126-
void "Test that the delete action deletes an instance if it exists"() {
127-
when:"The delete action is called for a null instance"
128-
request.method = "DELETE"
129-
controller.delete(null).get()
125+
void 'Test that the delete action deletes an instance if it exists'() {
126+
when: 'The delete action is called for a null instance'
127+
request.method = 'DELETE'
128+
controller.delete(null).get()
130129
131-
then:"A 404 is returned"
132-
status == 404
130+
then: 'A 404 is returned'
131+
status == 404
133132
134-
when:"A domain instance is created"
135-
response.reset()
136-
populateValidParams(params)
137-
def ${propertyName}= new ${className}(params).save(flush: true)
133+
when: 'A domain instance is created'
134+
response.reset()
135+
populateValidParams(params)
136+
def ${propertyName}= new ${className}(params).save(flush: true)
138137

139-
then:"It exists"
140-
${className}.count() == 1
138+
then: 'It exists'
139+
${className}.count() == 1
141140

142-
when:"The domain instance is passed to the delete action"
143-
controller.delete(${propertyName}.id).get()
141+
when: 'The domain instance is passed to the delete action'
142+
controller.delete(${propertyName}.id).get()
144143

145-
then:"The instance is deleted"
146-
${className}.count() == 0
147-
response.status == 204
144+
then: 'The instance is deleted'
145+
${className}.count() == 0
146+
response.status == 204
148147
}
149-
}
148+
149+
}

grace-plugin-scaffolding/src/main/resources/META-INF/templates/generators/scaffold/Controller.groovy.tpl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<%=packageName ? "package ${packageName}" : ''%>
22

33
import grails.validation.ValidationException
4+
45
import static org.springframework.http.HttpStatus.*
56

67
class ${className}Controller {
78
89
${className}Service ${propertyName}Service
910

10-
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
11+
static allowedMethods = [save: 'POST', update: 'PUT', delete: 'DELETE']
1112

1213
def index(Integer max) {
1314
params.max = Math.min(max ?: 10, 100)
@@ -31,7 +32,7 @@ class ${className}Controller {
3132
try {
3233
${propertyName}Service.save(${propertyName})
3334
} catch (ValidationException e) {
34-
respond ${propertyName}.errors, view:'create'
35+
respond ${propertyName}.errors, view: 'create'
3536
return
3637
}
3738

@@ -57,7 +58,7 @@ class ${className}Controller {
5758
try {
5859
${propertyName}Service.save(${propertyName})
5960
} catch (ValidationException e) {
60-
respond ${propertyName}.errors, view:'edit'
61+
respond ${propertyName}.errors, view: 'edit'
6162
return
6263
}
6364

@@ -81,7 +82,7 @@ class ${className}Controller {
8182
request.withFormat {
8283
form multipartForm {
8384
flash.message = message(code: 'default.deleted.message', args: [message(code: '${propertyName}.label', default: '${className}'), id])
84-
redirect action:"index", method:"GET"
85+
redirect action: 'index', method: 'GET'
8586
}
8687
'*'{ render status: NO_CONTENT }
8788
}
@@ -91,9 +92,10 @@ class ${className}Controller {
9192
request.withFormat {
9293
form multipartForm {
9394
flash.message = message(code: 'default.not.found.message', args: [message(code: '${propertyName}.label', default: '${className}'), params.id])
94-
redirect action: "index", method: "GET"
95+
redirect action: 'index', method: 'GET'
9596
}
9697
'*'{ render status: NOT_FOUND }
9798
}
9899
}
100+
99101
}

0 commit comments

Comments
 (0)