playframework/Play20 — pull request #283 by knuton (2012-05-04 14:56:26+00:00)
Title: Add ActionBuilder trait

Move the capabilities of the `Action` helper object to a trait which the `Action` helper object then extends.

This allows easy creation of additional helper objects where only the implementation of `apply[A <: AnyContent](bodyParser: BodyParser[A])(block: Request[A] => Result)` needs to be changed. This can be an alternative to using case classes for custom actions.

It would be convenient when wanting to override the default behavior of `Action`, turning

```scala
trait ControllerMagic {
def layout: Layouts.Layout

object BareAction extends play.api.mvc.ActionBuilder

object Action {
def apply[A <: AnyContent](bodyParser: BodyParser[A])(block: Request[A] => Result): Action[A] = {
new Layouts.WithLayout(layout)(new Action[A] {
def parser = bodyParser
def apply(ctx: Request[A]) = block(ctx)
})
}

def apply(block: Request[AnyContent] => Result): Action[AnyContent] = apply(BodyParsers.parse.anyContent)(block)

def apply(block: => Result): Action[AnyContent] = apply(_ => block)
}
}
```

into

```scala
trait ControllerMagic {
def layout: Layouts.Layout

object BareAction extends play.api.mvc.ActionBuilder

object Action extends play.api.mvc.ActionBuilder {
def apply[A <: AnyContent](bodyParser: BodyParser[A])(block: Request[A] => Result): Action[A] = {
new Layouts.WithLayout(layout)(new Action[A] {
def parser = bodyParser
def apply(ctx: Request[A]) = block(ctx)
})
}
}
}
```