Skip to content

Instantly share code, notes, and snippets.

View mathifonseca's full-sized avatar

Mathi Fonseca mathifonseca

View GitHub Profile
@mathifonseca
mathifonseca / note.md
Last active August 29, 2015 14:01
GRAILS - Strange behaviour when having a whitespace in a GSP tag

In Grails 2.3.7, if you accidentally (I can't think of any other reason) leave a white space between the closing bracket and quote of the test attribute of an <g:if> tag, its condition will always evaluate to false.

<g:if test="${1 != 2} ">
     Should be here...
</g:if>
<g:else>
     But it’s here!
</g:else>
@mathifonseca
mathifonseca / GRAILS - Get configuration from Holders.md
Last active February 9, 2018 12:47
GRAILS - Get configuration from Holders

Since Grails 2.0, the usage of org.codehaus.groovy.grails.commons.ConfigurationHolder has been deprecated, and you get a very annoying warning, even when it is being used by one of your plugins.

The recommended way of getting your config since then has been using dependency injection to get the grailsApplication bean into your controllers or services.

I've found a better (more concise and readable) way to get my configuration. I haven't done any serious research yet, but the only difference at first look is that instead of using DI, I'm getting my configuration from a static method in class grails.util.Holders.

So, here is an example Config.groovy:

@mathifonseca
mathifonseca / MVC.md
Last active August 29, 2015 14:06
MVC.md

#MVC = Model-View-Controller

##Contexto La interfaz de usuario es usualmente la parte más frecuentemente modificada de una aplicación interactiva. Por esta razón, es importante mantener las modificaciones a la interfaz de usuario separadas del resto del sistema. Los usuarios pueden querer ver los mismos datos desde distintas perspectivas, como en un gráfico de barras o de torta. Estas representaciones deberían reflejar el mismo estado actual de los datos.

##Problema Cómo se puede mantener la funcionalidad de interaz de usuario separada de la funcionalidad de la aplicación y aún poder responder a acciones del usuario o cambios a los datos de la aplicación? Y cómo pueden crearse, mantenerse y coordinarse las múltiples representaciones de los datos cuando estos cambien?

##Solución El patrón Model-View-Controller (MVC) separa la funcionalidad de aplicación en tres tipos de componentes:

@mathifonseca
mathifonseca / grails.md
Last active August 29, 2015 14:06
Grails

#Grails

##Objetivo Simplifica el desarrollo de aplicaciones Java. Lleva el desarrollo de aplicaciones web al siguiente nivel de abstracción. Construido sobre muchas herramientas que son usadas actualmente y sigue teniendo la flexibilidad para cambiar lo que se desee. Las configuraciones por defecto son usualmente suficientes.

Hibernate El ORM por default del mundo Java
Spring Inversion of Control (IoC) container y framework wrapper
SiteMesh Layout-rendering framework

I know it's a dangerous thing, it can generate confusion and unwanted magical happenings, but...

The Groovy Truth is what makes collections evaluate as false when they are null or empty, or Numbers evaluate as false when its value is null or zero and true otherwise.

As with many other operators and defult Groovy methods, you can add this magic to an object of your own. Just by overriding its asBoolean method.

For example, here is my Order class.

class Order {
@mathifonseca
mathifonseca / GRAILS - String and variable contatenation.md
Created October 1, 2014 01:53
GRAILS - String and variable contatenation

This seems pretty stupid, but it can happen to anyone.

This, of course, works:

boolean something = true
prinltn "The value of something is: " + something

But, this, strangely (at first) DOES NOT work:

@mathifonseca
mathifonseca / GRAILS - ConfigObject curiosity.md
Created October 8, 2014 15:00
GRAILS - ConfigObject curiosity

What happens if you look for a config key in a groovy.util.ConfigObject and it does not exist?

It should return null or an empty map. The latter is true.

But the curious thing is that the key is created if it does not exist! It seems pretty odd and useless, but they must have a reason...

Here is the code that does that in this class:

public Object getProperty(String name) {
@mathifonseca
mathifonseca / GRAILS - Don't use this packages!.md
Created October 27, 2014 14:34
GRAILS - Don't use this packages!

This is not Grails fault, but Spring-Loaded's.

They make the assumption that you won't put any of your own code under a package called in certain ways, and caches classes under that packages, which are meant to be used only by the framework. The most significant ones for us are grails.* and groovy.*. As a result, if you do use that packages, you will get something like this when trying to reload those classes:

2014-09-26 04:21:20,945 [FileSystemWatcher: files=#100 cl=java.net.URLClassLoader@
19d86dfc] ERROR plugins.AbstractGrailsPluginManager  - Plugin [services:2.4.2] could not reload changes to file [/<path to ggts workspace>/ggts-workspace/grails-slow3/grails-app/services/slow/SomeClassService.groovy]: Cannot get property 'cacheOperationSource' on null object
Message: Cannot get property 'cacheOperationSource' on null object
   Line | Method
->> 184 | doCall in CacheGrailsPlugin$_closure4
@mathifonseca
mathifonseca / GRAILS - Assigned id for domains.md
Last active July 15, 2019 00:26
GRAILS - Assigned id for domains

If you have a Domain class with an assigned id (not the default autoincremented long), the id assignment must not be in the constructor, but in a different line.

That means, having this domain:

class State implements Serializable {
    String id
    static mapping = {
    	id column: 'id', generator: 'assigned'
 }
@mathifonseca
mathifonseca / GRAILS - ToString of a null param.md
Created July 7, 2015 12:28
GRAILS - ToString of a null param

I hate it. I simply hate it.

If a Grails parameter is null and you call toString() on it, it doesn't fail with a good old NullPointerException like I expected. Instead, what you get is a String with value 'null'.

The only way to get a null value when the param is null, is to use the Groovy Safe Navigation operator.

Therefore:

given: