Skip to content

Instantly share code, notes, and snippets.

@k4200
Created February 2, 2011 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4200/807923 to your computer and use it in GitHub Desktop.
Save k4200/807923 to your computer and use it in GitHub Desktop.
Extending CRUDify, but this won't compile...
/**
* コンパイル通りません。
*
* やりたい事:
* 1. ProtoUserクラスを継承した任意のクラス(仮にUser)と、モデルクラス(Foo)の2つが存在。
* 2. Fooには、どのユーザーがレコードを作成したかを表すフィールドがあり、Userテーブルへの外部キーとなっている。
* 3. レコードの所有者のみがCRUDの機能を使えるよう。
* 4. 2, 3の機能はtraitにして、Foo以外のクラスでも同様の機能を使えるようにしたい。
*
* Liftのソースをにらめっこしながら頑張ったんだけど・・・
*
* This won't compile.
* What I want to achieve:
* 1. Have a class that extends ProtoUser and a model class. (User and Foo respectively)
* 2. The table for Foo has a field that indicates who created a record and a foreign key to User.
* 3. Only the owner of the record can use the CRUD features.
* 4. 2 and 3 should be a trait so I can use it for other model classes.
*
*/
import net.liftweb._
import common._;
import http._;
import mapper._
import sitemap._;
import Loc._;
import proto.{ProtoUser => GenProtoUser}
trait UserEditableCRUDify [KeyType,
CrudType <: UserEditableKeyedMapper[KeyType, CrudType]]
//TODO UserType <: ProtoUser[UserType] ?
extends CRUDify [KeyType, CrudType] {
self: CrudType with KeyedMetaMapper[KeyType, CrudType] =>
//looks ok, but not tested
override protected def doDeleteSubmit(item: TheCrudType, from: String)() = {
item.userObject.currentUserId match {
case Full(userId) =>
if(userId == item.userId.is) {
S.notice(S ? "Deleted")
item.delete_!
}
S.redirectTo(from)
case _ => S.redirectTo("/user_mgt/login")
}
}
}
trait UserEditableKeyedMapper[KeyType, OwnerType <: KeyedMapper[KeyType, OwnerType]]
extends KeyedMapper[KeyType, OwnerType] {
self: OwnerType =>
// 下の方の問題のコードのコンパイルが通ればこれはいらない。
val userObject: GenProtoUser
//追加:最初書き忘れてた
type UserType <: GenProtoUser //??
// このへんをどうやって書けばいいか分からない・・・
lazy val user: MappedLongForeignKey[OwnerType, KeyedMetaMapper[Long, UserType]]
= new MyUserField(this, userMeta)
protected class MyUserField[OwnerType, UserType](obj: OwnerType, _foreignMeta: KeyedMetaMapper[Long, UserType])
extends MappedLongForeignKey(obj, _foreignMeta)
// 色々試したけどダメ。
def userMeta: KeyedMetaMapper[Long, UserType] = userObject.asInstanceOf[KeyedMetaMapper[Long, UserType]]
// これは当然OK
lazy val userId: MappedLong[OwnerType] = new MyUserId(this)
protected class MyUserId(obj: OwnerType) extends MappedLong(obj) {
override def dbIndexed_? = true
override def dbDisplay_? = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment