groovy grails gr8ladies women techmakers: minneapolis

27
GROOVY, GRAILS, AND GR8LADIES Women Techmakers: Minneapolis Minneapolis, MN March 22, 2014

Upload: jenn-strater

Post on 06-May-2015

89 views

Category:

Technology


1 download

DESCRIPTION

An introduction to Groovy and Grails given at Women Techmakers: Minneapolis

TRANSCRIPT

Page 1: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

GROOVY, GRAILS,

AND GR8LADIES

Women Techmakers: MinneapolisMinneapolis, MNMarch 22, 2014

Page 2: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Setup

Prerequisites Java jdk

try ‘java –version’ to see if it’s already installed http://www.grails.org/Installation

To Install Groovy and Grails: Gvmtool.net

curl –s get.gvmtool.net | bash gvm install groovy gvm install grails

gvm use grails 2.3.4

Page 3: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Gr8Ladies

An organization for the support of women in the Gr8 stack community

Goals: Education Networking Professional Development

Contact Us @Gr8Ladies on twitter Gr8Ladies.org LinkedIn, Facebook search Gr8Ladies Contribute to our website via GitHub

Page 4: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

The Gr8 Stack

Groovy – the language Grails – MVC framework Gradle –build automation tool

Used to build Android Apps Griffon – desktop app framework Spock – testing framework Many other minor frameworks and

development tools

Page 5: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Image via: http://blogs.vitoria-gasteiz.org/ti/2010/05/31/full-stack/

Page 6: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Groovy – The Language

Dynamic compiled language Flexible

optional typing default scope optional getters/setters

Rapid Development Integrates with existing JAVA libraries and

infrastructure #groovylang

Page 7: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Hello World

In Java,

In Groovy,

Page 8: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Special Functions in Groovy

Elvis Operator ?: In PHP:

In Groovy:

Page 9: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Ranges A collection of numbers defined by boundaries

Spread dot Applies a function to every element of a

collection and returns the result

Page 10: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Null safe user?.chapter?.name?.toUpperCase()

will return null if user or chapter is null and will not return an error

Page 11: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Groovy Truth Null, empty string, empty set, and 0 are all

false!

Page 12: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Image via: http://blogs.vitoria-gasteiz.org/ti/2010/05/31/full-stack/

Page 13: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Grails – the framework

MVC framework similar to Symphony in PHP, Django in Python,

or Ruby on Rails Convention over configuration #grailsfw

Page 14: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

MVC Architecture

image from: http://www.mathiasbosman.be/wp-content/uploads/2014/02/mvc_role_diagram.png

Page 15: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

The Layout

Page 16: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Model

Domain Classes Grails Object Relationship

Mapping(GORM) Uses Hibernate Class names map to tables Field names map to

columns Dynamic finders

User.findAllByGenderAndChapterIsNotNull(‘F’)

Events beforeInsert, afterUpdate,

beforeDelete Alternatives

Mongo and Redis

Page 17: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Controllers

Mapped to urls by name /user/list

UserController.list() Url Parameters map

to controller /user/show/id

UserController.show(id)

Page 18: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Views

GSP (Groovy Server Page)

Directory structure by convention UserController.list()

renders/views/user/list.gsp

Special tags for forms, links, looping and other tasks

Reference model data by ${model.attribute}

Page 19: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Tests

Lots of Options for writing tests Spock (default) JUnit Geb

Page 20: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

• www.gebish.org• http://docs.spockframework.org/en/spock-0.7-groovy-2.0/• For Grails, in BuildConfig.groovy:

dependencies {test "org.seleniumhq.selenium:selenium-support:2.40.0"

test "org.seleniumhq.selenium:selenium-firefox-driver:2.40.0" test "org.gebish:geb-spock:0.9.2”

}plugins {

test ":geb:0.9.2" compile ":remote-control:1.4“ // For accessing the context of the

// grails app from tests// Spock is already included in Grails 2.3.6

}

