Monday, June 16, 2008
Tag Cloud Added to Grails Tutorials
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
Tuesday, June 10, 2008
Tweaking Star Rating component of RichUI Grails plugin
RichUI plugin for Grails is easy to use with number of interesting Ajax enabled components. One of the components is Star Rating component. Core behavior of this component is good but I wanted to get a little bit more than this core behavior. This component works fine out of the box. You click on the star, go to server, update rating and update number of stars assigned. And everything is executed without reloading the whole page. But what I wanted to achieve is also to update number of votes within the same Ajax request. And this is not supported by component. I checked the code of this component and found out that this can be achieved with really only few lines of code. Actually solution is so simple that maybe it would be good if it is included into the official plugin code. And all described in this post you can of course see live on the grails tutorials site.
So to make more clear what I am talking about have a look on the picture.
Idea is that when you click on the star, number of votes is increased also without necessity to reload the whole page. And this is not possible with the current star rating component.
Right place to start searching what to change is RatingRenderer.groovy file. This class is responsible for rendering the star rating component.
Signature of method is
protected void renderTagContent(Map attrs, GroovyPageTagBody body, MarkupBuilder builder)where attrs are attributes sent to the component from the gsp page. And after some code inspection it is clear that update is implemented using remoteFunction grails tag. To see more about remoteFunction read this post. One of the parameters to remoteFunction is also name of the element in the gsp page to update. Line of the code in RatingRenderer.groovy that accomplish this is
String link = attrs.link.replace(":class:", "r${i}-unit rater").replace(":title:", "$i").replace("update", "${id}").replace("number", "$i").replace("%3Arating%3A", "${i}")
and for us interesting part is replace("update",${id}). And id to update is automatically generated in the first line of method. So actually, all we need to be able to update any page element, not exactly only stars, is to provide alternate id instead of one automatically generated.
So all I changed is following:
String updateId = id
if (attrs.updateId) {
updateId = attrs.updateId
}
String link = attrs.link.replace(":class:", "r${i}-unit rater").replace(":title:", "$i").replace("update", "${updateId}").replace("number", "$i").replace("%3Arating%3A", "${i}")
This way if I provide updateId attribute to the star rating component, remote function generated by this component will update page element with provided id, otherwise it will keep original behavior.
And now, all I have to do is to use construct like this one in my gsp pages:
<div id="${tutorialid}tutorial">
<richui:rating dynamic="true" updateId="${tutorialid}tutorial" id="${tutorialid}" units="5" rating="${rating}" showCurrent="true" controller="rating" action="rate" />
${rate?.votes} votes; ${clicks} clicks
</div>
And of course previous code snippet is part of the grails template so during rendition necessary values are read from the provided model.
So only with three lines of code added to existing plugin code and one change to existing plugin code, star rating component is more powerful than the original one.
Wasn't that easy? ;)
Thursday, June 5, 2008
GrailsTutorials.com now supports RSS, Star Rating and Click Count
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.
Sunday, June 1, 2008
Make gsp readable with grails templates and taglibs
In one of my previous post I already explained how grails templates and taglibs can simplify your gsp files and make them more readable. Now I believe I made even step further. One of the reasons I like grails is that front end technology is solved very good and is easy to understand even for java back-end developers like me. My gsp files does not look like html files but more like collection of components. The reason is simple. With grails it is very easy to create templates and tags. Example in this post is taken from www.grailstutorials.com, portal of grails and groovy community on which I work in my free time. Let me try to explain mind flow. I wanted to display components like one on the picture in common and easy to read way.
The first solution that came to my mind was really simple. Thing like this one you can solve easily in this way:
<div class="messageBox">
<div class="msg_box_title">
<label>TITLE_HERE</label>
</div>
<div class="msg_box_body">
CONTENT_HERE
</div>
</div>
But in the sea of the divs it is really hard to orientate and the gsp looks more complicated than it really is. So I start thinking how it would be nicer. And I realized that what I want to achieve so my code is easier to read is following:
<g:messageBox title="TITLE_HERE">
CONTENT_HERE
</g:messageBox>
So as the first thing I created message box template.
<div class="messageBox">
<div class="msg_box_title">
<label>${title}</label>
</div>
<div class="msg_box_body">
${body}
</div>
</div>
This code is almost the same as the first code. Important things to notice are model variables ${title} and ${body}. Now when I had a template I needed possibility to display it with the messageBox tag. So the next thing to create was to create tag. Just create simple tag file, if you don't know how read the post mentioned at the begging. And the code of the tag is:
def messageBox = {attrs, body ->
out << render(template:"/templates/common/messageBoxTemplate", model:[title:attrs.title, body:body()])
}The only good thing to notice in the code above is usage of body() as a value of body model parameter instead of body that came as a input value into messageBox. Reason for this is that we don't want to send body closure to the template but content of the body closure.
Having messageBox tag available, the first code example in this post can be specified in the gsp file as:
<g:messageBox title="TITLE_HERE">
CONTENT_HERE
</g:messageBox>
exactly as we wanted.
Now let us think about CONTET_HERE part. If we put complex gsp code there we will not get too much in readability. But solution is very simple. Don't use complex gsp code there, create corresponding template and tag for that part and you gsp code will be easy for reading. Just to give you example of what I have in mind, this is code snippet I use in grailstutorials.com to display statistics (you can see the picture on the top of post):
<g:messageBox title="Stats">
<g:dataInfo/>
</g:messageBox>
I believe that the final result is really easy to understand.
Tuesday, May 27, 2008
Grails Tutorials
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 :)
Wednesday, May 21, 2008
What to do when Grails does not generate table for your domain class
After some googling and reading this post I found solution or better solution how to find solution :)
Go to Config.groovy and log4j hibernate level change to the debug. Start the application and hibernate will display all actions it performs. In the big stacktrace you should be able to find description what happend. In my case it was:
[1704] hbm2ddl.SchemaUpdate Unsuccessful: create table tutorial_link (id bigint ...
[1704] hbm2ddl.SchemaUpdate BLOB/TEXT column 'link' used in key specification ...
So it turned out its not a problem of Grails, nor problem of Hibernate but different handling of columns by different databases.
However it took me some time to find out and understand what is happening so I decided to post it so maybe I will save somebody's time.
Tuesday, May 13, 2008
Why is it so Easy to Work with Grails
- front end for end users
- front end for administrators and other special users
- Easy creation of templates and tag libraries
- business logic persisted to database
- security
- searching
- (java back end - this can be discussable)
Let see each of these points.
Front End for End Users
Regarding front end for end users different requirements may be important. Maybe you want fancy front end with lot of Ajax. If this is the case you have different plugins available like (GWT, Flex). This means you can easily integrate advance Ajax concepts into front end. If you need only simple Ajax you can achieve this with the core grails functionality. You may want to have a look on one of my previous posts Grails and Simple Ajax. At the final end, as with all frameworks to have really nice user interface you have to work hardly and cannot achieve this in minutes. With Grails it may be faster but need not. But the real velocity comes with further features.
Front End for Administrators
Creation of front end for administrators is mainly out of the box. Just try to think what are administrators responsibilities. They have to create something (usually business objects), they have to approve something - usually on business objects, they need to have overview of the system status - usually on business objects. Mainly they do not need fancy user interface. No fancy user interface means that you can use one of the grails out of the box features - scaffolding. For each domain object grails can for you generate CRUD screens. For administrators to be able to work with these screens minor if any updates are necessary.
Easy Creation of Templates and Tag Libraries
With Grails creation of templates and tags is so easy that very soon you will realize that all gsp files are created from templates and tags. Code that influence readability of gsp files, at least for me, are conditions, loops and similar programmatic constructs. All these constructs are easily replaceable with template or tags. Actually, when I create template, I immediately create tag for that template and after that work only with tag. To see example of template and tag lib you may want to have a look at my post about templates and tag.
Business Logic Persisted to Database
Here comes full power of the Grails. Just create domain object and database tables are generated for you. Even database is automatically updated when you update domain objects. For each domain object or relationship between domain objects you can create basic CRUD screens. Learning how to work with domain objects does not take a long time and if you have experience with Hibernate it can take you less than a day.
Security
Generated front end for administrators and working with domain object on the low level will be useless if there is no security for application. For sure in the most of the cases we cannot allow end users to see domain objects in CRUD screens. It means we have to secure CRUD screens. This is actually not a problem. There are security plugins for grails. I have used acegi security plugin. It will generate all necessary classes and screens for you. All you need to do is to specify what is visible by which role. And this configuration is really very simple. So in matter of minutes you integrate security into application. I am not sure about advance features as I didn't have used them but probably it should not be complex either. How much time it usually takes you to secure web applications developed with other technologies?
Searching
These days searching is popular. And of course it is useful. All data on you web site will be useless if users cannot find them. Grails has solution for this too. You can use Searchable plugin. You add line of the code to the domain class(es) and out of the box you have searching integrated into your application.
Java
I must agree that this is really discussable if we need Java or no. But in the case you want to have backend implemented with Java, important thing is that Java is integrated with Grails applications seamlessly.
I have just made short overview of features that was most useful for me so far while working on grails application. Features mentioned are not full set as grails frameworks contains lot of other important capabilities. Just to mention few of them: easy unit, functional and integration testing, excellent plugin system, work with XML, web services...
What about your experience with Grails?