abstract class AControllerUtils { /** * In Rails parameters are usually * multi-dimentional maps. * "foo[bar]" and foo[zed] would be mapped to * params['foo']['bar'] and params['foo']['zed'] (in ruby) * ...which happens to be the same for Groovy. :) * * Takes two parameters the params map to Railsify and a boolean indicating * if the modifications should be done to the map passed in or to * a new copy. The second is optional, and by default it will modify * the map in place. Returns the railsified map. */ static public railsifyParams(params, inPlace=true){ if (params != null && params.size() > 0){ def matchRegexp = /(\S+)\[(\S+)\]/ ; def newParams = [:]; params.keySet().each { key -> def matcher = (key =~ matchRegexp); if (matcher.matches()){ def newKey = matcher[0][1]; def newSubKey = matcher[0][2]; if (! params.containsKey(newKey)){ if (inPlace){ params[newKey] = [:]; } else { newParams[newKey] = [:]; } } if (inPlace){ params[newKey][newSubKey] = params[key]; } else { newParams[newKey][newSubKey] = params[key]; } } } if (! inPlace){ return newParams; } } return params; } }