I now it's total newbie question but cant't find answer on quick search in documnataion or in Google - where should I put mine own, general functions that i reuse in app? I write special a function to change empty strings in to strings of '-' and want to use them many, many, MANY times in app, so the "copy-paste" method for each Controller is illogical. Should all functions goes to Scripts catalog or in to other place?
This message was edited 1 time. Last update was at by Karol Litwinczuk
Dave Brown
Ranch Hand
Joined: Mar 08, 2005
Posts: 301
posted
0
You have a couple of options here, I tend to place reusable code that doesn't belong in Domain classes/Controllers or Services into it's own class in either the src/groovy or src/java folder depending on whether its groovy or pure java code obviously.
Alternatively for you specific task, have you considered the use of meta-programming? That is you can create a function that becomes part of the String class, so you would be able to call your function on any String instance.
I had a similar requirement where I needed a 'truncate' method so Strings over a certain length would be returned with a trailing "..." after a specified number of characters.
Have a look at my tiny post on how to accomplish it..