Ok. My comment system is now pretty closed to being done, so it’s time to screencast some progress. Here’s a sample of comments in a Gravl Screencast (4mb)

gravl Screencast in action

I though it might be a good chance to demonstrate command objects, the basic Grails Ajax tags and their usage, and how to handle errors nicely.

I’ve added a Ajax “Preview” feature so you can see your comment markup before you post them. I’m using Command objects for the comment preview since I want to be able to do simple validation without having to satisfy the more strict criteria of my real domain objects.

If you haven’t played with Command objects, they provide a very simple way to automatically marshall an html form into an object. You can define the same rich “constraints” model that Domain objects have, and get all that error message goodness that you’re used to.

Using command objects also provides a good chance to demonstrate custom constraints to do cross-field validation. In the case of my comment object, I don’t want to let them tick “email me when new comments are added” unless they’ve provided a valid email address (which is optional). Here’s how you implement it:

   emailUpdates(validator: { enabled, comment ->
        if (enabled &&  !comment.email ) {
            return false
        }
      })

Implementing the archive was a “lunchtimeable” undertaking once I read up on the g:paginate tag. For paginate to work, you need to know the size of your dataset, and the number of entries per page. This introduced me to the countBy dynamic method which makes implementing pagination a snack:

def offset = params.offset ? params.offset : 0
totalArchiveSize = BlogEntry.countByBlogAndStatus(blog, "published")
entries = BlogEntry.findAllByBlogAndStatus(
    blog, "published",
    [ sort: "created", order: "desc", offset: offset, max: 20 ] )

And you’ve in business with pagination:

Gravl paginate in action

One final tip around application versioning, which I picked up from Marc Palmer at Grails Exchange. It’s nice to know what version of the application is running in the wild. You can make use of <g:meta> to include both your application version and your grails version in the footer.

Gravl version  on
    Grails 

You can update your grails application version number by using the grails set-version 0.2 or whatever. The settings get written to your application.properties file which is where g:meta picks them up from.

Embed your app and grails version in a footer

Ok. Once I get image uploading done and the interface for approving comments, I’ll be ready to post M1. I’m pretty close to my 1000LOC, but I’m sure I’ll squeeze it all in.

Watch this space!