Permalink
Browse files

Merge pull request #120 from fb55/master

Added options argument to Resource.timestamps, fixed bug
  • Loading branch information...
2 parents 2f33d2d + fde6666 commit 8c423210015d5f2b739ed00f5a1ece106c1d687c @pksunkara pksunkara committed Sep 17, 2012
Showing with 27 additions and 13 deletions.
  1. +27 −13 lib/resourceful/resource.js
@@ -229,8 +229,10 @@ Resource.new = function (attrs) {
};
Resource.create = function (attrs, callback) {
- if (this._timestamps) {
- attrs.ctime = attrs.mtime = Date.now();
+ if (this._ctime || this._mtime) {
+ var now = Date.now();
+ if(this._ctime) attrs.ctime = now;
+ if(this._mtime) attrs.mtime = now;
}
var that = this,
@@ -278,14 +280,17 @@ Resource.save = function (obj, callback) {
validate = this.prototype.validate(obj, this.schema);
if (!validate.valid) {
- var e = { validate: validate, value: instance, schema: that.schema };
+ var e = { validate: validate, value: obj, schema: this.schema };
return callback && callback(e);
}
- if (this._timestamps) {
- obj.mtime = Date.now();
- if (obj.isNewRecord) {
- obj.ctime = obj.mtime;
+ if (this._ctime || this._mtime) {
+ var now = Date.now();
+ if (this._mtime) {
+ obj.mtime = now;
+ }
+ if (this._ctime && obj.isNewRecord) {
+ obj.ctime = now;
}
}
@@ -330,7 +335,7 @@ Resource.update = function (id, obj, callback) {
id = this.schema.properties[key].sanitize(id);
}
- if (this._timestamps) {
+ if (this._mtime) {
obj.mtime = Date.now();
}
@@ -826,24 +831,33 @@ Resource.property = function (name, typeOrSchema, schema) {
return definer;
};
-Resource.timestamps = function () {
- this._timestamps = true;
+Resource.timestamps = function (options) {
+ this._ctime = !options || !('ctime' in options) || options.ctime;
+ this._mtime = !options || !('mtime' in options) || options.mtime;
+ this._atime = !!options && !!options.atime;
//
// Remark: All timestamps should be considered Unix time format,
// see: http://en.wikipedia.org/wiki/Unix_time
//
//
// The time the resource was created
//
- this.property('ctime', 'number', { format: "unix-time", private: true });
+ if (this._ctime) {
+ this.property('ctime', 'number', { format: "unix-time", private: true });
+ }
//
// The last time the resource was modified
//
- this.property('mtime', 'number', { format: "unix-time", private: true });
+ if (this._mtime) {
+ this.property('mtime', 'number', { format: "unix-time", private: true });
+ }
//
// The last time the resource was accessed
//
- // TODO: this.property('atime', 'number', { format: "unix-time", private: true });
+ if (this._atime) {
+ this.property('atime', 'number', { format: "unix-time", private: true });
+ //TODO actually update atime
+ }
};
Resource.define = function (schema) {

0 comments on commit 8c42321

Please sign in to comment.