Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix for GRAILS-8072 "Improve default toString() output for domain cla…

…sses"
  • Loading branch information...
commit e07b78af55827afa2cc74174582cacc1c6d31d7c 1 parent 024063d
@graemerocher graemerocher authored
View
14 ...roovy/org/codehaus/groovy/grails/compiler/injection/DefaultGrailsDomainClassInjector.java
@@ -31,14 +31,7 @@
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.PropertyNode;
-import org.codehaus.groovy.ast.expr.ClassExpression;
-import org.codehaus.groovy.ast.expr.ConstantExpression;
-import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.ast.expr.GStringExpression;
-import org.codehaus.groovy.ast.expr.ListExpression;
-import org.codehaus.groovy.ast.expr.MapEntryExpression;
-import org.codehaus.groovy.ast.expr.MapExpression;
-import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.ast.expr.*;
import org.codehaus.groovy.ast.stmt.ReturnStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.classgen.GeneratorContext;
@@ -213,9 +206,10 @@ private void injectToStringMethod(ClassNode classNode) {
classNode, "toString", classesWithInjectedToString);
if (!hasToString && !isEnum(classNode)) {
- GStringExpression ge = new GStringExpression(classNode.getName() + " : ${id}");
+ GStringExpression ge = new GStringExpression(classNode.getName() + " : ${id ? id : '(unsaved)'}");
ge.addString(new ConstantExpression(classNode.getName() + " : "));
- ge.addValue(new VariableExpression("id"));
+ VariableExpression idVariable = new VariableExpression("id");
+ ge.addValue(new TernaryExpression(new BooleanExpression(idVariable), idVariable, new ConstantExpression("(unsaved)")));
Statement s = new ReturnStatement(ge);
MethodNode mn = new MethodNode("toString", Modifier.PUBLIC, new ClassNode(String.class), new Parameter[0], new ClassNode[0], s);
classNode.addMethod(mn);
View
3  ...er/src/test/groovy/org/codehaus/groovy/grails/plugins/DomainClassGrailsPluginTests.groovy
@@ -70,6 +70,9 @@ class Parent2 {
void testToString() {
def instance = appCtx.getBean("ChildDomainClass").newInstance()
+
+ assertEquals('Child : (unsaved)', instance.toString())
+
instance.id = 1
assertEquals('Child : 1', instance.toString())
Please sign in to comment.
Something went wrong with that request. Please try again.