Grails Scaffolding - grails

I am recently working on grails and would like to know how to do more complex scaffolding
For example, if I want to Scaffold a class
class Book{
Author a
Publisher p
// ....
}
Author class
class Author{
String firstName
String lastName
// ...
}
Publisher class
class Publisher{
String name
String address
// ....
}
Now if I have a BookController
class BookController{
static scaffold = true;
}
I have a layout of
Author Publisher
However if I want a layout with
AuthorID AuthorFirstName AuthorLastName PublisherName PublisherAddress
I have looked through the http://grails.org/doc/latest/guide/scaffolding.html, however, I am unable to set it to the given property. I would like to know I am able to accomplish it? A tutorial would be helpful.

The scaffolding plugin within Grails is not designed to handle these types of complex views out of the box. You have a few options:
Use the install-templates command and modify the scaffolding templates to handle your needs.
Re-design your domain class to use embedded Author and Publisher. This will change the scaffolding output, but it also will change a lot more too. I wouldn't use this option unless you understand all the changes this will make to your domain model.
Generate the code using scaffolding then customize the output to suit your needs.
Of the three options presented here I would recommend the third as it makes the most sense to address the narrow scope of your issue.

You could also use transients. But transients aren't displayed by default.
You need to modify the templates and explicitly hide otherwise hidden fields using constraints i.e. id
NOTE: code below untested, for illustration purposes only.
//optional, but allows code completion in IDE ;-P
String authorName
String getAuthorName(){
return a.firstName + ' ' + a.lastName
}
static transients = [authorName:String]
static constraints = {
id(display:false)
}

Related

Grails: How do i select from a list of previously created objects in my view?

Let's say i have the following classes:
package test
class Person {
String name
static hasMany = [stuff:Stuff]
static constraints = {
}
}
and
package test
class Stuff {
String stuff
static belongsTo = Person
static constraints = {
}
}
When i implement the view for Person i want to be able to select from a list of previously created stuff. How do i achieve that? I see that, when i use scaffolding Grails generates that drop down menu where i can do that but since i a designing my own views i would like to understand how that is done.
Thank you.
Probably good to start be reviewing the documentation for the select tag here: https://gsp.grails.org/latest/ref/Tags/select.html
A simple example to present a list of all Stuff would look like:
<g:select name="stuffSelect" from="${Stuff.list()}" optionKey="id" optionValue="stuff"/>
This should give you a dropdown of all Stuff in your database, displaying the String value to the user, but submitting the DB ID when the form submits.
I'm pretty sure you can use the generate-all command in grails, to see what the scaffolding code looks like:
http://docs.grails.org/3.1.1/ref/Command%20Line/generate-all.html
Using this command should generate Controllers, views, etc. so you can see how the scaffolded code works. Don't worry about being able to go back to generated scaffold code, just delete the stuff created by generate-all, and grails will autogenerate it at runtime like it does now.

How to properly use Grails Command Objects

I've been struggling for a while reading and trying to understand command objects, but I've yet to understand how to use them in my particular scenario.
Here's what I have:
class Beneficary {
String name
//more attributes
static hasMany = [dependents = Dependent]
}
class Dependent {
DegreeKinship degreeKinship //enum
//several atrributes
static belongsTo = [beneficiary: Beneficiary]
}
I've read in several articles, including SO answers, that one should be using Command Objects for this if one wishes but I'm failing to understand just how.
I've wrote this:
class DependentCommand {
List<Dependent> dependents = ListUtils.lazyList([], {new Dependent()} as Factory)
}
but I'm not sure how to use it in my Beneficiary class.
Also, I wish to have it all under a single view (beneficiary/create)
Any help would be greatly appreciated.
I don't think you should use them in the Beneficary class, use them in BeneficaryController.
Command objects give you a standardized way of encapsulating, converting and validating request parameters. As such the main use for them is in a controller, not a domain class which can already do most of a command object's functions natively.
You could rewrite your command like this if you wanted to accept a request containing parameters along the lines of dependents=1&dependents=2:
class DependentCommand {
List<Dependent> dependents
}

Grails bindData exclude by annotation

I want to exclude domain field from data binding
Is it possible to mark class field by an annotation?
For example domain:
class Article {
String text
.....
Author author
}
in code I have to write bindData(article, params, [exclude: ['author']]) for cheating prevention
But much easier simple to annotate Author author. But I didn't find how.
Since Grails 2.1.0 you can use the bindable constraint to indicate that a property should not be automatically assigned during data binding.
class Article {
String text
...
Author author
static constraints = {
author bindable: false
}
}
Now calling bindData(article, params) will automatically exclude the article's author property.

grails: scaffolding create view for domain with hasMany relation

Let's assume the following (simplified) domain classes:
class Person {
static hasMany = [stringProperties: StringProperty]
static constraints = {
}
}
and
class StringProperty {
String name
String value
static constraints = {
name blank:false
value blank: true
}
}
When scaffolding generates the create view, there is no option in the gsp to create a StringProperty from Person.
Does a plugin exist or does somebody know a best practice, that can render a sort of create ui that allows to create members of a hasmany relation.
I'm just asking before i take the time to modify the scaffolding templates.
This is one of the areas where a plugin or enhanced scaffolding would be welcomed by the community. If I had the time I would take the information presented here and make a plugin for it. I have used this approach a few times and it works well.

domains have some common fields,extends domain or embedded?

when i design database.I use embedded to embed common fields.but it's can't init dateCreated and createdBy,what'd i do?extends domain or embedded is right way to handle common fields?
code to say?
class Created {
Date dateCreated
Long createdBy
def beforeInsert()
{
dateCreated= new Date()
createdBy=0
}
}
class Updated {
Date lastUpdated
Long updatedBy
//it works?
def beforeUpdate(){
lastUpdated=new Date()
updatedBy=0
}
//it works?
def beforeInsert(){
lastUpdated=new Date()
updatedBy=0
}
}
class CreatedUpdated {
Created created
Updated updated
//Must use the embedded option, or the type of exception, can not find CreatedUpdated
static embedded = ['created','updated']
}
class Term {
String name
CreatedUpdated createdUpdated
static embedded = ['createdUpdated']
Term parent
static hasMany =[terms:Term]
static mapping = {
version false
}
String toString()
{
name
}
static constraints = {
name unique:true,size: 1..20
parent nullable: true
createdUpdated display:false,nullable:true
terms display:false
url url: true
}
}
or use extends?
class Term extends CreatedUpdated{
String name
Term parent
static hasMany =[terms:Term]
static mapping = {
version false
}
String toString()
{
name
}
static constraints = {
name unique:true,size: 1..20
parent nullable: true
terms display:false
url url: true
}
}
`
what is right to me?
I'd definitely make this example embedded rather than inherited. I don't think you should make this call based solely on the fact that objects contain common fields. Instead, you should use inheritance if it makes sense for your model using standard OO design techniques. For example, if "myClass is a myBaseClass" doesn't hold true, inheritance is probably the wrong solution.
In general, I'd stay away from classes like CreatedUpdated that are just a collection of properties and not an actual object from your domain. Java/Groovy has only single inheritance, so this only works if you have one base class like this.
Also, for that particular case, created and updated timestamps can automatically be applied by GORM. If you're using spring security, check out the audit-trail plugin for automatically creating createdBy and updatedBy columns.
In this particular case audit-trail plugin should suffice the requirements. However if you have such requirement for other fields wherein no plugin is available, then one of the possible solution could be to inject such common fields at compile time via AST Transformation. Internally audit-trail plugin uses this concept to inject those fields. Depending upon your requirement you can either use Global AST Transformations or Local AST Transformations.

Resources