Skip to content

Instantly share code, notes, and snippets.

View sergiomichels's full-sized avatar

Sérgio Michels sergiomichels

View GitHub Profile
@sergiomichels
sergiomichels / version
Created October 4, 2012 00:47
Get application version in runtime
def version = grailsApplication.metadata['app.version']
@sergiomichels
sergiomichels / GetGrailsApplication.groovy
Created October 4, 2012 00:54
Get GrailsApplication
def grailsApplication = Holders.getGrailsApplication()
@sergiomichels
sergiomichels / MyTagLib.groovy
Created October 4, 2012 02:46
TagLib that uses Grails tags
class MyTagLib {
static namespace = "my"
def input = { attrs, body ->
//some business logic here
//you can access grails tags in the namespace g
out << g.textField(attrs, body)
}
@sergiomichels
sergiomichels / MyExtendedTagLib.groovy
Created October 4, 2012 02:52
TagLib that overwrite tag and call original
class MyExtendedTagLib {
static namespace = "my"
def grailsApplication
def input = {attrs, body ->
//some custom logic that changes attrs here...
//get the original TagLib, must be the complete path
def myTagLib = grailsApplication.mainContext.getBean("packages.MyTagLib")
@sergiomichels
sergiomichels / CompositeDomainClass.groovy
Created October 4, 2012 03:12
Example of using composite primary key
class CompositeDomainClass implements Serializable {
//this is part of the pk
MyOtherDomainClass otherDomainClass
//other fields here...
//and we try to map this
static mapping {
table 'composite_table'
id composite: ['id','otherDomainClass'], column: 'my_code_field'
otherDomainClass(column: 'other_table_id')
}
@sergiomichels
sergiomichels / CompositeDomainClass.groovy
Created October 4, 2012 03:15
Adjusts in CompositeDomainClass to make it work
class CompositeDomainClass implements Serializable {
//this is part of the pk
MyOtherDomainClass otherDomainClass
//other fields here...
//and we try to map this
static mapping {
table 'composite_table'
//id composite: ['id','otherDomainClass'], column: 'my_code_field'
id composite: ['id','otherDomainClass']
columns {
@sergiomichels
sergiomichels / MyGroovyClass.groovy
Created October 4, 2012 22:24
Load domain class in groovy class
//this will throw ClassNotFoundException
Class clazz = Class.forName(className)
//but this will load the class with success
Class clazz = Class.forName(className, true, Thread.currentThread().contextClassLoader)
@sergiomichels
sergiomichels / Deletes.groovy
Created October 15, 2012 19:41
Groovy Sql & ORA-00903
def sql = new Sql(dataSource)
def tables =["myTable1", "myTable2"]
tables.each {tab ->
sql.execute("truncate table ${tab}") //ORA-00903 here
}
@sergiomichels
sergiomichels / DeletesSolution.groovy
Created October 15, 2012 19:48
Groovy Sql & ORA-00903 - Solution
def sql = new Sql(dataSource)
def tables =["myTable1", "myTable2"]
tables.each {tab ->
sql.execute("truncate table ${tab}".toString()) //works!
}
@sergiomichels
sergiomichels / PasswordChangeCommand.groovy
Created October 16, 2012 02:04
Command Object for control of password change
package com.wordpress.sergiosmind.command
import grails.validation.Validateable;
import groovy.transform.ToString;
@Validateable
@ToString
class PasswordChangeCommand {
//dependency injection for SpringSecurityService
def springSecurityService