Last active
August 29, 2015 14:24
-
-
Save biancarosa/135b9f7ffc7acccc0a75 to your computer and use it in GitHub Desktop.
Implementing interfaces on Grails Domains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Buyable { | |
def buy() | |
} | |
class UpperClass {} | |
class ChildClass extends UpperClass {} | |
class SomeBuyableObject extends UpperClass implements Buyable { | |
String foo | |
def buy() { //do stuff } | |
} | |
class AnotherBuyableObject extends ChildClass implements Buyable { | |
String bar | |
def buy() { //do stuff } | |
} | |
class MyService { | |
def abbreviate(String name) { | |
return name.collect { Character.isUpperCase(it as Character)?it.toLowerCase():'' }.join() | |
} | |
def listBuyables() { | |
new SomeBuyableObject(foo: 'foo').save() | |
new AnotherBuyableObject(bar : 'bar').save() | |
//doest work | |
// def buyables = Buyable.findAll() // GORM find all | |
def classes = grailsApplication.domainClasses.findAll { | |
Buyable.isAssignableFrom(it.clazz) | |
}.name | |
def tables = classes.each{ it += abbreviate(it) }.join(', ') | |
def query = "from "+tables | |
def buyables = UpperClass.executeQuery(query)[0] | |
assert buyables.size() == 2 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Vai ficar uma query do tipo:
"from SomeBuyableObject sbo, AnotherBuyableObject abo"