Grails natural names - GRAILS-564
Saturday, November 15th, 2008 | grails
True to my pledge I’ve dived straight into the Grails source code, and the project’s issue tracker.
To start with I picked an improvement classed as ‘Trivial’ which I thought just about matched my level of Grails source code knowledge.
GRAILS-564 - “Have scaffolding display friendly names instead of camel case class names”
With my own use of Grails I’d seen that class properties were now being displayed with ‘friendly names’.
For example a domain class such as:
-
class ManyWords {
-
String LotsOfWords
-
}
The property LotsOfWords is defaultly displayed in the scaffolding as “Lots Of Words”, however the class name was still being displayed as the short code “ManyWords” instead of “Many Words”.
So to the fix.
I traced the scaffolding creation to the class DefaultGrailsTemplateGenerator. This class is responsible for the creation of the default scaffolding views and controllers. Its in this class where a map of values is passed to the templates in “src/grails/templates/scaffolding/” when you call a command like “grails generate-all <classname>”. Here is where I was going to make my change.
-
def binding = [packageName: packageName,
-
domainClass: domainClass,
-
multiPart: multiPart,
-
naturalClassName:domainClass.naturalName,
-
shortClassName:domainClass.shortName,
-
propertyName: getPropertyName(domainClass),
-
renderEditor: renderEditor,
-
-
t.make(binding).writeTo(out)
I passed in a new property naturalClassName, to the view templates and replaced it with the
previous place holder where it was appropriate. The natural name is created using the method:
-
GrailsClassUtils.getNaturalName(string)