Skip to content

Latest commit

 

History

History
117 lines (95 loc) · 3.9 KB

play-framework.md

File metadata and controls

117 lines (95 loc) · 3.9 KB

PLAY FRAMEWORK

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

PLAY PLUGIN

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.

ActivatePlayContext

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

ActivatePlayTest

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

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.

FILLWITH

To fill a form with an entity instance attributes, use the “fillWith” method:

computerForm.fillWith(aComputerInstance)

BINDFROMREQUEST

To fill a form with the request values, use the “bindFromRequest” method:

computerForm.bindFromRequest

UPDATEENTITY

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))
    })

ASYNCUPDATEENTITY

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))
        }
    })

CREATEENTITY

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))
    })

ASYNCCREATEENTITY

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))
        }
    })