Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
#49
Browse files Browse the repository at this point in the history
- specific API for modal forms
  • Loading branch information
szimano committed May 7, 2015
1 parent fb2bc20 commit 0eceba0
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 44 deletions.
6 changes: 3 additions & 3 deletions examples/src/main/scala/org/demo/PersonForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ object PersonForm {
f.selectOneField(_.gender)(identity).possibleValues(_ => List("Male", "Female")).label("Gender").renderHint(asRadio()) ||
f.field(_.secret).label("Secret").renderHint(asPassword()),
f.field(_.bio).label("Biography").renderHint(asTextarea(rows = 6)),
f.subform(_.cars, carForm(f.parentAction((person, index, car) => ActionResult(deleteCar(person, car)))), false).label("Cars"),
f.subform(_.cars, carForm(f.parentAction((person, index, car) => ActionResult(deleteCar(person, car))))).label("Cars"),
f.action("addcar")(p => ActionResult(p.copy(cars = p.cars :+ Car("", "", 0)))).label("Add car"),
f.subform(_.legoSets, legoSetForm(f.parentAction((person, index, ls) => ActionResult(deleteLegoSet(person, ls)))), false).label("Lego sets").renderHint(asTable()),
f.subform(_.legoSets, legoSetForm(f.parentAction((person, index, ls) => ActionResult(deleteLegoSet(person, ls))))).label("Lego sets").renderHint(asTable()),
f.action("addlegoset")(p => ActionResult(p.copy(legoSets = p.legoSets :+ LegoSet("", "", 0, 0)))).label("Add lego set"),
f.staticField(p => Message(p.registrationDate)).label("Registration date"),
f.field(_.id).renderHint(asHidden()),
f.subform(_.favoriteCar, carModal, true).label("Favorite Car"),
f.modal("favorite", p => p.favoriteCar, (p: Person, c: Car) => p.copy(favoriteCar = c), carModal).label("Favorite Car By Modal"),
f.field(_.a1) || f.field(_.a2) || f.field(_.a3) || f.field(_.a4).label("4th field") || f.field(_.a5)
|| f.field(_.a6) || f.field(_.a7) || f.field(_.a8) || f.field(_.a9) || f.field(_.a10) || f.field(_.a11)
|| f.field(_.a12) || f.field(_.a13) || f.field(_.a14) || f.field(_.a15) || f.field(_.a16)
Expand Down
14 changes: 8 additions & 6 deletions supler/src/main/scala/org/supler/Supler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object Supler extends Validators with RenderHints {
*
* By default subforms are rendered as a list. Use the `.renderHint()` method to customize.
*/
def subform[T, ContU, U, Cont[_]](param: T => ContU, form: Form[U], lazyForm: Boolean)
def subform[T, ContU, U, Cont[_]](param: T => ContU, form: Form[U])
(implicit container: SubformContainer[ContU, U, Cont]): SubformField[T, ContU, U, Cont] =
macro SuplerFieldMacros.subform_impl[T, ContU, U, Cont]

Expand All @@ -54,12 +54,10 @@ object Supler extends Validators with RenderHints {
*
* By default subforms are rendered as a list. Use the `.renderHint()` method to customize.
*/
def subform[T, ContU, U, Cont[_]](param: T => ContU, form: Form[U], lazyForm: Boolean, createEmpty: () => U)
def subform[T, ContU, U, Cont[_]](param: T => ContU, form: Form[U], createEmpty: () => U)
(implicit container: SubformContainer[ContU, U, Cont]): SubformField[T, ContU, U, Cont] =
macro SuplerFieldMacros.subform_createempty_impl[T, ContU, U, Cont]

//todo add helper for just form passing, instead of function

/**
* A new action field. Must have a unique `name`.
*
Expand Down Expand Up @@ -110,7 +108,7 @@ trait Supler[T] extends Validators {
*
* By default subforms are rendered as a list. Use the `.renderHint()` method to customize.
*/
def subform[ContU, U, Cont[_]](param: T => ContU, form: Form[U], lazyForm: Boolean)
def subform[ContU, U, Cont[_]](param: T => ContU, form: Form[U])
(implicit container: SubformContainer[ContU, U, Cont]): SubformField[T, ContU, U, Cont] =
macro SuplerFieldMacros.subform_impl[T, ContU, U, Cont]

Expand All @@ -120,10 +118,14 @@ trait Supler[T] extends Validators {
*
* By default subforms are rendered as a list. Use the `.renderHint()` method to customize.
*/
def subform[U, ContU, Cont[_]](param: T => ContU, form: Form[U], lazyForm: Boolean, createEmpty: () => U)
def subform[U, ContU, Cont[_]](param: T => ContU, form: Form[U], createEmpty: () => U)
(implicit container: SubformContainer[ContU, U, Cont]): SubformField[T, ContU, U, Cont] =
macro SuplerFieldMacros.subform_createempty_impl[T, ContU, U, Cont]

def modal[U](name: String, read: T => U, write: (T, U) => T, form: Form[U]) =
new SubformField[T, U, U, Id](SubformContainer.singleSubformContainer[U], name, read, write,
None, None, form, None, SubformListRenderHint, AlwaysCondition, AlwaysCondition, true)

/**
* A new action field. Must have a unique `name`.
*
Expand Down
8 changes: 4 additions & 4 deletions supler/src/main/scala/org/supler/SuplerFieldMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ object SuplerFieldMacros {
}

def subform_impl[T: c.WeakTypeTag, ContU, U: c.WeakTypeTag, Cont[_]](c: blackbox.Context)
(param: c.Expr[T => ContU], form: c.Expr[Form[U]], lazyForm: c.Expr[Boolean])
(param: c.Expr[T => ContU], form: c.Expr[Form[U]])
(container: c.Expr[SubformContainer[ContU, U, Cont]]): c.Expr[SubformField[T, ContU, U, Cont]] = {

subform_createempty_impl[T, ContU, U, Cont](c)(param, form, lazyForm, null)(container)
subform_createempty_impl[T, ContU, U, Cont](c)(param, form, null)(container)
}

def subform_createempty_impl[T: c.WeakTypeTag, ContU, U: c.WeakTypeTag, Cont[_]](c: blackbox.Context)
(param: c.Expr[T => ContU], form: c.Expr[Form[U]], lazyForm: c.Expr[Boolean], createEmpty: c.Expr[() => U])
(param: c.Expr[T => ContU], form: c.Expr[Form[U]], createEmpty: c.Expr[() => U])
(container: c.Expr[SubformContainer[ContU, U, Cont]]): c.Expr[SubformField[T, ContU, U, Cont]] = {

import c.universe._
Expand All @@ -117,7 +117,7 @@ object SuplerFieldMacros {
writeFieldValueExpr.splice,
form.splice,
createEmptyOpt.splice,
lazyForm.splice)
false)
}
}

Expand Down
4 changes: 2 additions & 2 deletions supler/src/test/scala/org/supler/FieldOrderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FieldOrderTest extends FlatSpec with ShouldMatchers {
// given
val formWithSubform = form[OrderTestParentClass](f => List(
f.field(_.field1),
f.subform(_.obj, flatForm, false)
f.subform(_.obj, flatForm)
))
val formInst = formWithSubform(OrderTestParentClass("hej", o, Nil))
// when
Expand All @@ -79,7 +79,7 @@ class FieldOrderTest extends FlatSpec with ShouldMatchers {
// given
val formWithSubform = form[OrderTestParentClass](f => List(
f.field(_.field1),
f.subform(_.objList, nonFlatForm, false)
f.subform(_.objList, nonFlatForm)
))
val formInst = formWithSubform(OrderTestParentClass("hej", o, List(o, o)))
// when
Expand Down
16 changes: 8 additions & 8 deletions supler/src/test/scala/org/supler/FrontendTestsForms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ class FrontendTestsForms extends FlatSpec with ShouldMatchers {
writeTestData("complexSubformsList") { writer =>
val complexFormTable = form[ComplexSubformsList](f => List(
f.field(_.field10).label("Field 10"),
f.subform(_.simples, simple1Form, false).renderHint(asTable())
f.subform(_.simples, simple1Form).renderHint(asTable())
))
val complexFormList = form[ComplexSubformsList](f => List(
f.field(_.field10).label("Field 10"),
f.subform(_.simples, simple1Form, false).renderHint(asList())
f.subform(_.simples, simple1Form).renderHint(asList())
))

val objNonEmpty = ComplexSubformsList("c1", List(
Expand All @@ -200,11 +200,11 @@ class FrontendTestsForms extends FlatSpec with ShouldMatchers {
writeTestData("complexSubformsWithRowsList") { writer =>
val complexFormTable = form[ComplexSubformsList](f => List(
f.field(_.field10).label("Field 10"),
f.subform(_.simples, simple1FormWithRows, false).renderHint(asTable())
f.subform(_.simples, simple1FormWithRows).renderHint(asTable())
))
val complexFormList = form[ComplexSubformsList](f => List(
f.field(_.field10).label("Field 10"),
f.subform(_.simples, simple1FormWithRows, false).renderHint(asList())
f.subform(_.simples, simple1FormWithRows).renderHint(asList())
))

val objNonEmpty = ComplexSubformsList("c1", List(
Expand All @@ -223,7 +223,7 @@ class FrontendTestsForms extends FlatSpec with ShouldMatchers {
writeTestData("complexSingleSubform") { writer =>
val complexForm1 = form[ComplexSingleSubform](f => List(
f.field(_.field10).label("Field 10"),
f.subform(_.simple, simple1Form, false)
f.subform(_.simple, simple1Form)
))

val obj1 = ComplexSingleSubform("c1", Simple1("f11", Some("x"), 11, field4 = true))
Expand All @@ -236,7 +236,7 @@ class FrontendTestsForms extends FlatSpec with ShouldMatchers {
writeTestData("complexSingleSubformWithRows") { writer =>
val complexForm1 = form[ComplexSingleSubform](f => List(
f.field(_.field10).label("Field 10"),
f.subform(_.simple, simple1FormWithRows, false)
f.subform(_.simple, simple1FormWithRows)
))

val obj1 = ComplexSingleSubform("c1", Simple1("f11", Some("x"), 11, field4 = true))
Expand All @@ -247,7 +247,7 @@ class FrontendTestsForms extends FlatSpec with ShouldMatchers {
writeTestData("complexOptionalSubform") { writer =>
val complexForm1 = form[ComplexOptionalSubform](f => List(
f.field(_.field10).label("Field 10"),
f.subform(_.simple, simple1Form, false)
f.subform(_.simple, simple1Form)
))

val objSome = ComplexOptionalSubform("c1", Some(Simple1("f11", Some("x"), 11, field4 = true)))
Expand All @@ -272,7 +272,7 @@ class FrontendTestsForms extends FlatSpec with ShouldMatchers {

val complexForm = form[ConditionalComplex](f => List(
f.field(_.f1).label("Field 1"),
f.subform(_.f2, simpleForm, false).label("Simples").enabledIf(_.f1 == "enabled")
f.subform(_.f2, simpleForm).label("Simples").enabledIf(_.f1 == "enabled")
))

writer.writeForm("simpleFormEnabled", simpleForm, ConditionalSimple("v1", "v2"))
Expand Down
17 changes: 10 additions & 7 deletions supler/src/test/scala/org/supler/ProcessTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class ProcessTest extends FlatSpec with ShouldMatchers {
case class WorkPlace(boss: Person, employees: List[Person])

val workPlaceForm = form[WorkPlace] { f => List(
f.subform(_.boss, personForm, true),
f.subform(_.employees, personForm, false)
f.subform(_.boss, personForm),
f.subform(_.employees, personForm)
)
}

Expand All @@ -91,8 +91,12 @@ class ProcessTest extends FlatSpec with ShouldMatchers {

case class Mordor(workplaces: List[WorkPlace])

val modalWorkplacesForm = form[Mordor] {f => List(
f.subform(_.workplaces, workPlaceForm)
)}

val mordorForm = form[Mordor] { f => List(
f.subform(_.workplaces, workPlaceForm, false)
f.modal("mordor", m => m, (m: Mordor, mm: Mordor) => mm, modalWorkplacesForm)
)
}

Expand All @@ -104,14 +108,13 @@ class ProcessTest extends FlatSpec with ShouldMatchers {
val json = mordorForm(mordor).process(
parseJson(
"""{
|"supler_modals":"workplaces[0].boss",
|"supler_modals":"mordor.workplaces[0].boss",
|"mordor": {
|"workplaces": [
| {"boss": {"firstName": "WillShow", "lastName": "WillShow" } },
| {"boss": {"firstName": "WontShow", "lastName": "WontShow" } } ] }""".stripMargin)).generateJSON()
| {"boss": {"firstName": "WillShow", "lastName": "WillShow" } } ] } }""".stripMargin)).generateJSON()

// then
val textjson = pretty(render(json))
textjson should include ("WillShow")
textjson should not include "WontShow"
}
}
8 changes: 4 additions & 4 deletions supler/src/test/scala/org/supler/SuplerActionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class SuplerActionTest extends FlatSpec with ShouldMatchers {
f.action("a") { c => ActionResult(c.copy(name = c.name + "*")) }))

val fb = form[B](f => List(
f.subform(_.cs, fc, false)))
f.subform(_.cs, fc)))

val fa = form[A](f => List(
f.subform(_.bs, fb, false)))
f.subform(_.bs, fb)))

// when
val Some(runnableAction) = fa.findAction(
Expand All @@ -43,10 +43,10 @@ class SuplerActionTest extends FlatSpec with ShouldMatchers {
f.action("a") { c => callLog ::= "c"; remove(c.copy(name = c.name + "*"))}))

def fb(remove: B => ActionResult[B]) = form[B](f => List(
f.subform(_.cs, fc(f.parentAction { (b, j, c) => callLog ::= s"b $j $c"; remove(b)}), false)))
f.subform(_.cs, fc(f.parentAction { (b, j, c) => callLog ::= s"b $j $c"; remove(b)}))))

val fa = form[A](f => List(
f.subform(_.bs, fb(f.parentAction { (a, i, b) => callLog ::= s"a $i $b"; ActionResult(a)}), false)))
f.subform(_.bs, fb(f.parentAction { (a, i, b) => callLog ::= s"a $i $b"; ActionResult(a)}))))

// when
val Some(runnableAction) = fa.findAction(
Expand Down
18 changes: 9 additions & 9 deletions supler/src/test/scala/org/supler/SuplerSubformTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
"subform" should "create a case class list field representation" in {
// when
object PersonMeta extends Supler[PersonManyCars] {
val carsField = subform(_.cars, carForm, false)
val carsField = subform(_.cars, carForm)
}

// then
Expand All @@ -45,7 +45,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
"subform" should "create a case class vector field representation" in {
// when
object PersonMeta extends Supler[PersonManyCarsVector] {
val carsField = subform(_.cars, carForm, false)
val carsField = subform(_.cars, carForm)
}

// then
Expand All @@ -59,7 +59,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
"subform" should "create a case class single field representation" in {
// when
object PersonMeta extends Supler[PersonOneCar] {
val carField = subform(_.car, carForm, false)
val carField = subform(_.car, carForm)
}

// then
Expand All @@ -73,7 +73,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
"subform" should "create a case class optional field representation" in {
// when
object PersonMeta extends Supler[PersonOptionalCal] {
val carField = subform(_.car, carForm, false)
val carField = subform(_.car, carForm)
}

// then
Expand All @@ -91,7 +91,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
import org.supler.Supler._
val personForm = form[PersonManyCars](f => List(
f.field(_.name),
f.subform(_.cars, carForm, false)
f.subform(_.cars, carForm)
))

val jsonInOrder = parseJson("""
Expand Down Expand Up @@ -122,7 +122,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
import org.supler.Supler._
val personForm = form[PersonOneCar](f => List(
f.field(_.name),
f.subform(_.car, carForm, false)
f.subform(_.car, carForm)
))

val json = parseJson("""
Expand All @@ -146,7 +146,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
import org.supler.Supler._
val personForm = form[PersonOptionalCal](f => List(
f.field(_.name),
f.subform(_.car, carForm, false)
f.subform(_.car, carForm)
))

val json1 = parseJson("""
Expand Down Expand Up @@ -177,7 +177,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
// given
val personForm = form[PersonOneCar](f => List(
f.field(_.name),
f.subform(_.car, carForm, true)
f.modal("car", _.car, (p: PersonOneCar, c: Car) => p.copy(car = c), carForm)
))

// when
Expand All @@ -199,7 +199,7 @@ class SuplerSubformTest extends FlatSpec with ShouldMatchers {
// given
val personForm = form[PersonOneCar](f => List(
f.field(_.name),
f.subform(_.car, carForm, true)
f.modal("car", _.car, (p: PersonOneCar, c: Car) => p.copy(car = c), carForm)
))

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SuplerValidationTest extends FlatSpec with ShouldMatchers {
val personForm = form[Person](f => List(f.field(_.size).validate(gt(0))))
val cityForm = form[City](f => List(
f.field(_.name),
f.subform(_.people, personForm, false)
f.subform(_.people, personForm)
))

val c1 = City("city1", List(Person(10), Person(20)))
Expand Down

0 comments on commit 0eceba0

Please sign in to comment.