The “activate-play” component has some classes to facilitate the Activate usage with the Play Framework 2.2.
Take a look at the activate-example-play project. It is based on the computer-database example.
https://github.com/fwbrasil/activate-example-play
There also a non-blocking version:
https://github.com/fwbrasil/activate-example-play-async
To support play application reload, add this line to the conf/play.plugins file (create it if necessary):
100:net.fwbrasil.activate.play.ActivatePlayPlugin
The plugin has full entity class loading/reloading support. It also supports migration loading/reloading, but not running already executed migrations.
It is possible to define a persistence context using the common Activate approach, but the ActivatePlayContext provides an integration to define the storage based on the Play configurations:
import net.fwbrasil.activate.ActivateContext
import net.fwbrasil.activate.play.ActivatePlayContext
object computerPersistenceContext extends ActivatePlayContext
The ActivatePlayTest trait provides some useful methods to write Play tests.
"some test using activate" inActivate {
// test code
}
"some integration test using activate" inBrowserWithActivate { browser =>
// test code
}
Entity form is a Play Form specialized in Activate entities. Constructor example:
val computerForm =
EntityForm[Computer](
_.name -> nonEmptyText,
_.introduced -> optional(date("yyyy-MM-dd")),
_.discontinued -> optional(date("yyyy-MM-dd")),
_.company -> optional(entity[Company]))
Each attribute is referenced by his accessor in a type-safe way in the left. The right side determines the play form properties.
The entity form just hold attributes values. It doesn’t holds entity instances.
To fill a form with an entity instance attributes, use the “fillWith” method:
computerForm.fillWith(aComputerInstance)
To fill a form with the request values, use the “bindFromRequest” method:
computerForm.bindFromRequest
To update an entity instance with the form values use the “updateEntity” method:
computerForm.bindFromRequest.fold(
formWithErrors => BadRequest(html.editForm(id, formWithErrors)),
computerData => {
val computer = computerData.updateEntity(id)
Home.flashing("success" -> "Computer %s has been updated".format(computer.name))
})
Async variation example:
computerForm.bindFromRequest.fold(
formWithErrors =>
Future.successful(BadRequest(html.editForm(id, formWithErrors, companyOptions))),
computerData => {
computerData.asyncUpdateEntity(id).map { computer =>
Home.flashing("success" -> "Computer %s has been updated".format(computer.name))
}
})
To create an entity instance with the form values use the “createEntity” method:
computerForm.bindFromRequest.fold(
formWithErrors => BadRequest(html.createForm(formWithErrors)),
computerData => {
val computer = computerData.createEntity
Home.flashing("success" -> "Computer %s has been created".format(computer.name))
})
Async variation example:
computerForm.bindFromRequest.fold(
formWithErrors =>
Future.successful(BadRequest(html.createForm(formWithErrors, companyOptions))),
computerData => {
computerData.asyncCreateEntity.map { computer =>
Home.flashing("success" -> "Computer %s has been created".format(computer.name))
}
})