logo

Gravl: Screencast, Command objects, Custom Validators, Pagination

logo

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 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!

3 Responses to “Gravl: Screencast, Command objects, Custom Validators, Pagination”

  1. Man its looking awesome! I think I’ll migrate my blog over to it once its all done ;-)

  2. David Michael says:

    Hello Glen,

    The video URL is currently giving an error when accessed.

    But I’m following your work looking and the source from SVN and you are doing a awesome job!

    []‘s

  3. Howdy,
    It looks like you’ve taken down the screencast link, but I guess that might be appropriate as it seems the comment code is running on your blog right here? Sure, it says Gravl on the footer… Neat!

    Looks like a fantastic intro, I’ve been drying on the vine looking for something like this.

    I’ll probably check out Pebble when I’m done here. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

logo
logo
Powered by WordPress | Designed by Elegant Themes