mandubian/play-autosource — pull request #19 by dwhjames (2013-10-16 06:07:55+00:00)
Title: Improve Play Autosource for Datomisca

This pull request makes some fairly major internal changes to the `DatomiscaAutoSourceController`.

The most significant change is to how actions are built. The operations of the controller are grouped into ‘get’, ‘insert’, ‘update’, and ‘delete’. Any and all of these are able to be precisely customized by overriding the following members of `DatomiscaAutoSourceController`:

```scala
protected lazy val defaultAction: ActionBuilder[Request] = Action
protected lazy val getAction: ActionBuilder[Request] = defaultAction
protected lazy val insertAction: ActionBuilder[Request] = defaultAction
protected lazy val updateAction: ActionBuilder[Request] = defaultAction
protected lazy val deleteAction: ActionBuilder[Request] = defaultAction
```

(side note: `val` leads to initialization problems, so I went with `lazy val`, but maybe `def` leaves more options.)

All the controller methods are defined in terms of these:
```scala
override def get(id: Long) =
getAction.async {
…
}
```

The other key customization point is exception handling. All error cases in the category of invalid request data now invoke the following method:

```scala
protected def onBadRequest(request: RequestHeader, error: String): Future[SimpleResult] =
Play.maybeApplication map { app =>
app.global.onBadRequest(request, error)
} getOrElse {
Future.successful(BadRequest)
}
```

The default implementation (adapted from somewhere in Play internals) simply tries to pass it on to the globally defined `onBadRequest` method. But obviously this method is available for overriding.

I think this method is actually worth lifting into the core `AutoSourceController` trait. Similarly, the overridable action builders could also be lifted there, for use in other implementations of autosource.