Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Tuesday, October 27, 2009

RSS Builder in Grails in a Few minutes

As I planned to include RSS feeds into www.spenddaywithkids.com I needed possibility to create kind of RSS Builder. The good example of what I needed is IceRocket RSS Builder. So you are not generating RSS feeds directly from the data on your site but you are writing them manually. This way RSS feeds become kind of communication channel between the web site and users.

I would stay with IceRocket but I couldn’t find possibility to do even simple formatting for RSS items. So my thought was let see how it is complicated to do it in Grails. And it turned out that it takes only few minutes to get very similar functionality. So here is short example done almost step by step.

Note: As my site is currently running on Grails 1.0.3 maybe not everything will be compatible with 1.1 version out of the box.

So let go with implementation.

First create grails application and named it rssbuilder.

grails create-app rssbuilder
Then create two domain classes:
class RssChannel {
String name
String channelLink
String description

static constraints = {
name(blank:false, nullable:false, unique:true)
channelLink(blank:false, nullable:false, unique:false, url:true)
description(blank:true, nullable:true, maxSize:30000)
}
}


class RssItem {
String title
String itemLink
String summary
RssChannel rssChannel
Date dateCreated
Date lastUpdated

static constraints = {
title(blank:false, nullable:false, unique:false)
itemLink(blank:false, nullable:false, unique:false, url:true)
summary(blank:false, nullable:false, unique:false, maxSize:50000)
rssChannel(blank:false, nullable:false, unique:false)
}
}
Ah, don’t forget to install two plugins: Feeds Plugin and RichUI plugin.

Now we can generate controllers and views for two domain classes:
grails generate-all RssChannel
grails generate-all RssItem
Well now if you start application you are able to specify RSS channel and items for RSS channel. To provide possibility to specify better formatting for RssItem we can reuse <richui:richTextEditor tag from the RichUI plugin. In the create and edit view of the RssItem change summary and bind it to the richTextEditor. More or less you should input tag with the following:

<richui:richTextEditor name="summary" value="${rssItem?.summary}" width="525" height="525" />
Now to generate RSS feed we can reuse functionality of the Feed plugin. So create FeedController class with command grails create-controller Feed.

The code of the feed controller should be:

    def rss = {
if (!params.id) {
render "missing id in params"
return
}

RssChannel rssChannel = RssChannel.get(new Long(params.id))

if(!rssChannel) {
render "rss channel not found"
return
}


render(feedType:"rss", feedVersion:"2.0") {
title = rssChannel.name
link = rssChannel.channelLink
description = rssChannel.description
RssItem.findAllByRssChannel(rssChannel, [sort:"dateCreated", order:"desc"]).each() { item ->
entry(item.title) {
link=item.itemLink
item.summary
}
}
}
}
So now if you start application and create RSS channel and some items in that channel all you need to know is ID of that channel.

If the Id of the channel is e.g. 1 in the browser write http://localhost:8080/rssbuilder/feed/rss/1 and you will see your feed.

I believe this was really easy :)

As soon as I switch fun places to go with kids to the Grails 1.1 I will create plugin from this.

Sunday, October 18, 2009

Tips for working with front-end in Grails

I have implemented few web projects with Grails and some techniques while working with Grails front-end turned out to be better than other. When I didn’t follow them later on I usually had to refactor the code and did it the correct way.

If you are reading this blog you noticed that I write very often about developing front-end with Grails. The reason is not that I am the best in the front-end but the reason is that I am the worst in the front-end. I am back-end developer and what I was missing in the majority of web frameworks was impossibility to make front-end more modular. In Grails this is easily to achieve thanks to Grails templates and tags. And then you are almost to code in the normal java code.

But come back to tips.

Tip 1: Always create one gsp page per state of the result.

Tip 2: Use templates and tags to make your pages more modular.

Tip 3: When ever it gives sense send custom model classes to the gsp page (not the domain classes).

Well tips are nothing without some example. As an example I will take my latest project fun things to do with kids.

In the first installment home page had following responsibilities:

  • As a starting point for visitors
  • To display found places to go with kids
  • To inform user that the city provided cannot be uniquely identified (there is more than one city with provided name)
  • To inform user that no attractions were found around provided city

As you can see that is obvious break of the SRP (single responsibility principle). Because of too much responsibilities the model I was sending to the page was quite complex and the page contained number of if blocks. And you must admit that having if blocks in the html page is not the best practice. Another side effect was that page had about 300 lines that is simply too much.

So what I did? Well, of course I did some refactoring.

I have created one page per responsibility and depending on result controller decide which page to display and provides correct data model (is this maybe kind of strategy pattern?). This way it is much easier to understand what pages are doing. And now they are up to 30 lines long.

But if you check pages you can see they are quite similar:

To see home page navigate to: spend day with kids
To see page with results navigate to: things to do with kids in Nottingham
To see page where there are multiple cities e.g. search for Malinovo. Select Malinovo, Slovakia to see places to visit nearby my home :)
To see page where there are no results check e.g. for Belgrade.

If you checked all these pages you can notice that they are very similar but little bit different. So how do you solve similarities. Very easy. Use templates and tags.

And the last but not the least, it is obvious that the data models delivered to the pages can be quite different and even some cases need not to contain any domain classes.

So I hope that those tips will help you while working on your own grails projects.