Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails3 controller template for dynamic scaffolding

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) alternating this file (probably 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.

like image 272
mkr Avatar asked Feb 08 '16 23:02

mkr


1 Answers

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

like image 181
DAC Avatar answered Oct 15 '22 23:10

DAC