tags:

views:

45

answers:

3

I am working on a social site where users can block other users. Throughout the site --dozens of places, user information is displayed. For example, user comments, reply forms, online user list.. etc etc.. The problem is that given the high number of places user info is displayed, it's becoming very difficult to check each time if that user is blocked. For example:

<g:each var="comment" in="${comments}">
  <g:if test="!${loggedInUser.blockedUsers.find { it == comment.user}">
     show comment
  </g:if>
</g:each>

Does Grails provide any functionality that would facilitate creating some kind of filter or intercepter where I could simply exclude blocked users when iterating lists, etc? If not, what would you suggest I do?

A: 

It could probably be done like this:

<g:each var="comment" in="${comments.findAll { !(it in loggedInUsers.blockedUsers) }}">
    show comment
</g:each>

But I think it would be more efficient to filter your comments in the controller instead. You could probably do a Grails Criteria on your comments.

sbglasius
A: 

Maybe the Hibernate Filter plugin can help you?

wwwclaes
This plugin would be perfect if it worked :)
UltraVi01
Are you using WebFlow? There is currently a warning about that. I'm using the plugin and for me it is working like a charm.
wwwclaes
A: 

A good rule of thumb is to only load what you need, so your service method should filter out blocked users at the database level. Then your controllers will be simpler and views will only have to display what they are given.

But if you prefer interceptors, take a look here:

http://www.grails.org/Controllers+-+Interceptors

armandino