Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue 1118 - Make MongoMetaRecord.useColl as public and move up to Mo…

…ngoMeta
  • Loading branch information...
commit 993026cf1351639e8fd86824556c191289791a40 1 parent ddf1cb8
Tim Nelson eltimn authored
4 persistence/mongodb-record/src/main/scala/net/liftweb/mongodb/record/BsonRecord.scala
View
@@ -124,8 +124,8 @@ trait BsonMetaRecord[BaseRecord <: BsonRecord[BaseRecord]] extends MetaRecord[Ba
* using setFromAny passing it the DBObject returned from Mongo.
*
* @param inst - the record that will be populated
- * @param obj - The DBObject
- * @return Box[BaseRecord]
+ * @param dbo - The DBObject
+ * @return Unit
*/
def setFieldsFromDBObject(inst: BaseRecord, dbo: DBObject): Unit = {
for (k <- dbo.keySet; field <- inst.fieldByName(k.toString)) {
5 persistence/mongodb-record/src/main/scala/net/liftweb/mongodb/record/MongoMetaRecord.scala
View
@@ -63,9 +63,6 @@ trait MongoMetaRecord[BaseRecord <: MongoRecord[BaseRecord]]
true
}
- protected def useColl[T](f: DBCollection => T) =
- MongoDB.useCollection(mongoIdentifier, collectionName)(f)
-
def bulkDelete_!!(qry: DBObject): Unit = {
useColl(coll =>
coll.remove(qry)
@@ -276,7 +273,7 @@ trait MongoMetaRecord[BaseRecord <: MongoRecord[BaseRecord]]
* Update records with a JObject query
*/
def update(qry: JObject, newbr: BaseRecord, opts: UpdateOption*) {
- MongoDB.use(mongoIdentifier) ( db =>
+ useDb ( db =>
update(qry, newbr, db, opts :_*)
)
}
2  persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoFieldSpec.scala
View
@@ -27,7 +27,7 @@ import org.specs.Specification
import common._
import json.{Num => _, _}
-import JsonDSL._
+import BsonDSL._
import util.Helpers.randomString
import http.{LiftSession, S}
import http.js.JE._
58 persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoRecordSpec.scala
View
@@ -31,6 +31,7 @@ import JsonDSL._
import net.liftweb.record.field.Countries
+import com.mongodb._
/**
* Systems under specification for MongoRecord.
@@ -560,6 +561,63 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
rftr.mandatoryLongRefListField.objs mustEqual List(btr)
}
+ "use defaultValue when field is not present in the database" in {
+ checkMongoIsRunning
+
+ val missingFieldDocId = ObjectId.get
+
+ // create a dbobject with no fields manually
+ val builder = BasicDBObjectBuilder.start
+ .add("_id", missingFieldDocId)
+
+ FieldTypeTestRecord.useColl { coll => coll.save(builder.get) }
+
+ val recFromDb = FieldTypeTestRecord.find(missingFieldDocId)
+
+ recFromDb must notBeEmpty
+
+ recFromDb foreach { r =>
+ r.mandatoryBooleanField.is must_== false
+ r.legacyOptionalBooleanField
+ r.optionalBooleanField.is must beEmpty
+ r.mandatoryCountryField.is must_== Countries.C1
+ r.legacyOptionalCountryField.valueBox must beEmpty
+ r.optionalCountryField.is must beEmpty
+ r.mandatoryDecimalField.is must_== 0.00
+ r.legacyOptionalDecimalField.valueBox must beEmpty
+ r.optionalDecimalField.is must beEmpty
+ r.mandatoryDoubleField.is must_== 0d
+ r.legacyOptionalDoubleField.valueBox must beEmpty
+ r.optionalDoubleField.is must beEmpty
+ r.mandatoryEmailField.is must_== ""
+ r.legacyOptionalEmailField.valueBox must beEmpty
+ r.optionalEmailField.is must beEmpty
+ r.mandatoryEnumField.is must_== MyTestEnum.ONE
+ r.legacyOptionalEnumField.valueBox must beEmpty
+ r.optionalEnumField.is must beEmpty
+ r.mandatoryIntField.is must_== 0
+ r.legacyOptionalIntField.valueBox must beEmpty
+ r.optionalIntField.is must beEmpty
+ r.mandatoryLocaleField.is must_== "en_US"
+ r.legacyOptionalLocaleField.valueBox must beEmpty
+ r.optionalLocaleField.is must beEmpty
+ r.mandatoryLongField.is must_== 0L
+ r.legacyOptionalLongField.valueBox must beEmpty
+ r.optionalLongField.is must beEmpty
+ r.mandatoryPostalCodeField.is must_== ""
+ r.legacyOptionalPostalCodeField.valueBox must beEmpty
+ r.optionalPostalCodeField.is must beEmpty
+ r.mandatoryStringField.is must_== ""
+ r.legacyOptionalStringField.valueBox must beEmpty
+ r.optionalStringField.is must beEmpty
+ r.mandatoryTextareaField.is must_== ""
+ r.legacyOptionalTextareaField.valueBox must beEmpty
+ r.optionalTextareaField.is must beEmpty
+ r.mandatoryTimeZoneField.is must_== "America/Chicago"
+ r.legacyOptionalTimeZoneField.valueBox must beEmpty
+ r.optionalTimeZoneField.is must beEmpty
+ }
+ }
}
}
73 persistence/mongodb/src/main/scala/net/liftweb/mongodb/MongoMeta.scala
View
@@ -19,7 +19,7 @@ import org.bson.types.ObjectId
import json.{DefaultFormats, Formats}
import json.JsonAST.JObject
-import com.mongodb.{BasicDBObject, DB, DBObject}
+import com.mongodb.{BasicDBObject, DB, DBCollection, DBObject}
trait JsonFormats {
// override this for custom Formats
@@ -65,22 +65,25 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
def mongoIdentifier: MongoIdentifier = DefaultMongoIdentifier
/*
+ * Use the collection associated with this Meta.
+ */
+ def useColl[T](f: DBCollection => T) =
+ MongoDB.useCollection(mongoIdentifier, collectionName)(f)
+
+ /*
+ * Use the db associated with this Meta.
+ */
+ def useDb[T](f: DB => T) = MongoDB.use(mongoIdentifier)(f)
+
+ /*
* Count all documents
*/
- def count: Long = {
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
- coll.getCount
- )
- }
+ def count: Long = useColl { coll => coll.getCount }
/*
* Count documents by DBObject query
*/
- def count(qry: DBObject):Long = {
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
- coll.getCount(qry)
- )
- }
+ def count(qry: DBObject):Long = useColl { coll => coll.getCount(qry) }
/*
* Count documents by JObject query
@@ -90,20 +93,14 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
/*
* Count distinct records on a given field
*/
- def countDistinct(key: String, query: DBObject): Long = {
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
- coll.distinct(key, query).size
- )
- }
+ def countDistinct(key: String, query: DBObject): Long =
+ useColl { coll => coll.distinct(key, query).size }
/*
* Delete documents by a DBObject query
*/
- def delete(qry: DBObject) {
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
- coll.remove(qry)
- )
- }
+ def delete(qry: DBObject): Unit =
+ useColl { coll => coll.remove(qry) }
// delete a document
def delete(k: String, v: Any) {
@@ -116,45 +113,35 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
/*
* Delete documents by a JObject query
*/
- def delete(qry: JObject) {
- delete(JObjectParser.parse(qry))
- }
+ def delete(qry: JObject): Unit = delete(JObjectParser.parse(qry))
/* drop this document collection */
- def drop {
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
- coll.drop
- )
- }
+ def drop: Unit = useColl { coll => coll.drop }
/*
* Ensure an index exists
*/
- def ensureIndex(keys: JObject) {
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll => {
- coll.ensureIndex(JObjectParser.parse(keys))
- })
- }
+ def ensureIndex(keys: JObject): Unit =
+ useColl { coll => coll.ensureIndex(JObjectParser.parse(keys)) }
/*
* Ensure an index exists and make unique
*/
- def ensureIndex(keys: JObject, unique: Boolean) {
+ def ensureIndex(keys: JObject, unique: Boolean): Unit = {
val options = new BasicDBObject
if (unique) options.put("unique", true)
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll => {
+ useColl { coll =>
coll.ensureIndex(JObjectParser.parse(keys), options)
- })
+ }
}
/*
* Ensure an index exists with options
*/
- def ensureIndex(keys: JObject, opts: JObject) {
- MongoDB.useCollection(mongoIdentifier, collectionName) ( coll => {
+ def ensureIndex(keys: JObject, opts: JObject): Unit =
+ useColl { coll =>
coll.ensureIndex(JObjectParser.parse(keys), JObjectParser.parse(opts))
- })
- }
+ }
/*
* Update document with a DBObject query using the given Mongo instance.
@@ -185,9 +172,7 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
* Update document with a JObject query.
*/
def update(qry: JObject, newobj: JObject, opts: UpdateOption*) {
- MongoDB.use(mongoIdentifier) ( db => {
- update(qry, newobj, db, opts :_*)
- })
+ useDb { db => update(qry, newobj, db, opts :_*) }
}
}
25 persistence/mongodb/src/test/scala/net/liftweb/mongodb/MongoDocumentExamplesSpec.scala
View
@@ -17,6 +17,8 @@
package net.liftweb
package mongodb
+import BsonDSL._
+
import java.util.{Calendar, Date, UUID}
import java.util.regex.Pattern
@@ -27,7 +29,6 @@ import org.specs.Specification
import json.DefaultFormats
import json.JsonParser._
-import json.JsonDSL._
package mongotestdocs {
@@ -175,7 +176,7 @@ package mongotestdocs {
/**
* Systems under specification for MongoDocumentExamples.
*/
-object MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Specification") with MongoTestKit {
+class MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Specification") with MongoTestKit {
import mongotestdocs._
override def dbName = "lift_mongodocumentexamples"
@@ -200,16 +201,14 @@ object MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Sp
p mustEqual pFromDb.get
// retrieve it using a Json query
- def pFromDbViaJson = SimplePerson.find(("_id" -> ("$oid" -> p._id.toString)))
+ def pFromDbViaJson = SimplePerson.find(("_id" -> pid))
pFromDbViaJson.isDefined must_== true
p mustEqual pFromDbViaJson.get
// modify and save the person
- // with scala 2.8 you can use the copy function to do this
- // val p3 = p.copy(name = "Tim3")
- val p2 = SimplePerson(p._id, "Timm", 27)
+ val p2 = p.copy(name="Timm", age=27)
p2.save
pFromDb.isDefined must_== true
p2 must_== pFromDb.get
@@ -220,16 +219,16 @@ object MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Sp
all.isEmpty must_== false
- if (!debug) {
- all.size must_== 1
- all.head must_== p2
+ all.size must_== 1
+ all.head must_== p2
- // delete it
- p2.delete
+ // delete it
+ p2.delete
- pFromDb.isEmpty must_== true
- pFromDbViaJson.isEmpty must_== true
+ pFromDb.isEmpty must_== true
+ pFromDbViaJson.isEmpty must_== true
+ if (!debug) {
SimplePerson.drop
}
}
2  web/webkit/src/main/scala/net/liftweb/http/LiftScreen.scala
View
@@ -250,7 +250,7 @@ trait AbstractScreen extends Factory {
stuff)
/**
- * Add a validtion (the wacky symbols are supposed to look like a check mark)
+ * Add a validation (the wacky symbols are supposed to look like a check mark)
*/
def ^/(f: T => List[FieldError]): FieldBuilder[T] =
new FieldBuilder[T](name, default,
Please sign in to comment.
Something went wrong with that request. Please try again.