Showing posts with label grails tutorials. Show all posts
Showing posts with label grails tutorials. Show all posts

Sunday, January 25, 2009

Favorites Section on Grails Tutorials

After a long time I have added new feature to the grails tutorials.
The new section is favorites.
Now when you find interesting article on the grails tutorials you can add this article into your favorites. Later on you can access favorites and browse articles. Further on you can browse favorites of other users. Favorites of each user are also exposed as RSS feed so if you like somebody's collection of favorites you can follow it via RSS feed.

"Unfortunately" implementation of this feature was more or less straightforward so there is nothing I could really blog about.
More or less if you created your code with enough templates and custom tags, implementing new feature in most cases is actually adding one or more domain classes, reusing existing services and templates and using dynamic gorm methods.
Well and this is the part I really like about grails. Adding new features usually is more or less matter of hours (not days).

Thursday, August 7, 2008

Grails, Groovy, XML

Last few days, while working on grails tutorials, I needed to translate some Grails domain objects to XML. I thought it should not be so hard as there is lot of articles about Groovy and XML. But what I found out is that most of the articles present only the simple usage of Groovy XmlSlurper. One of the examples would be:

def xml = new groovy.xml.MarkupBuilder()
xml.person(id:99){
firstname("John" )
lastname("Smith" )
}

And what I need is to traverse recursive structure of domain object. I know it is not so hard but anyway I decided to describe it here. At the end, as you will see it is very simple. But, by the way I will also explain how Grails supports XML transformations.

Let start with Grails support. If you don't request special structure of XML but it is enough to directly transform domain object to XML then Grails is excellent solution for you.

Domain object to translate is:

class LearningArea {
String title
String description
Date dateCreated
Date lastUpdated
List tutorials
List subAreas

static hasMany = [tutorials: TutorialLink, subAreas: LearningArea]

static constraints = {
title(blank:false, minSize:3, maxSize:500)
description(blank:false, maxSize:3000)
}

public String toString() {
return title
}
}

In Grails you can translate object to XML as in following example:

LearningArea learningArea = LearningArea.get(1)
String xml = render learningArea as XML

But interesting thing to notice is that render can be imported from two packages:

grails.converters.XML and grails.converters.deep.XML

And of course there is difference which package you use.

If you use deep package complete domain objects tree (with all sub areas's, sub areas and so on...) will be generated with all the properties. So in the case of LearningArea complete tree of all subAreas and all tutorials will be translated to XML including all properties.

If you use just converters.XML package, top level object is fully translated but referenced objects are translated only to the level of ids. This means that XML generated will be much smaller but to obtain additional information you need to retrieve additional data later.

Unfortunately for me, I couldn't use this Grails XML magic because I didn't want tutorials in the XML but I wanted full tree of subAreas only with titles and without description properties. So I had to use Groovy XmlSlurper. Important thing to notice in the above example of XmlSlurper is that when using XML builder, you can also provide closures. So all I needed was to recursively traverse the tree of the subAreas. And the code to do that is very simple:

    def generateXML= {
def writer = new StringWriter()
MarkupBuilder xml = new MarkupBuilder(writer)

LearningArea lr = LearningArea.findByTitle("Groovy")

toXml(lr, xml)

String str = writer.toString()
}

private void toXml(LearningArea la, def xml) {
xml.learningArea(name: la.title, id: la.id) {
la.subAreas.each {
toXml(it, xml)
}
}
}

As we can see all the logic is in the toXml method. This method accepts instance of LearningArea and MarkupBuilder, writes element learningArea to XML with attributes name and id, and the as subelements recursively writes all sub learning areas. This way the complete XML tree is generated in few lines of code and I got example that is little bit more complex than most of the examples on the web.

And if you didn't visited grails tutorials yes, you can do it now :)

Monday, June 16, 2008

Tag Cloud Added to Grails Tutorials

I have added tag cloud to http://www.grailstutorials.com. Now each tutorial can be tagged so navigation is possible also by tags.

The main difference between tag cloud on grails tutorials compared to tag cloud on other sites is that it is enough to be logged in to change (add/remove) tags on any tutorial.

So far there is no possibility to add new tags, you have to reuse precreated set, but if there will be interest I plan to add this possibility too.

For tag cloud implementation I used Act As Taggable Plugin
This plugin give you boost at the beginning but from my point of view still misses lot of support functionality that I had to implement. I hope that in next days I will be able to organize code I implemented for tag cloud features and provide it to 'act as taggable plugin'. This way everybody can reuse what I had implemented and don't have to reimplement the same thing again.

If time permits me for sure I will write at least one post about tag cloud implementation on grails tutorials.

If you have any ideas or proposals for grails tutorials please let me know. I would be happy to have more responses from those who visit grailstutorials.com

Thursday, June 5, 2008

GrailsTutorials.com now supports RSS, Star Rating and Click Count

After few days of other responsibilities I had found some free time and additionally to star rating and click count I have added also support for RSS feeds. To see and try the latest features visit www.grailstutorials.com.

During development of RSS feeds and star rating I have hit some not too complicated but interesting problems and challenges. I will write separate posts about those challenges.

And as usually, just to remind you, register and post interesting links.

In the case of any feature requests, bugs or just comments do not hesitate to comment to this post.

Tuesday, May 27, 2008

Grails Tutorials

I have just released first draft version of grails tutorials powered by Grails. You can find it at www.grailstutorials.com.

For me it is only alpha version but I believe it can be useful already. You are able to register, to post link and to search among posted articles. I hope that in the near future there will be much more features like tags, rating, hierarchical search...

As you find interesting article, just paste link to that article here so the all community of Grails and Groovy practicioners can benefit from it. This way our knowledge will be focused and the Grails and Groovy community will be able to grow even faster.

To understand how I see future development of www.grailstutorials.com just visit vision section.

And don't be lazy and post some interesting links :)