Company select field
authorTomas Zeman <tzeman@volny.cz>
Fri, 27 Apr 2012 16:18:28 +0200
changeset 84 43674362ff5e
parent 83 f7553032b302
child 85 3ef84ec492fa
Company select field
src/main/scala/fis/crm/model/Company.scala
src/main/scala/fis/crm/model/CompanyField.scala
src/main/scala/fis/crm/model/CrmSchema.scala
src/main/scala/fis/crm/ui/CompanySnippet.scala
--- a/src/main/scala/fis/crm/model/Company.scala	Fri Apr 27 14:23:34 2012 +0200
+++ b/src/main/scala/fis/crm/model/Company.scala	Fri Apr 27 16:18:28 2012 +0200
@@ -37,6 +37,8 @@
   val postAddress = new OptionalLongField(this) with AddressField with FL
 }
 
-object Company extends Company with MetaRecord[Company]
+object Company extends Company with MetaRecord[Company] with SimpleInjector {
+  object companies extends Inject[Iterable[Company]](() => Nil)
+}
 
 // vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/crm/model/CompanyField.scala	Fri Apr 27 16:18:28 2012 +0200
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2012 Tomas Zeman <tzeman@volny.cz>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package fis.crm.model
+
+import fis.base.model._
+import fis.base.ui.EntityLink
+import net.liftweb.common._
+import net.liftweb.util._
+import scala.xml.NodeSeq
+
+trait CompanyField extends SelectField[Long] with Vendor[Box[Company]] {
+
+  override def asHtml = (for {
+    c <- vend
+    l <- EntityLink(c)
+  } yield l.asHtml) openOr NodeSeq.Empty
+
+  protected def buildDisplayList = 
+    Company.companies() map { c => (Full(c.id), c.linkName) } toSeq
+
+  def vend = valueBox flatMap(CompanyCrud.get _)
+  def make = Full(vend)
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Apr 27 14:23:34 2012 +0200
+++ b/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Apr 27 16:18:28 2012 +0200
@@ -64,8 +64,8 @@
   companyContacts.rightForeignKeyDeclaration.constrainReference(onDelete cascade)
   ContactEntities.register(companyContacts)
 
-  def allCompanies: Iterable[Company] = from(companyT) (c =>
-    select(c) orderBy(c.name asc))
+  Company.companies.default.set { () => from(companyT)(c =>
+    select(c) orderBy(c.name asc)) }
 
 }
 
--- a/src/main/scala/fis/crm/ui/CompanySnippet.scala	Fri Apr 27 14:23:34 2012 +0200
+++ b/src/main/scala/fis/crm/ui/CompanySnippet.scala	Fri Apr 27 16:18:28 2012 +0200
@@ -73,7 +73,7 @@
   private def cur = viewLoc.currentValue or editLoc.currentValue or
     deleteLoc.currentValue
 
-  private def list: CssTr = { _ => CompanyTable(CrmSchema.allCompanies) }
+  private def list: CssTr = { _ => CompanyTable(Company.companies()) }
 
   private def panel: CssTr = "*" #> cur.map(CompanyPanel(_))