curl -s get.gvmtool.net | bash
gvm list grails
gvm install grails 2.4.4
gvm use grails 2.1.0
grails help
grails create-app todo
grails run-app
grails create-domain-class es.ua.expertojava.todo.todo
package es.ua.expertojava.todo
class Todo {
static constraints = {
}
}
package es.ua.expertojava.todo
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
* See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
*/
@TestFor(Todo)
class TodoSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test something"() {
}
}
package es.ua.expertojava.todo
class Todo {
String title
String description
Date date
Date reminderDate
String url
static constraints = {
title(blank:false)
description(blank:true, nullable:true, maxSize:1000)
date(nullable:false)
reminderDate(nullable:true)
url(nullable:true, url:true)
}
String toString(){
title
}
}
grails create-controller es.ua.expertojava.todo.todo
package es.ua.expertojava.todo
class TodoController {
def index() { }
}
Sustituimos
def index = {}
por...
def scaffold = Todo
o...
def scaffold = true
package es.ua.expertojava.todo
class Category {
String name
String description
static hasMany = [todos:Todo]
static constraints = {
name(blank:false)
description(blank:true, nullable:true, maxSize:1000)
}
String toString(){
name
}
}
package es.ua.expertojava.todo
class Todo {
String title
String description
Date date
Date reminderDate
String url
Category category
static constraints = {
title(blank:false)
description(blank:true, nullable:true, maxSize:1000)
date(nullable:false)
reminderDate(nullable:true)
url(nullable:true, url:true)
category(nullable:true)
}
String toString(){
"title"
}
}
grails create-controller es.ua.expertojava.todo.category
package es.ua.expertojava.todo
class CategoryController {
def scaffold = Category
}
package es.ua.expertojava.todo
class Tag {
String name
static hasMany = [todos:Todo]
static constraints = {
name(blank:false, nullable:true, unique:true)
}
String toString(){
name
}
}
package es.ua.expertojava.todo
class Todo {
String title
String description
Date date
Date reminderDate
String url
Category category
static hasMany = [tags:Tag]
static belongsTo = [Tag]
static constraints = {
title(blank:false)
description(blank:true, nullable:true, maxSize:1000)
date(nullable:false)
reminderDate(nullable:true)
url(nullable:true, url:true)
category(nullable:true)
}
String toString(){
"title"
}
}
grails create-controller es.ua.expertojava.todo.tag
package es.ua.expertojava.todo
class TagController {
def scaffold = Tag
}
import es.ua.expertojava.todo.*
class BootStrap {
def init = { servletContext ->
def categoryHome = new Category(name:"Hogar").save()
def categoryJob = new Category(name:"Trabajo").save()
def tagEasy = new Tag(name:"Fácil").save()
def tagDifficult = new Tag(name:"Difícil").save()
def tagArt = new Tag(name:"Arte").save()
def tagRoutine = new Tag(name:"Routine").save()
def tagKitchen = new Tag(name:"Cocina").save()
def todoPaintKitchen = new Todo(title:"Pintar cocina", date:new Date()+1)
def todoCollectPost = new Todo(title:"Recoger correo postal", date:new Date()+2)
def todoBakeCake = new Todo(title:"Cocinar pastel", date:new Date()+4)
def todoWriteUnitTests = new Todo(title:"Escribir tests unitarios", date:new Date())
todoPaintKitchen.addToTags(tagDifficult)
todoPaintKitchen.addToTags(tagArt)
todoPaintKitchen.addToTags(tagKitchen)
todoPaintKitchen.category = categoryHome
todoPaintKitchen.save()
todoCollectPost.addToTags(tagRoutine)
todoCollectPost.category = categoryJob
todoCollectPost.save()
todoBakeCake.addToTags(tagEasy)
todoBakeCake.addToTags(tagKitchen)
todoBakeCake.category = categoryHome
todoBakeCake.save()
todoWriteUnitTests.addToTags(tagEasy)
todoWriteUnitTests.category = categoryJob
todoWriteUnitTests.save()
}
def destroy = { }
}
grails generate-all es.ua.expertojava.todo.Todo
grails generate-controller es.ua.expertojava.todo.Tag
grails generate-views es.ua.expertojava.todo.Tag