Skip to content

Commit

Permalink
Updated docs and added ability to render as string
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescookie committed Jun 8, 2012
1 parent 8c8880e commit 444042c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
2 changes: 1 addition & 1 deletion GrailsAddressGrailsPlugin.groovy
Expand Up @@ -19,7 +19,7 @@ class GrailsAddressGrailsPlugin {
An address domain object that can be embedded in other domain object to save redefining it all the time
'''

def documentation = "http://grails.org/plugin/grails-address"
def documentation = "https://github.com/jamescookie/grails-address"

def license = "APACHE"
def scm = [ url: "http://github.com/jamescookie/grails-address" ]
Expand Down
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -18,7 +18,17 @@ There is a taglib to assist you when you want to display an address:

<address:display address="${personInstance?.address}"/>

This will create a unordered list that contains all the fields that have been populated. Each list item will have the class of the field name so that you can target them for styling.
This will create a unordered list that contains all the fields that have been populated. Each list item will have the class of the field name so that you can target them for styling. e.g.

<ul class="address">
<li class="line1">line 1</li>
<li class="line2">line 2</li>
<li class="line3">line 3</li>
<li class="town">a town</li>
<li class="county">a county</li>
<li class="postCode">WW1 1WW</li>
<li class="country">United Kingdom</li>
</ul>

If you want to include extra list items in this list, them you can give them in the body of the taglib. e.g.

Expand Down
49 changes: 37 additions & 12 deletions grails-app/taglib/org/grails/plugins/address/AddressTagLib.groovy
@@ -1,30 +1,55 @@
package org.grails.plugins.address

import org.apache.commons.lang.StringUtils

class AddressTagLib {
static namespace = "address"

def display = {args, body ->
def address = args.address
if (!address || !(address instanceof Address)) throw new IllegalArgumentException("Need to supply an address")
out << '<ul class="address">'
if (body) out << body()
renderField(address, "line1")
renderField(address, "line2")
renderField(address, "line3")
renderField(address, "town")
renderField(address, "county")
renderField(address, "postCode")
String type = args.as ?: 'list'
def method = 'renderAs' + StringUtils.capitalize(type)
if (!this.metaClass.methods*.name.contains(method)) {
throw new IllegalArgumentException("Unable to render as $type");
}
out << this."$method"(address, body)
}

String renderAsString(Address address, Closure body) {
def output = createAddress(address) {name, value->"$value, "}
if (output.size() > 2) {
output = output[0..-3]
}
output
}

String renderAsList(Address address, Closure body) {
def writer = new StringWriter()
writer << '<ul class="address">'
if (body) writer << body()
writer << createAddress(address) {name, value->"""<li class="$name">$value</li>"""}
writer << '</ul>'
return writer.toString()
}

private String createAddress(Address address, Closure how) {
def writer = new StringWriter()
def renderItem = renderField.curry(how)
writer << ['line1', 'line2', 'line3', 'town', 'county', 'postCode'].collect {renderItem(address, it)}.join('')
def country = address.country
if (country) {
out << """<li class="country">${g.country(code: country)}</li>"""
writer << how("country", g.country(code: country))
}
out << '</ul>'
return writer.toString()
}

private void renderField(address, name) {
private renderField = {Closure output, address, name->
def value = address."$name"
if (value) {
out << """<li class="$name">$value</li>"""
return output(name, value)
} else {
return ""
}
}
}
24 changes: 24 additions & 0 deletions test/unit/org/grails/plugins/address/AddressTagLibTests.groovy
Expand Up @@ -48,4 +48,28 @@ class AddressTagLibTests {
line1: "line 1")
assert applyTemplate('<address:display address="${address}"><li>A name</li></address:display>', [address: address]) == '<ul class="address"><li>A name</li><li class="line1">line 1</li></ul>'
}

void testShouldRenderAsString() {
def address = new Address(
line1: "line 1",
line2: "line 2",
line3: "line 3",
town: "a town",
postCode: "WW1 1WW",
county: "a county",
country: "gbr")
assert applyTemplate('<address:display address="${address}" as="string" />', [address: address]) == 'line 1, line 2, line 3, a town, a county, WW1 1WW, United Kingdom'
}

void testCanRenderEmptyAddressAsString() {
def address = new Address()
assert applyTemplate('<address:display address="${address}" as="string" />', [address: address]) == ''
}

void testCannotRenderAsAnythingElse() {
def address = new Address()
shouldFail {
applyTemplate('<address:display address="${address}" as="rubbish" />', [address: address])
}
}
}

0 comments on commit 444042c

Please sign in to comment.