- Revision
- 734
- Author
- mauro
- Date
- 2008-06-18 13:28:14 -0500 (Wed, 18 Jun 2008)
Log Message
WAFFLE-80: Refactor select macros to support options as hashes with value and name. Added function asNameables to convert to sequence of hashes with value and name fields. Added unit tests using waffle-testing.
Modified Paths
- trunk/.classpath
- trunk/waffle-resources/pom.xml
- trunk/waffle-resources/src/main/resources/ftl/waffle/form.ftl
Added Paths
- trunk/waffle-resources/src/test/
- trunk/waffle-resources/src/test/java/
- trunk/waffle-resources/src/test/java/org/
- trunk/waffle-resources/src/test/java/org/codehaus/
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormController.java
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormMacroTest.java
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.ftl
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.txt
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.ftl
- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.txt
Diff
Modified: trunk/.classpath (733 => 734)
--- trunk/.classpath 2008-06-18 16:00:57 UTC (rev 733) +++ trunk/.classpath 2008-06-18 18:28:14 UTC (rev 734) @@ -9,6 +9,7 @@ <classpathentry excluding="**" kind="src" output="waffle-ruby/target/classes" path="waffle-ruby/src/main/ruby"/> <classpathentry kind="src" output="waffle-mock/target/classes" path="waffle-mock/src/main/java"/> <classpathentry kind="src" output="waffle-mock/target/test-classes" path="waffle-mock/src/test/java"/> + <classpathentry kind="src" output="waffle-resources/target/test-classes" path="waffle-resources/src/test/java"/> <classpathentry excluding="**" kind="src" output="waffle-resources/target/classes" path="waffle-resources/src/main/resources"/> <classpathentry kind="src" output="waffle-taglib/target/classes" path="waffle-taglib/src/main/java"/> <classpathentry kind="src" output="waffle-taglib/target/test-classes" path="waffle-taglib/src/test/java"/>
Modified: trunk/waffle-resources/pom.xml (733 => 734)
--- trunk/waffle-resources/pom.xml 2008-06-18 16:00:57 UTC (rev 733) +++ trunk/waffle-resources/pom.xml 2008-06-18 18:28:14 UTC (rev 734) @@ -1,4 +1,5 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.codehaus.waffle</groupId> @@ -8,8 +9,27 @@ <packaging>jar</packaging> <artifactId>waffle-resources</artifactId> <name>Waffle Resources</name> - <description> - A collection of distributable Waffle webapp resources. - </description> + <description>A collection of distributable Waffle webapp resources.</description> + <dependencies> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>waffle-testing</artifactId> + <version>${pom.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <testResources> + <testResource> + <directory>${project.build.testSourceDirectory}</directory> + <filtering>false</filtering> + <excludes> + <exclude>**/*.java</exclude> + </excludes> + </testResource> + </testResources> + + </build> </project> \ No newline at end of file
Modified: trunk/waffle-resources/src/main/resources/ftl/waffle/form.ftl (733 => 734)
--- trunk/waffle-resources/src/main/resources/ftl/waffle/form.ftl 2008-06-18 16:00:57 UTC (rev 733) +++ trunk/waffle-resources/src/main/resources/ftl/waffle/form.ftl 2008-06-18 18:28:14 UTC (rev 734) @@ -24,6 +24,7 @@ * * @param values the values to join * @param separator the separator to join list with + * @return A string with joined values --> <#function join values separator> <#assign result = ''> @@ -32,6 +33,25 @@ </#function> <#-- + * Converts a sequence to a sequence of nameables hashes with name and value fields + * + * @param elements the sequence of elements to convert + * @param valueField the name of the value field in the input element (defaults to "value") + * @param nameField the name of the name field in the input element (defaults to "name") + * @return A sequence of nameables hashes + --> +<#function asNameables elements valueField="value" nameField="name"> + <#assign result = []> + <#list elements as element> + <#assign value='${element["${valueField}"]!element}'> + <#assign name='${element["${nameField}"]!element}'> + <#assign nameable = {"value":"${value}", "name":"${name}"}> + <#assign result=result+[nameable]> + </#list> + <#return result> +</#function> + +<#-- * Shows values as CSV * * @param values the sequence of values @@ -119,6 +139,7 @@ <#-- * Writes a select input element allowing a value to be chosen from a list of options. + * If option value and name fields are not set, they'll default to the option value itself. * * @param field the name of the field to bind the element to * @param options a sequence of available options @@ -127,14 +148,17 @@ --> <#macro selectSingle field options selectedValue="" attributes=""> <select id="${field}" name="${field}" ${attributes}> - <#list options as value> - <option value="${value?html}"<@isSelected value selectedValue/>>${value?html}</option> + <#list options as option> + <#assign value="${option.value!option}"> + <#assign name="${option.name!option}"> + <option value="${value?html}" <#if selectedValue==value>selected="true"</#if>>${name?html}</option> </#list> </select> </#macro> <#-- * Writes a select input element allowing a multiple values to be chosen from a list of options. + * If option value and name fields are not set, they'll default to the option value itself. * * @param field the name of the field to bind the element to * @param options a sequence of available options @@ -143,9 +167,11 @@ --> <#macro selectMultiple field options selectedValues attributes=""> <select multiple="multiple" id="${field}" name="${field}" ${attributes}> - <#list options as value> + <#list options as option> + <#assign value="${option.value!option}"> + <#assign name="${option.name!option}"> <#assign selected = contains(selectedValues?default([""]), value)> - <option value="${value?html}" <#if selected>selected="true"</#if>>${value?html}</option> + <option value="${value?html}" <#if selected>selected="true"</#if>>${name?html}</option> </#list> </select> </#macro>
Added: trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormController.java (0 => 734)
--- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormController.java (rev 0) +++ trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormController.java 2008-06-18 18:28:14 UTC (rev 734) @@ -0,0 +1,75 @@ +package org.codehaus.waffle.resources.ftl; + +import static java.util.Arrays.asList; + +import java.util.List; + +/** + * @author Mauro Talevi + */ +public class FormController { + + private List<String> values = asList("one", "two"); + private List<Nameable> nameables = asList(new Nameable("one", "One"), new Nameable("two", "Two")); + private List<Displayable> displayables = asList(new Displayable("one", "One"), new Displayable("two", "Two")); + private List<String> selectedValues; + + public List<String> getValues() { + return values; + } + + public List<String> getSelectedValues() { + return selectedValues; + } + + public void setSelectedValues(List<String> selectedValues) { + this.selectedValues = selectedValues; + } + + public List<Nameable> getNameables() { + return nameables; + } + + public List<Displayable> getDisplayables() { + return displayables; + } + + public static class Nameable { + private final String value; + private final String name; + + public Nameable(String value, String name) { + this.value = value; + this.name = name; + } + + public String getValue() { + return value; + } + + public String getName() { + return name; + } + + } + + public static class Displayable { + private final String id; + private final String display; + + public Displayable(String id, String display) { + this.id = id; + this.display = display; + } + + public String getId() { + return id; + } + + public String getDisplay() { + return display; + } + + } + +}
Added: trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormMacroTest.java (0 => 734)
--- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormMacroTest.java (rev 0) +++ trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/FormMacroTest.java 2008-06-18 18:28:14 UTC (rev 734) @@ -0,0 +1,43 @@ +package org.codehaus.waffle.resources.ftl; + +import static java.util.Arrays.asList; +import static org.codehaus.waffle.testing.view.ViewHarness.processView; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +/** + * @author Mauro Talevi + */ +public class FormMacroTest { + + private static final boolean SHOW_VIEW = false; + + protected String processTemplateView(Object controller, String templatePath) { + return processView(templatePath, controller, SHOW_VIEW); + } + + private String readResource(String path) throws IOException { + return IOUtils.toString(ClassLoader.getSystemResourceAsStream(path)); + } + + + @Test + public void canProcessSelectMacros() throws IOException{ + FormController controller = new FormController(); + controller.setSelectedValues(asList("one")); + String expected = readResource("org/codehaus/waffle/resources/ftl/select.txt"); + assertEquals(expected, processTemplateView(controller, "org/codehaus/waffle/resources/ftl/select.ftl")); + } + + @Test + public void canDecorateOptionsAsNameables() throws IOException{ + FormController controller = new FormController(); + String expected = readResource("org/codehaus/waffle/resources/ftl/nameables.txt"); + assertEquals(expected, processTemplateView(controller, "org/codehaus/waffle/resources/ftl/nameables.ftl")); + } + +} \ No newline at end of file
Added: trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.ftl (0 => 734)
--- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.ftl (rev 0) +++ trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.ftl 2008-06-18 18:28:14 UTC (rev 734) @@ -0,0 +1,9 @@ +<#import "/ftl/waffle/form.ftl" as w> +<#assign nameables=w.asNameables(controller.getNameables()) /> +<#list nameables as n> +${n.value}:${n.name} +</#list> +<#assign displayables=w.asNameables(controller.getDisplayables(), "id", "display")/> +<#list nameables as n> +${n.value}:${n.name} +</#list>
Added: trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.txt (0 => 734)
--- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.txt (rev 0) +++ trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/nameables.txt 2008-06-18 18:28:14 UTC (rev 734) @@ -0,0 +1,4 @@ +one:One +two:Two +one:One +two:Two
Added: trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.ftl (0 => 734)
--- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.ftl (rev 0) +++ trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.ftl 2008-06-18 18:28:14 UTC (rev 734) @@ -0,0 +1,6 @@ +<#import "/ftl/waffle/form.ftl" as w> +<@w.selectSingle "values" controller.getValues() "one"/> +<@w.selectSingle "nameables" controller.getNameables() "two"/> +<@w.selectMultiple "values" controller.getValues() controller.getSelectedValues() /> +<@w.selectMultiple "nameables" controller.getNameables() controller.getSelectedValues() /> +<@w.selectMultiple "displayables" w.asNameables(controller.getDisplayables(),"id","display") controller.getSelectedValues() /> \ No newline at end of file
Added: trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.txt (0 => 734)
--- trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.txt (rev 0) +++ trunk/waffle-resources/src/test/java/org/codehaus/waffle/resources/ftl/select.txt 2008-06-18 18:28:14 UTC (rev 734) @@ -0,0 +1,20 @@ + <select id="values" name="values" > + <option value="one" selected="true">one</option> + <option value="two" >two</option> + </select> + <select id="nameables" name="nameables" > + <option value="one" >One</option> + <option value="two" selected="true">Two</option> + </select> + <select multiple="multiple" id="values" name="values" > + <option value="one" selected="true">one</option> + <option value="two" >two</option> + </select> + <select multiple="multiple" id="nameables" name="nameables" > + <option value="one" selected="true">One</option> + <option value="two" >Two</option> + </select> + <select multiple="multiple" id="displayables" name="displayables" > + <option value="one" selected="true">One</option> + <option value="two" >Two</option> + </select>
To unsubscribe from this list please visit:
