I have the following Grails domain class:
class Product {
String name
Float basePrice
Category category
String image = "default.jpg"
static constraints = {
name(size:3..25, blank:false)
basePrice(scale:2, nullable:false)
category(inList:Category.list(), nullable:false)
image(blank:false)
}
}
From a controller, I want to get the default value for the image property (in this case "default.jpg"). Something like this:
def productInstance = new Product(params)
productInstance.image = getProductPicturePath() ?: Product().image
getProductPicturePath returns an image path, but in case no image was submitted, the controller shall replace the null value with the default. While I could certainly write something like this:
productInstance.image = getProductPicturePath() ?: "default.jpg"
It's certainly not very DRY, and I would prefer to keep that default value in one place. How can I achieve this?