Reputation: 1688
I am trying to eliminate some redundancy in the models in my lift application that uses Mapper and CRUDify (see below for the models). I repeatedly override some class members in the object definition as follows:
override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
I would like to make a trait that takes care of this, so that I can mix in the trait as follows:
object Test1 extends Test1 with LongKeyedMetaMapper[Test1] with CRUDify[Long, Test1] with MyNewTrait {
where MyNewTrait would be defined something like:
trait MyNewTrait {
override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
}
I guess this trait would have to have access to the User model. Can anyone advise on how to write the trait? As written above, the trait is incomplete. I can't figure out what to put after trait MyNewTrait
Thanks,
/* ********************************************************************************************* */
object Test1 extends Test1 with LongKeyedMetaMapper[Test1] with CRUDify[Long, Test1] {
override def dbTableName = "test1"
override def fieldOrder = List(c1, c2, c3)
override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
}
class Test1 extends LongKeyedMapper[Test1] with IdPK {
def getSingleton = Test1
object c1 extends MappedString(this, 50)
object c2 extends MappedString(this, 50)
object c3 extends MappedString(this, 50)
}
/* ********************************************************************************************* */
object Test1 extends Test1 with LongKeyedMetaMapper[Test1] with CRUDify[Long, Test1] {
override def dbTableName = "test2"
override def fieldOrder = List(c1, c2, c3)
override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
}
class Test1 extends LongKeyedMapper[Test1] with IdPK {
def getSingleton = Test1
object c1 extends MappedString(this, 50)
object c2 extends MappedString(this, 50)
object c3 extends MappedString(this, 50)
}
Upvotes: 1
Views: 45
Reputation: 16412
I don't have a project handy to check the code but this should work:
trait MyNewTrait[KeyType, CrudType <: KeyedMapper[KeyType, CrudType]]
extends CRUDify[KeyType, CrudType] {
self: CrudType with KeyedMetaMapper[KeyType, CrudType] =>
override def showAllMenuLocParams: List[Loc.AnyLocParam] =
List(If(User.loggedIn_? _, "Not logged in"))
override def createMenuLocParams: List[Loc.AnyLocParam] =
List(If(User.loggedIn_? _, "Not logged in"))
}
You can mix it instead of CRUDify
trait afterwards.
Since you using Long
key type you can also extend LongCRUDify
in a similar way having one type parameter less.
Upvotes: 3