Rozdajemy Raspberry Pi podczas 33rd DegreeRaspberry Pi to give away at 33rd Degree Conference

Zapraszaliśmy Was już do odwiedzenia stoiska TouK podczas konferencji 33rd Degree i wzięcia udziału w konkursie. Pora powiedzieć, dlaczego warto to zrobić.We’ve already invited you to visit our stand during 33rd Degree Conference and to take part in our contest. It’s time to tell you, why it’s worth to do it.

Zapraszaliśmy Was już do odwiedzenia stoiska TouK podczas konferencji 33rd Degree i wzięcia udziału w konkursie.  Pora powiedzieć, dlaczego warto to zrobić.

Nasz konkurs to konkurs hakerski i gra terenowa w jednym. Oprócz dobrej zabawy macie szansę na powrót do domu z własnym Raspberry Pi. Więc szukajcie piórek TouK, żeby wejść do gry!

Do zobaczenia w środę!We’ve already invited you to visit our stand during 33rd Degree Conference and to take part in our contest. It’s time to tell you, why it’s worth to do it.

Our contest is a hacker competition and a field game in one. Apart from a lot of fun you have a chance to come home with your own Raspberry Pi. So look for TouK’s feathers to enter the game!

See you on Wednesdey!

You May Also Like

Simple trick to DRY your Grails controller

Grails controllers are not very DRY. It's easy to find duplicated code fragments in default generated controller. Take a look at code sample below. It is duplicated four times in show, edit, update and delete actions:

class BookController {
def show() {
def bookInstance = Book.get(params.id)
if (!bookInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])
redirect(action: "list")
return
}
[bookInstance: bookInstance]
}
}

Why is it duplicated?

There is a reason for that duplication, though. If you move this snippet to a method, it can redirect to "list" action, but it can't prevent controller from further execution. After you call redirect, response status changes to 302, but after method exits, controller still runs subsequent code.

Solution

At TouK we've implemented a simple trick to resolve that situation:

  1. wrap everything with a simple withStoppingOnRender method,
  2. whenever you want to render or redirect AND stop controller execution - throw EndRenderingException.

We call it Big Return - return from a method and return from a controller at once. Here is how it works:

class BookController {
def show(Long id) {
withStoppingOnRender {
Book bookInstance = Book.get(id)
validateInstanceExists(bookInstance)
[bookInstance: bookInstance]
}
}

protected Object withStoppingOnRender(Closure closure) {
try {
return closure.call()
} catch (EndRenderingException e) {}
}

private void validateInstanceExists(Book instance) {
if (!instance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])
redirect(action: "list")
throw new EndRenderingException()
}
}
}

class EndRenderingException extends RuntimeException {}

Example usage

For simple CRUD controllers, you can use this solution and create some BaseController class for your controllers. We use withStoppingOnRender in every controller so code doesn't look like a spaghetti, we follow DRY principle and code is self-documented. Win-win-win! Here is a more complex example:

class DealerController {
@Transactional
def update() {
withStoppingOnRender {
Dealer dealerInstance = Dealer.get(params.id)
validateInstanceExists(dealerInstance)
validateAccountInExternalService(dealerInstance)
checkIfInstanceWasConcurrentlyModified(dealerInstance, params.version)
dealerInstance.properties = params
saveUpdatedInstance(dealerInstance)
redirectToAfterUpdate(dealerInstance)
}
}
}