Tuesday 5 May 2009

Solution for transient problem

After much head scratching and reference searching I've come up with a solution to the previously mentioned transient problem. Basically I've changed the list action in the Player controller to:
- check to see if the user is trying to sort by percentage,
- if so a closure sorts based on the getPercentage() method.
- then if the order was supposed to be descending, we call reverse on the list.

Here's the list action code:

def list = { params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)

if(params.sort=="percentage"){
def players = Player.list().sort {player ->
player.getPercentage()
}
if(params.order=="desc"){
println params.order
players = players.reverse()
}
return [ playerInstanceList: players, playerInstanceTotal: Player.count() ]
}
else{
return [ playerInstanceList: Player.list(params), playerInstanceTotal: Player.count() ]
}
}


Quick and dirty tests look good so far. I'm sure there's probably a more elegant solution out there, if so let me know.

No comments: