Skip to content

Instantly share code, notes, and snippets.

@imjayson
imjayson / Grails: Render deep JSON
Last active December 18, 2015 10:59
Grails: Render deep JSON
import grails.converters.JSON
JSON.use('deep')
render(contentType: 'text/json') {[
'results':results,
'status': results ? "200" : "500"
]}
@imjayson
imjayson / Grails: Get images from an URL's metatag
Last active December 18, 2015 10:59
Grails: Get images from an URL's metatag
def getImageFromUrl(String url) {
println "getting image from url: " + url
if (!url) return null
def document
try {
document = Jsoup.connect(url).get()
def imgStr = getMetaTag(document, "twitter:image")
if (!imgStr) imgStr = getMetaTag(document, "og:image")
return imgStr
} catch (Exception e) {
@imjayson
imjayson / Grails: createCriteria query, group by day, on a single date field
Last active October 23, 2019 14:57
Grails: createCriteria query, group by day, on a single date field (modify for grouping by month, year, hour, minute, seconds)
DOMAIN CLASS (Post):
String timeDay
Date date
static mapping = {
timeDay formula: "FORMATDATETIME(date, 'yyyy-MM-dd')" // h2 sql
//timeMonth formula: "DATE_FORMAT(time, '%Y-%m-%d')" // mysql sql
}
CONTROLLER:
def c = Post.createCriteria()
@imjayson
imjayson / Javscript: Efficient JS Looping - reminder to myself
Last active December 21, 2015 00:39
Javscript: Efficient JS Looping - reminder to myself
for (var i = 0, item; item = a[i++];) {
// Do something with item
}
function inc(x, y = 1) { return x += y; }
@imjayson
imjayson / ECMAScript 6: Lexical block scope
Created August 15, 2013 14:40
ECMAScript 6: Lexical block scope
function doSomething() {
let N = 5;
if (someCondition) {
let N = 10;
doSomethingElse(N);
}
console.log(N); // 5
}
var Person = {
name: 'Joe',
hello() {
console.log('Hello, my name is', this.name);
}
};
[1,2,3].map(x => x * x);
@imjayson
imjayson / gist:5f97e77494f88affd01a
Last active August 29, 2015 14:01
Grails 2.3: REST + Scaffolding
Not sure if it is designed this way but it is wierd that the @Resource annotation doesn't work with generated scaffolding (generate-all).
It is easy to have 2 controller serving 1 domain class, but not ideal.
Evan's answer @ http://stackoverflow.com/questions/19465421/how-to-use-scaffolding-and-restfulness-together-in-grails-2-3 works.
It's a bit more work but the only 1 controller is needed
Note: will need a entry point (linking to the same controller)
@imjayson
imjayson / .block
Last active October 30, 2017 10:20
Gantt Chart
license: MIT