Grails: hasMany dependencies - grails

I have two Grails classes with a hasMany dependency:
class Author {
String name
static hasMany = [books: Book]
}
I generated the controller with scaffolding its running and works. But is there a way to gernate a book within the the author controller? So there i can generate a author and some book without changing the view? Thanks in advance.

The built in scaffolding templates for Grails do not have this ability. However, you can use the Grails command install-templates and modify/enhance the templates to have this ability.
Using this command the following directories and contents will be added to your project:
src
templates
artifacts
scaffolding
war
The templates within the "scaffolding" directory are where you want to make your changes/enhancements.
So, in short, no, there is no out of the box functionality for this, but you can add it on your own.

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.

Grails Application Folder Organization Best Practice

I am struggling to organize domains, controller and views in Grails 2.3.8 application. The applications is quite small right now but its planned to grow bigger and I would like to organize folder structure and package naming convention little better. Ideally I would like to have following structure, but I am sure there are better approaches. I would gladly welcome the wonderful solutions you guys will have.
Domain
Item
Category
Controller
admin
ItemController (package = com.example.admin, namespace = admin)
CategoryController (package = com.example.admin, namespace = admin)
public
ItemController (package = com.example.public, namespace = public)
CategoryController (package = com.example.admin, namespace = public)
Controller
admin
ItemController (package = com.example.admin, namespace = admin)
CategoryController (package = com.example.admin, namespace = admin)
public
ItemController (package = com.example.public, namespace = public)
CategoryController (package = com.example.admin, namespace = public)
Views
admin
index.gsp
create.gsp
etc
public
index.gsp
create.gsp
etc
The questions now are
1. Is this the right folder structure? Any pitfalls this might create
2. How do I accomplish this on grails
Grails is a convention over configuration framework. Grails projects all follow a predefined folder arrangement, which is fully described in the documentation:
http://grails.github.io/grails-doc/latest/guide/single.html#conventionOverConfiguration
When you create a Grails application, it sets up a default Groovy/Java package. This default package (which can be changed) is used by default when you create Grails artefacts (domains, controllers, etc). I've toyed with the idea of creating sub-packages for these artefacts, but now I simply don't do that. Here's why.
Grails does a lot of stuff for you. That's fantastic because it eliminates a lot of boilerplate code and saves you time. And the more you stick with the conventions, the more Grails can do for you. And this applies to packaging. If you leave the artefacts in the default package, that cuts down on the number of import statements. This applies not only when you create artefacts but also when you create unit tests (they use the same default package).
Your concern about putting many classes into the same package is a valid one. And since you're expecting your application to grow in size that's a likely possibility. The Grails solution is plugins. You can modularize your application into plugins and then use a main app to bring everything together. Each plugin would have a different package namespace.
If you want your app and its plugins to live in the same codebase, you can create a plugins directory in your project (as a sibling directory of grails-app). Then, create your Grails plugins within the plugins directory. Finally, refer to each plugin in grails-app/conf/BuildConfig.groovy.
grails.plugin.location.'plugin-a' = './plugins/plugin-a'
grails.plugin.location.'plugin-b' = './plugins/plugin-b'

Where should Grails Command objects be placed in the project structure?

I have a class called LoginCommand in domain/my/package/name
class LoginCommand {
String emailAddress
String password
}
My question is why is a table be auto generated in my database for a ***Command object in grails? Are these command objects supposed to be placed ouside of /domain to avoid auto generation of a table by hibernate/orm.
They should not go in grails-app/domain; they are not domain classes. Place them in src/groovy. Alternatively, a common convention is to put the command class in the same file as the controller that uses it.
Take a look at the Convention Over Configuration section in the grails manual to get an idea of what goes where.

Grails adding attributes to one-to-many relationship

I have set up a one-to-many relationship in my scaffolded Grails application:
class Course {
County county
Date date
int maxAttendance
static hasMany = [ persons:Person ]
}
class Person {
String firstName
String lastName
String email
Course course
boolean attended
boolean paid
static belongsTo = [ class:Course ]
}
So, when a user views the CourseController, they are able to see Person's registered in the selected Course.
My question is, how can I change the application so that when a user views the people in a given course, they can also view/modify the checkboxes for 'boolean attended' and 'boolean paid', which are also in the Person domain? Here is a screenshot:
The thing is that you are using the scaffolded view, so you are pretty much stuck with the default design.
You can modify the behaviour, by installing and modifying the templates that grails uses to generate those views:
grails install-templates
This will create the templates in src/templates/scaffolding, although I do not recommend that approach. Maybe it is time that you start developing your own controllers and views, since the scaffolding is there mainly for testing and for administrative use.

Inject field in a grails domain class dynamically

I have a problem in a grails application. My application use a plugin to generate some domain models, I need to add a field to one of those domain models and I don't have the source code. Is there a way to do it dynamically? For example using the GORM API the metaclass or something like that?
You could probably just extend the class from the plugin and add your field. That is a common thing to do with plugins like Spring Security Core.
class MyUser extends SecUser {
String phoneNumber
...
}

Resources