Permalink
Browse files

buildout

  • Loading branch information...
1 parent 728f023 commit 737c5c87e5f97e5f5ea9e335fa7a676a58a960df @brynbellomy committed Sep 19, 2011
Showing with 69 additions and 32 deletions.
  1. +55 −9 classes/model/merlinModel.js
  2. +2 −2 plugins/comments/classes/commentable.js
  3. +12 −21 plugins/users/classes/user.js
@@ -126,22 +126,68 @@ function merlinModel() { this.fullConstruct() }
/**
* validates the object in preparation for saving it to the database
+ *
+ * @param callback
+ * function(err, merge)
*/
presave: function(callback) {
var myself = this
- this.ensureHasId(function() {
- myself.type = myself.getClassname()
- myself._rev = myself._rev || null
- callback(null)
+
+ myself.type = myself.getClassname()
+ myself._rev = myself._rev || null
+
+ myself.ensureHasId(function() {
+ myself.hasRequiredFields(function(hasFields) {
+ if (!hasFields) {
+ myself.idExists(myself._id, function(err, exists) {
+ if (err) { // @@TODO
+ callback('Error')
+ return
+ }
+
+ if (!exists)
+ callback({error: 'Missing required field(s).'})
+ else
+ callback(null, true)
+ })
+ }
+ else
+ callback(null, false)
+ })
})
},
+ hasRequiredFields: function(callback) {
+ var fields = this.requiredFields()
+ for (var x in fields) {
+ if (!this[fields[x]]) {
+ callback(false)
+ return
+ }
+ }
+ callback(true)
+ },
+
+ requiredFields: function() {
+ return []
+ },
+
/**
* saves the object to the database
+ *
+ * @param merge
+ * boolean. if true, object should be merged rather than saved.
*/
- dbSave: function(callback) {
- var myself = this
- this.merlin().db().save(this._id, this, function (err, response) {
+ dbSave: function(callback, merge) {
+ var myself = this,
+ saveFunc
+
+ if (merge)
+ saveFunc = this.merlin().db().merge
+ else
+ saveFunc = this.merlin().db().save
+
+ saveFunc(this._id, this, function (err, response) {
if (err) {
console.error('Error saving object to database.', err)
callback(err, null)
@@ -161,12 +207,12 @@ function merlinModel() { this.fullConstruct() }
save: function(callback) {
var myself = this
- myself.presave(function(err) {
+ myself.presave(function(err, merge) {
if (err) { // @@TODO ??
callback(err)
return
}
- myself.dbSave(callback)
+ myself.dbSave(callback, merge)
})
},
@@ -35,8 +35,8 @@ function commentable() { this.fullConstruct() }
// remove the "comments" convenience array so data isn't duplicated in the db
if (this.comments)
delete this.comments
-
- commentable.prototype.presave.super('merlinModel').call(this, callback)
+
+ callback()
}
}
@@ -102,38 +102,29 @@ function user() { this.fullConstruct() }
user.methods = {
constructFrom: function(baseType, base) {
if (baseType == 'document') {
- if (base.type != this.getClassname()) {
+ if (base.type != this.getClassname())
return false
- }
}
- for (i in base) {
+ for (i in base)
this[i] = base[i]
- }
+
return true
},
generateId: function() {
return this.getClassname() + '-' + this.username
},
-
- follow: function(username) {
-
+
+ requiredFields: function() {
+ return ['username', 'password', 'email']
},
-
- save: function(callback, updatePassword) {
- if (typeof updatePassword == 'undefined')
- updatePassword = true
-
- if (this.username == null || (updatePassword && this.password == null) || this.email == null) {
- callback('Required field is missing.', null)
- return
- }
- if (this.dateCreated == null) {
- this.dateCreated = new Date().getTime()
- }
+
+ presave: function(callback) {
+ if (!this.dateCreated)
+ this.dateCreated = new Date().getTime()
- user.prototype.save.super('merlinModel').call(this, callback)
- },
+ user.prototype.presave.super('merlinModel').call(this, callback)
+ }
}
module.exports = user

0 comments on commit 737c5c8

Please sign in to comment.