GORM Inheritance Mapping

2
Dec
2010

A really nice feature of GORM (and Hibernate) is support for inheritance in domain objects. For example if your application has different types of user accounts with specific properties then you can create distinct domain objects that inherit from a User base class that has all the common properties of a user (email, password, etc). Here’s what it might look like if you had a student user type and an admin user type:

class User {
	String email
	String password
	
	static mapping = {
		 tablePerHierarchy false
	 }
}
class AdminUser extends User {
	// Admin user specific properties
	String adminGroup
}
class Student extends User {
	// Student specific properties
	Date graduationDate
	Float gpa
}

Because of the tablePerHierarchy set in the user mapping this will result in 3 seperate tables (user, admin_user, and student). Omit this if you’d like all the fields to be contained within a single user table.

If you store the user object in session after authentication you can write code like this in your view:

<g:if test="${session.user instanceof Student}">
You are a student with a GPA of ${session.user.gpa}
</g:if>

See also:

Url Mapping in Grails

25
Oct
2010

In grails you have a lot of control over how specfic controller actions are mapped to urls through the UrlMappings.groovy file. The default behavior is for the urls to look something like this:

"/$controller/$action?/$id?"

With the ‘?’ denoting optional parts of the url. So for example the user controller actions will map to urls that look like this

/user/index
/user/show/22

These are pretty reasonable defaults but we might want to override these mappings so we have a little more control over the urls. One reason to do this is so we can map all administrative controller actions to urls that begin with /admin. Then we can easily limit access to these admin controllers easily with a filter.

To change the mapping with our previous user control example we would add the following line to the UrlMappings.groovy file:

"/admin/$controller/$action?/$id?"(controller : "user")

Or alternatively this:

"/admin/$controller/$action?/$id?"{controller = "user"}

Then we’d get urls that looks like the following:

/admin/user/index
/admin/user/show/22

Another good approach is to explicitly define all your public urls in your UrlMapping and then creating a catch all for all other actions that map to /admin/ as shown in the example below

class UrlMappings {
	static mappings = {
		// public pages
		"/"(view:"/index")
		"/auth/$action"(controller: "auth")
		"/portfolio/$action?/$id?"(controller: "portfolio")
		"/project/$action?/$id?"(controller: "project", action: "show")
                
		// protected pages
		"/admin/$controller/$action?/$id?"()
                
		"500"(view:'/error')
	}
}

Grails with jQuery

26
Jun
2010

I’ve been doing some development in Grails (think Ruby on Rails in Java) and while I can appreciate the opinionated nature of the framework I have a differing opinion when it comes to choice of javascript framework. I’m a big fan of jQuery but unfortunately Grails uses Prototype by default. You can easily change this by installing the jquery plugin. To do this run the following commands:

grails install-plugin jquery
grails install-jquery

Then change the javascript reference in your layout (main.gsp):

<g:javascript library="jquery" />

Now you can use jquery in your project without worrying about conflicts between the two frameworks or the unnecessary overhead of including both.

Autonomy and Motivation

6
Jun
2010

There’s a fascinating TED Talk from Daniel Plink about motivation as it relates to creative work. He presents the case that providing external incentives (such as cash bonuses) actually decease productivity in any kind of job that requires even the least bit of creative problem solving.

It seems that Netflix has incorporated some of these ideas of what truly motives employees into their own corporate culture. Netflix employees have a tremendous amount of freedom and autonomy, work in a ROWE (Results Only Work Environment) and see the purpose in their work through management transparency: