I have a bunch of model classes in my Grails app, all of which are for the purpose of dynamic scaffolding of their respective database tables. For the index of the app, I'd like to have a menu of all these scaffoldings so that if a new model is added, the menu is updated.
Is there any automatic, Grails way of doing this or am I stuck with creating a naive index view with a bunch of g:link
's statically typed out for each class to take the user to their respective CRUD views?
As an extension to Joshua's answer, the following should get you a list of scaffolded controllers, in grails 3 at least.
<g:each var="c" in="${grailsApplication.controllerClasses.findAll{ it.isReadableProperty('scaffold') } }">
<li><g:link controller="${c.logicalPropertyName}">${c.fullName}</g:link></li>
</g:each>
EDIT
As requested in the comments, to get the table name you'll need access to the sessionFactory which you'll need to inject into a controller, something like the following will get you a map of domain name to domain table name.
Controller
class YourController {
def sessionFactory
def index() {
def scaffoldedTables = grailsApplication.controllerClasses.findAll{ it.isReadableProperty( 'scaffold' ) }.collectEntries {
[(it.name): sessionFactory.getClassMetadata(it.getPropertyValue( 'scaffold', Object.class )).tableName]
}
[scaffoldedTables: scaffoldedTables]
}
}
gsp
<g:each var="c" in="${scaffoldedTables}">
<li><g:link controller="${c.key}">${c.value}</g:link></li>
</g:each>