Skip to content

Instantly share code, notes, and snippets.

@arashi01
Forked from jgoday/Boot.scala
Created March 31, 2013 18:29
Show Gist options
  • Save arashi01/5281527 to your computer and use it in GitHub Desktop.
Save arashi01/5281527 to your computer and use it in GitHub Desktop.
package bootstrap.liftweb
import net.liftweb._
import util._
import Helpers._
import common._
import http._
import sitemap._
import Loc._
import net.liftmodules.JQueryModule
import net.liftweb.http.js.jquery._
import code.model.{Coffee, Coffees}
import code.lib.DependencyFactory
import slick.session.Database
/**
* A class that's instantiated early and run. It allows the application
* to modify lift's environment
*/
class Boot {
def boot {
// where to search snippet
LiftRules.addToPackages("code")
// Build SiteMap
val entries = List(
Menu.i("Home") / "index", // the simple way to declare a menu
// more complex because this menu allows anything in the
// /static path to be visible
Menu(Loc("Static", Link(List("static"), true, "/static/index"),
"Static Content")))
// set the sitemap. Note if you don't want access control for
// each page, just comment this line out.
LiftRules.setSiteMap(SiteMap(entries:_*))
//Show the spinny image when an Ajax call starts
LiftRules.ajaxStart =
Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)
// Make the spinny image go away when it ends
LiftRules.ajaxEnd =
Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)
// Force the request to be UTF-8
LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
// Use HTML5 for rendering
LiftRules.htmlProperties.default.set((r: Req) =>
new Html5Properties(r.userAgent))
//Init the jQuery module, see http://liftweb.net/jquery for more information.
LiftRules.jsArtifacts = JQueryArtifacts
JQueryModule.InitParam.JQuery=JQueryModule.JQuery172
JQueryModule.init()
DependencyFactory.database.default.set(
() => Database.forURL("jdbc:h2:mem:test2", driver = "org.h2.Driver"))
}
}
package code
package model
import SlickProfile._
import SlickProfile.driver._
/**
* User: ggarcia
* Date: 3/27/13
* Time: 6:16 PM
*/
case class Coffee(name: String, supId: Int, price: Double)
object Coffees extends SlickTable[Coffee]("coffees") {
def name = column[String]("cof_name", O.PrimaryKey)
def supID = column[Int]("sup_id")
def price = column[Double]("price")
def * = name ~ supID ~ price <>(Coffee, Coffee.unapply _)
def listAll = withSession {
createAll()
Query(Coffees).list()
}
def createAll() {
(Coffees.ddl).create
Coffees.insertAll(
Coffee("Colombian", 101, 7.99),
Coffee("Colombian_Decaf", 101, 8.99),
Coffee("French_Roast_Decaf", 49, 9.99)
)
}
}
package code
package lib
import net.liftweb._
import http._
import util._
import common._
import java.util.Date
import slick.session.Database
/**
* A factory for generating new instances of Date. You can create
* factories for each kind of thing you want to vend in your application.
* An example is a payment gateway. You can change the default implementation,
* or override the default implementation on a session, request or current call
* stack basis.
*/
object DependencyFactory extends Factory {
implicit object time extends FactoryMaker(Helpers.now _)
implicit object database extends FactoryMaker(
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver"))
/**
* objects in Scala are lazily created. The init()
* method creates a List of all the objects. This
* results in all the objects getting initialized and
* registering their types with the dependency injector
*/
private def init() {
List(time, database)
}
init()
}
/*
/**
* Examples of changing the implementation
*/
sealed abstract class Changer {
def changeDefaultImplementation() {
DependencyFactory.time.default.set(() => new Date())
}
def changeSessionImplementation() {
DependencyFactory.time.session.set(() => new Date())
}
def changeRequestImplementation() {
DependencyFactory.time.request.set(() => new Date())
}
def changeJustForCall(d: Date) {
DependencyFactory.time.doWith(d) {
// perform some calculations here
}
}
}
*/
package code
package model
import scala.slick.driver.H2Driver
import scala.slick.driver.H2Driver.simple._
import slick.session.Database
abstract class SlickTable[T](name: String) extends Table[T](name) {
def conn = lib.DependencyFactory.inject[Database].openOr(null)
def withSession[A](block : => A): A = {
conn withSession { block }
}
}
object SlickProfile {
val driver = H2Driver.simple
implicit val session: slick.session.Session = Database.threadLocalSession
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment