<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sander Verbruggen &#187; grails</title>
	<atom:link href="http://www.sanderverbruggen.com/tag/grails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sanderverbruggen.com</link>
	<description>Is there anything I *don&#039;t* have an opinion about?</description>
	<lastBuildDate>Wed, 31 Mar 2010 09:45:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>DomainLink tag for Grails</title>
		<link>http://www.sanderverbruggen.com/2009/07/28/domainlink-tag-for-grails/</link>
		<comments>http://www.sanderverbruggen.com/2009/07/28/domainlink-tag-for-grails/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 07:03:58 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[taglib]]></category>

		<guid isPermaLink="false">http://www.sanderverbruggen.com/?p=38</guid>
		<description><![CDATA[One of the first things I noticed when generating the views from my domain classes was the code to link to an object in list.gsp.
Wouldn&#8217;t it be nice to just &#8216;mention&#8217; an object using a tag that would render itself including a link to it&#8217;s controller?

That resulted in the following tag:

    def [...]]]></description>
			<content:encoded><![CDATA[<p>One of the first things I noticed when generating the views from my domain classes was the code to link to an object in list.gsp.</p>
<p>Wouldn&#8217;t it be nice to just &#8216;mention&#8217; an object using a tag that would render itself including a link to it&#8217;s controller?<br />
<span id="more-38"></span><br />
That resulted in the following tag:</p>
<pre name="code" class="java">
    def domainLink = {attrs, body ->
        def object = attrs.object
        def controller = attrs.controller
        // Action defaults to 'show'
        def action = attrs.action ?: 'show'

        if (!controller) {
            // If no controller was given in the tag, determine it through the object's class
            def lookForClass = object.metaClass.theClass

            // Find out which domain class this is
            def domainClass = grailsApplication.domainClasses.find {
                domain -> domain.clazz == lookForClass
            }

            if (!domainClass) {
                throwTagError("The property [object] of tag [domainLink] isn't a Domain class, but a ${object.class}")
            }
            controller = domainClass.shortName
            // first char to lower case
            controller = controller[0].toLowerCase() + controller.substring(1)
        }

        def bodyContent = body()
        // Do we have a body?
        // In that case use the body, otherwise "toString" the object
        def display = bodyContent ?: object
        out << g.link (controller: controller, action: action, id: object.id, {display})
    }
</pre>
<p>Now we can use it in a GSP like this:</p>
<pre name="code" class="xml">
&lt;my:domainLink object="${todoInstance}"/>
</pre>
<p>which renders a link to the show action of the Todo controller (probably) with the ID of this todoInstance. You can specify a specific action and even a controller if you want. If you don't include a body, it uses toString().</p>
<pre name="code" class="xml">
&lt;my:domainLink object="${todoInstance}" action="delete">Delete me!&lt;/my:domainLink>
</pre>
<p>You might wonder why I didn't use the convention, that a gsp links to the controller it came from. My domain model contains some inherited classes (e.g. User extends Person), so a list of objects can contain both classes. Now, the links in this list point to the correct controller and I'm not editing a User through the PersonController.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sanderverbruggen.com/2009/07/28/domainlink-tag-for-grails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