Page 21: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

package com.allison.pages

import geb.Page

class EmployeeListPage extends Page {

// the link that is navigated to // on the 'to' call static url = "employee/list"

// the closure that is verified on // the 'at' call static at = { pageTitle.text() == 'Employee List Page' }

// The navigator objects static content = { pageTitle(wait: true) { $('h2.title') } employeeLink(wait: true, to: EmployeeShowPage) { id -> $('a.employeeLink', 'id': "${id}") } }

// It is good practice to wrap all the // navigator objects in methods boolean employeeLinkIsPresent(Long id) { employeeLink(id).isDisplayed() }

EmployeeShowPage clickEmployeeLink(Long id) { employeeLink(id).click()

return browser.page }}

<html> <head> <title>Employee</title> </head> <body> <h2 class="title">Employee List Page</h2> <ul> <li> <a href="/gebDemo/employee/show/1" id="1” class="employeeLink"> Alice LastName </a> </li> <li> <a href="/gebDemo/employee/show/2" id="2” class="employeeLink"> Bob LastName2 </a> </li> <li> <a href="/gebDemo/employee/show/3" id="3” class="employeeLink"> Charles LastName3 </a> </li> </ul> </body> </html>

Page 22: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

package com.allison.functional

import com.allison.pages.EmployeeListPageimport com.allison.remote.EmployeeRemoteControlimport geb.spock.GebReportingSpec

class firstSpec extends GebReportingSpec {

EmployeeRemoteControl remote = new EmployeeRemoteControl()

Long employeeId_1 Long employeeId_2 Long employeeId_3

def setup() { EmployeeRemoteControl remote =

new EmployeeRemoteControl() employeeId_1 =

remote.createEmployee('Alice', 'LastName') employeeId_2 =

remote.createEmployee('Bob', 'LastName2') employeeId_3 =

remote.createEmployee('Charles', 'LastName3')

}

CONTINUED

def 'A descriptive name for the feature'() {

given: 'Setup stuff' // Can be blank

when: 'The employee list page is navigated to' EmployeeListPage employeeListPage =

to EmployeeListPage then: 'The employee list page header is displayed' assert at(EmployeeListPage) // All statements in a

// then-block are implicitly // asserted

and: 'The employee is listed' assert employeeListPage.employeeLinkIsPresent(

employeeId_1)

when: 'The employee is clicked' employeeListPage.clickEmployeeLink(employeeId_1)

then: 'At the show employee page' assert at(EmployeeShowPage) }

def cleanup() { remote.deleteEmployee(employeeId_1) remote.deleteEmployee(employeeId_2) remote.deleteEmployee(employeeId_3) }

}

Page 23: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Setting Up the Environment

GVM! Groovy enVironment Manager

curl –s get.gvmtool.net | bash gvm install groovy gvm install grails

Use grails 2.3.4 Choose an IDE

IntelliJ Eclipse Netbeans

Page 24: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Creating An App

grails create-app appName auto generates app structure

grails create-domain-class User Creates domain class and test file

grails generate-all appName.User Auto generates views and controllers based on

the properties of the domain class

Page 25: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Deploying a grails app

Grails war Defaults to target/appName-0.1.war

Deploy to JVM Example via Amazon ElasticBeanstalk

Page 26: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

Suggested Reading

Making Java Groovy – Ken Kousen Email [email protected] for coupon code

Programming Grails – Bert Beckwith (O’reilly animal book)

Groovy In Action - Dierk Koenig with Andrew Glover, Paul King, Guillaume Laforge and Jon Skeet

Many Tutorials available on grails.org

Page 27: Groovy Grails Gr8Ladies Women Techmakers: Minneapolis

What Next?

Come to Groovy Users of Minnesota(GUM) every second Tuesday of the month at Smart Things

Join Gr8Ladies for hack sessions and contribute to the gr8ladies website on github

Gr8Ladies.org or @Gr8Ladies on twitter