I've got this so far. The CustomerFields trait just appends the
customer's object to the class.
The Customerize trait is for extending the singleton.

Firs thing, Jeppe wrote to use the find method, which doesn't seem to
be available there. At least it doesn't find it.
So i switched to findAll (which works), but the problem is, that the
By-Clause i am trying to create, does not work.

How can i get A's customer Property? self and A don't work. Even if i
extend self: A with CustomerFields[A].

trait Customerize[A <: MetaMapper[A] with CustomerFields[A]] {

        self: A =>

        def findForCustomer(params: QueryParam[A]*): A = {
                var seq = Seq(MaxRows(1))
                if (!User.isAdmin_?) seq += By(self.customer, User.customerId)
                self.findAll((seq ++ params): _*)
        }

        def findAllForCustomer(params: QueryParam[A]*): A = {
                var seq = Seq()
                if (!User.isAdmin_?) seq += By(self.customer, User.customerId)
                self.findAll((seq ++ params): _*)
        }


}


trait CustomerFields[A <: Mapper[A]] {

        this: A with Mapper[A] =>

        private val thisTyped = this.asInstanceOf[A]


        object customer extends MappedLongForeignKey(thisTyped, Customer)
with LifecycleCallbacks {

                override def dbNotNull_? = true

                override def beforeCreate = {
                        User.currentUser match {
                                case Full(u: User) => 
Customer.find(By(Customer.id,
u.customer.get)) match {
                                        case Full(c: Customer) => this(c)
                                        case _ => Log.error("Customer *not* 
found! This shouldn't
happen!"); false
                                }
                                case _ => Log.error("User *not* found! This 
shouldn't happen!");
false
                        }
                }

        }

}

On 21 Jan., 18:47, Franz Bettag <[email protected]> wrote:
> I will implement it into my own MetaMapper-sub-trait so all of my
> models can share the code.
>
> Thank you all for your input!
>
> On Jan 21, 9:44 am, Jeppe Nejsum Madsen <[email protected]> wrote:
>
> > Naftoli Gugenheim <[email protected]> writes:
> > > Why not define a new method?
> > > def findByCurUser(params: QueryParam*) = User.currentUser.flatMap(user=> 
> > > find((Seq(By(User.username, user.username.is)) ++ params): _*))
> > > find returns a Box and currentUser does too, so flatMap flattens them 
> > > into one. Basically find takes a varargs and you're passing a Seq instead 
> > > using : _*, and that Seq contains the user and other params.
> > > You could probably write params + By(User.username, user.username.is) 
> > > instead of the reverse order, as it's shorter than the previous version.
>
> > The problem with this solution is that you cannot use any of Lift's
> > builtin stuff such as CRUDify.
>
> > If you wan't to create a multitenant service with only a single set of 
> > tables, you wan't do make damn
> > sure that each customer only sees his/her own data :-)
>
> > /Jeppe

-- 
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.

Reply via email to