Skip to content

Instantly share code, notes, and snippets.

@biancarosa
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save biancarosa/135b9f7ffc7acccc0a75 to your computer and use it in GitHub Desktop.
Save biancarosa/135b9f7ffc7acccc0a75 to your computer and use it in GitHub Desktop.
Implementing interfaces on Grails Domains
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
}
}
@biancarosa
Copy link
Author

Vai ficar uma query do tipo:
"from SomeBuyableObject sbo, AnotherBuyableObject abo"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment