Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated docs and added ability to render as string
- Loading branch information
1 parent
8c8880e
commit 444042c
Showing
4 changed files
with
73 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 37 additions & 12 deletions
49
grails-app/taglib/org/grails/plugins/address/AddressTagLib.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters