6

In grails 2.4.x there was template named src/templates/scaffolding/Controller.groovy which was used to generate CRUD actions for dynamic scaffolding like:

class BookController {
        static scaffold = domain.Book
        static responseFormats = ['html', 'json']
}

In grails 3 (3.1.x) altering this file (the one 3.0.x install-templates created one for me) just does not work. Is there any way to alter default behavior of dynamic scaffolding in grails 3(.1)? install-templates creates some src/main/templates/scaffolding/ScaffoldedController.groovy file but its content looks like my BookController class. Documentation says nothing about that.

tstenner
  • 10,080
  • 10
  • 57
  • 92
mkr
  • 73
  • 5

2 Answers2

1

Take a look at the create-script command.

create-script quick ref...

create-script User Guide

That will allow you to create your own script to do code generation, but I do not believe it allows you to get access to GORM domain properties in Grails 3.(1). I actually posted that question my self here: How can I access GORM object properties in a GroovyScriptCommand?

I first ran the install-templates command and then the create-script command, not sure if needed to, but it gave me some limited example templates to look at.

Here's an example of the one I created. I put the println statements in just so that I could see what the different properties on the model are that I have to work with. They feel a little limited because they are all based off of the command line argument that you enter and not an instance of an actual Grails artifact.

src/main/scripts/geta-create-screen-groovy:

import grails.build.logging.ConsoleLogger

description("Creates a GETA scaffolded controller, views, and integration test artifacts") {
    usage 'geta-create-screen [domain name]'
    completer org.grails.cli.interactive.completers.DomainClassCompleter
    argument name:'Controller Name', description:"The name of controller", required:true
    flag name:'force', description:"Whether to overwrite existing files"
}

def model = model(args[0])
def overwrite = flag('force') ? true : false

println "DAC: model.className:..... ${model.className}"
println "DAC: model.fullName:...... ${model.fullName}"
println "DAC: model.propertyName:.. ${model.propertyName}"
println "DAC: model.packageName:... ${model.packageName}"
println "DAC: model.simpleName:.... ${model.simpleName}"
println "DAC: model.lowerCaseName:. ${model.lowerCaseName}"
println "DAC: model.packagePath:... ${model.packagePath}"

render   template: template('scaffolding/EtaController.groovy'),
    destination: file("grails-app/controllers/${model.packagePath}/${model.convention("Controller")}.groovy"),
    model: model,
    overwrite: overwrite

render template: template('scaffolding/EtaDomainObject.groovy'),
    destination: file("grails-app/domain/${model.packagePath}/${model.convention("Domain")}.groovy"),
    model: model,
    overwrite: overwrite

render template: template('scaffolding/EtaService.groovy'),
    destination: file("grails-app/services/${model.packagePath}/${model.convention("Service")}.groovy"),
    model: model,
    overwrite: overwrite

render template: template('scaffolding/EtaGsp.gsp'),
    destination: file("grails-app/views/${model.packagePath}/${model.propertyName}/${model.propertyName}.gsp"),
    model: model,
    overwrite: overwrite

render template: template('scaffolding/EtaGspTabHeader.gsp'),
    destination: file("grails-app/views/${model.packagePath}/${model.propertyName}/_tabHeader.gsp"),
    model: model,
    overwrite: overwrite


return true

The template: src/main/templates/scaffolding/EtaController.groovy

<%=packageName ? "package ${packageName}" : ''%>

class ${className}Controller {

    static scaffold = ${className}

    def index (){
        render view: "${packageName.replaceAll('\\\\', '/')}/${propertyName}/${propertyName}.gsp"
    }

}

To execute the command: grails geta-create-screen my.package.MyClass --force --stacktrace --verbose

Community
  • 1
  • 1
DAC
  • 707
  • 1
  • 6
  • 20
  • Interesting, but this is completely different functionality. My question covered dynamic scaffolding - something which sets up controller at runtime using some bunch of CRUD actions. Command script freezes those actions on generation time and is far more difficult to maintain. If you have to change something, you have to refactor or re-generate every generated controller. In grails 2.x dynamic scaffolding worked like such a re-generation of every controller having `static scaffold` at app compilation, so it was enough to change the template used there, but in grails 3 this doesn't work. – mkr Feb 25 '16 at 07:38
0

I think you are looking for generate-controller.

Sascha Frinken
  • 3,134
  • 1
  • 24
  • 27
  • No. `generate-controller` creates actual file/class for the controller scaffolding particular domain class. Look at the situation when you have dozens of such files and you would like to change some app-wide behavior, for example 'every save action should redirect back to previous controller calling edit action' - dozens files to change. And `templates/Controller.groovy` was the file which templated every dynamic scaffold created on-the-fly by the `static scaffold = domain.class` inside the controller. – mkr Feb 09 '16 at 18:24