Skip to content

Instantly share code, notes, and snippets.

import org.bowlerframework.controller.Controller
import org.bowlerframework.model.{ ParameterMapper, Validations}
import org.bowlerframework.view.{Renderable}
/**
*
* extends:
* - Controller: used to construct routes and deal with them by providing functions that respond to routes.
* - ParameterMapper: takes a request and maps any values into beans or other objects.
* - Validations: validation enables the Controller
@wfaler
wfaler / validator.scala
Created January 18, 2011 01:05
validator.scala
import com.recursivity.commons.validator._
import org.bowlerframework.model.DefaultModelValidator
/**
* Validates a widget - requires a .properties file on the classpath with the same package and class as this class.
*/
class WidgetValidator(widget: Widget) extends DefaultModelValidator(classOf[WidgetValidator]) {
// adding individual field validators. The string key is the key used to lookup the property name for the error message.
@wfaler
wfaler / validator2.scala
Created January 18, 2011 01:11
validator2.scala
/**
* Checks uniqueness of a Widget for create
*/
class UniqueValidator(func: => Long) extends Validator {
def getKey = "id"
def getReplaceModel = List[Tuple2[String, Any]]()
def isValid: Boolean = {
if (Widgets.find(func) == None) return true
@wfaler
wfaler / WidgetValidator.properties
Created January 18, 2011 01:17
WidgetValidator.properties
NotNullOrNoneValidator={key} cannot be null or empty
StringLengthValidator={key} must be between {min} and {max} characters long
MaxIntValidator={key} can be at most {max}
MinIntValidator={key} must be at least {min}
MinLongValidator={key} must be at least {min}
UniqueValidator={key} must be unique
id=ID
name=Name
yearMade=Year Made
@wfaler
wfaler / new.mustache
Created January 18, 2011 01:28
new.mustache
<h3>Create new Widget</h3>
<ul>
{{#validationErrors}}
<li>{{.}}</li>
{{/validationErrors}}
</ul>
{{#widget}}
<form method="POST" action="/widgets">
@wfaler
wfaler / newwidget.scala
Created January 18, 2011 01:29
newwidget.scala
// form for creating a new Widget - passes in a new, empty widget to be filled out.
get ("/widgets/new")((request, response) => {render(Widget(0, null, 0))})
@wfaler
wfaler / RenderStrategy.scala
Created January 20, 2011 23:53
RenderStrategy.scala
package org.bowlerframework.view
import org.bowlerframework.{Request, Response}
/**
* Strategy for resolving a ViewRenderer given a request
*/
trait RenderStrategy{
def resolveViewRenderer(request: Request): ViewRenderer
@wfaler
wfaler / reuse.ssp
Created January 26, 2011 00:20
reuse.ssp
<% import org.bowlerframework.view.scalate.SimpleComponent._ %>
<%@ val tuple: scala.Tuple2[String, String] %>
Here is your template body..
${unescape(show(tuple))}
..here's the rest of your template body
@wfaler
wfaler / crudsetup.scala
Created February 2, 2011 01:00
crudsetup.scala
import org.squeryl.Schema
// setup Squeryl Schema with a single entity type/table
object Library extends Schema {
val authors = table[Author]("authors")
}
// setup CRUD Controller - gives you full CRUD serverside behavior with JSON rendering etc!
val dao = new LongKeyedDao[Author](authors)
@wfaler
wfaler / intercepting.scala
Created February 16, 2011 00:15
intercepting.scala
class SquerylController extends InterceptingController {
def around(request: Request, response: Response)(controller: (Request, Response) => Unit) = {
val session = SessionFactory.newSession
session.bindToCurrentThread
try{
transaction{
controller(request, response)
}
}finally{
session.close