CFObjective Notes: How Groovy and Grails made me a better ColdFusion Developer - Scott Stroz

May 17, 2013

How Groovy & Grails made me a better ColdFusion Developer - Scott Stroz

What is groovy?
OO language for java
dynamic language w/ similar feature set to Python and Ruby
compiled to JVM byte code
interoperates w/ other java code and libraries
-- most java code is also syntactically valid groovy
like CF script all grown up
like java if it were written today

What is Grails?
open source web app f/w
--uses Groovy
uses coding by convention paradigm
(no fuse.xml config files)
provides a full stand-alone dev environment
--- can code in Grails, have different settings, objects, etc, for different environments, and it will use those objects when you build the Dev or Production version of your app
adds persistence w/ Grails ORM (GORM)
deployed as a WAR file

How are they like ColdFusion?
Groovy is an easy way to leverage Java without having to write Java code
Both grails/groovy use script based syntax
grails uses tab based syntax in view pages
all are fun and easy to learn
...ColdFusion is all those things too

Groovy / Grails
combination of favorite things
script syntax, fw/1, orm, coldspring, mx unit, validate this
...all INCLUDED by default in Groovy / Grails

IDE
2 main options
InteliJIDEA
Spring Tool Suite

First, install Groovy/Grails.
there is good documentation for this, regardless of the IDE you use

Creating a Grails Project
-- uses command line
grails create-app [app name]
or in IntelliJ
quick start 'create new project' options.

IntelliJ has a "grails view"
-- shows you the stuff you need in a way that's easy to develop when doing a Grails app
-- directory structure looks different than the directory structure from the command line / windows explorer

Domain classes
your "stuff"
the objects we use to define our business model
User, UserType objects, etc.
similar to ORM in CF but they do a lot more
--- GORM is the actual engine that's used for this
by default Grails uses Hibernate for persistence, but you can swap in whatever you want (mongoDB, etc)
via use of static variables called "constraints", it sets up your validation

don't NEED semicolons but you can use them (but IntelliJ will tell me i don't need them)

Relationships -- easy to read, easier to do than CF ORM
class User
{
static hasMany = [favoriteSports : Sport]
}

depending on where you define these relationships will dictate how things cascade

constraints
static contraints = {
firstName blank: false, maxSize 50;
}
all constraints are processed at 2 different places
object.save()
object.validate()

so now in CF, a better practice would be to have the save() method call the validate() method inside it
that makes it impossible to save an object that hasn't been validated first

can write regex for your constraints too

That was all the 'model' stuff, now let's look at "controllers"...

can build from the command line
can also run the command from IntelliJ
generate-all command
creates controllers for each model in your app
full CRUD methods in your controllers
list, save, create new, etc
creates view files
can download plugins that allow you to use Bootstrap templates as your views.
creates test files
-- generated code isn't the best, but it gets you started
Grails lets you test your Controllers!

URL
http://site/user/list

same as in FW/1 or ColdBox
calls User.list() method in the controllers
uses the list.cfm file in the /views/user folder

instead of "rc" like ColdBox, in Grails you use "params"

Dynamic finders
User.findByLastName( "stroz" )
User.findBydobBetween( date1, date2 )
"findBy" is a keyword and the rest of the method is just based on a property name

Grals understands "environments"
Dev
Test
Production
can perform different actions based on environment
-- use mock services for test/dev
-- run processes to populate database in test/dev

Running the app
grails run-app (or click "play" in IntelliJ")
spins up the JVM

CAN do step-debugging!
CAN do breakpoints!

Some caveats
by default Grails "dev" and "test" env's use an "in memory" h2 database
-- this means every time you start app, you have NO DATA
-- modify datasource.groovy to use the file based h2 database or other RDMS

other tidbits
"return" is optional
-- whatever is returned from the last line of code, IS what the method will return (unless you specify something)
"safe navigation" operator
user?.firstName
-- if firstName is null, it won't throw an error (CF throws an error)

Elvis operator
shortcut ternary operator ?:
displayName = user.firstName ?: "None"

Classes and methods are public by default

nifty looping construct
(1..5).each{
// do stuff
}
...runs that loop 5 times

tons of plugins
-spring security
-twitter bootstrap templates
-css/js minification
-jquery
-resource management
-caching
-etc

"Groovy Truth" -- all objects can be coerced into a boolean
if( user )
...if user doesn't have any values, it returns "false"

optional parentheses
method( a, b )
same as
method a, b

Resources:
grails.org
-- amazing documentation
groovy.codehaus.org