Skip to content

Commit

Permalink
Validators are now working but I'm having a pain with ObjectIds
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Bergeron committed Apr 17, 2012
1 parent 5ad614b commit 6bc1a53
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
2 changes: 0 additions & 2 deletions modules/app/controllers/home.js
@@ -1,7 +1,5 @@
module.exports = {
index: function(req, res) {
this.helpers.flash(req, 'error', 'Testing messages');

res.render('index', {
title: 'ThinAir'
});
Expand Down
28 changes: 17 additions & 11 deletions modules/app/repositories/Projects.js
Expand Up @@ -29,41 +29,47 @@ var Projects = createRepository('projects', {

// saves a project
save: function(project, callback) {
var project_code = this.helpers.slugify(project.name);
var project_code = this.helpers.slugify(project.name),
that = this;

//todo: CHECK FOR DUPLICATES
this.validator.validate('project', project, function(errors) {
console.log(errors);
if (!errors) {
if (this.helpers.isNew(project)) {
if (that.helpers.isNew(project)) {
//if it's a new project, save it
this.save({ code: project_code, name: project.name });
that.save({ code: project_code, name: project.name });
//fetch the saved project
this.findOne({ code: project_code }, function(err, new_project) {
that.findOne({ code: project_code }, function(err, new_project) {
return callback(new_project);
});
} else {
//if it's not a new project, update the existing one
this.update({ _id: db.ObjectId(project._id) }, {
that.update({ _id: project._id }, {
$set: { code: project_code, name: project.name }
});
//fetch the created project
this.findOne({ _id: db.ObjectId(project._id) }, function(err, updated_project) {
that.findOne({ _id: project._id }, function(err, updated_project) {
return callback(updated_project);
});
}
} else {
// todo: remove this when validations are done
var errors = null;
//if there's validation errors
if (this.helpers.isNew(project)) {
if (that.helpers.isNew(project)) {
//if it's a new project, send it as it is
return callback(project, errors);
} else {
//if it's an existing one, fetch it and put and replace the values with the on from the form
this.findOne({ _id: db.ObjectId(project._id) }, function(err, fetched_project) {
fetched_project.code = project_code;
fetched_project.name = project.name;
return callback(fetched_project, errors);
that.findOne({ _id: project._id }, function(err, fetched_project) {
if (fetched_project) {
fetched_project.code = project_code;
fetched_project.name = project.name;
return callback(fetched_project, errors);
} else {
return callback(null, errors);
}
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions modules/dependency.json
Expand Up @@ -11,6 +11,9 @@
"./libs/configure.js": {
"helpers": "./libs/helpers.js"
},
"./libs/validator.js": {
"helpers": "./libs/helpers.js"
},
"./app/routes.js": {
"router": "./libs/router.js",
"controllers": ["./app/controllers/"]
Expand Down
4 changes: 4 additions & 0 deletions modules/libs/helpers.js
Expand Up @@ -58,6 +58,10 @@ var Helpers = {
}
},

isNew: function(object) {
return (object._id.length > 0) ? false : true;
},

// todo: it would be cool to eventually do something with this
registerHandlebarsHelpers: function(hbs) {
// hbs.registerHelper('fullName', function(person) {
Expand Down

0 comments on commit 6bc1a53

Please sign in to comment.