Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrockwell committed Sep 23, 2015
2 parents f28a692 + 682a184 commit 4e15d6a
Show file tree
Hide file tree
Showing 22 changed files with 840 additions and 680 deletions.
535 changes: 401 additions & 134 deletions README.md

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions es5/lib/model/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ var _jargon2 = _interopRequireDefault(_jargon);
function deleteSelf(callback) {
var _this = this;

if (!this[_symbols2["default"].getDatabase]()) {
throw new Error("Cannot delete without Model.database set.");
}

_flowsync2["default"].series([function (done) {
_this.beforeDelete(done);
}, function (done) {
Expand All @@ -55,10 +51,24 @@ function deleteSelf(callback) {
}

function performDelete(callback) {
if ((0, _incognito2["default"])(this)._softDelete) {
softDelete.call(this, callback);
var _ = (0, _incognito2["default"])(this);
if (_.mockDelete) {
if (_._softDelete) {
this.deletedAt = new _fleming2["default"]();
callback();
} else {
callback();
}
} else {
hardDelete.call(this, callback);
if (this[_symbols2["default"].getDatabase]()) {
if (_._softDelete) {
softDelete.call(this, callback);
} else {
hardDelete.call(this, callback);
}
} else {
callback(new Error("Cannot delete without Model.database set."));
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions es5/lib/model/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ function fetchByBelongsTo(associationName, associations, callback) {
}

function fetchBy() {
var fields = arguments.length <= 0 || arguments[0] === undefined ? [this.primaryKey] : arguments[0];
var callback = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];

var _ = (0, _incognito2["default"])(this);
if (_.mockFetchRecord) {
for (var attributeName in _.mockFetchRecord) {
var mockValue = _.mockFetchRecord[attributeName];
this[attributeName] = mockValue;
}
callback();
} else {
fetchFromDatabase.call(this, fields, callback);
}
}

function fetchFromDatabase() {
var _this5 = this;

var fields = arguments.length <= 0 || arguments[0] === undefined ? [this.primaryKey] : arguments[0];
Expand Down
53 changes: 53 additions & 0 deletions es5/lib/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ var Model = (function () {

return fieldAttributes;
}
}, {
key: "mock",

/**
* INSTANCE MOCKING
*/

get: function get() {
return new InstanceMock(this);
}
}], [{
key: "database",
get: function get() {
Expand All @@ -514,13 +524,56 @@ var Model = (function () {
var modelQuery = new _modelFinderJs2["default"](this.database);
return modelQuery.count(this);
}
}, {
key: "mock",
get: function get() {
var modelQuery = new _modelFinderJs2["default"](this.database);
return modelQuery.mock(this);
}
}]);

return Model;
})();

exports["default"] = Model;

var InstanceMock = (function () {
function InstanceMock(instance) {
_classCallCheck(this, InstanceMock);

(0, _incognito2["default"])(this).instance = instance;
}

_createClass(InstanceMock, [{
key: "save",
value: function save(mockNewId) {
var instance = (0, _incognito2["default"])(this).instance;
(0, _incognito2["default"])(instance).mockNewId = mockNewId;
}
}, {
key: "fetch",
value: function fetch(mockRecord) {
var instance = (0, _incognito2["default"])(this).instance;
(0, _incognito2["default"])(instance).mockFetchRecord = mockRecord;
}
}, {
key: "delete",
value: function _delete() {
var instance = (0, _incognito2["default"])(this).instance;
(0, _incognito2["default"])(instance).mockDelete = true;
}
}, {
key: "record",
value: function record(mockRecord) {
this.save(mockRecord.id);
this.fetch(mockRecord);
this["delete"]();
}
}]);

return InstanceMock;
})();

Object.assign(Model.prototype, _defineProperty({
"fetch": require("./fetch.js"),
"save": require("./save.js"),
Expand Down
56 changes: 32 additions & 24 deletions es5/lib/model/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,42 @@ function saveOrUpdate(callback) {
var _this = this;

var now = new _fleming2["default"]();
if (this.isNew) {
this.createdAt = now.toDate();
var fieldAttributes = this[_symbols2["default"].getFieldAttributes]();

this[_symbols2["default"].getDatabase]().insert(fieldAttributes).into(this.tableName).results(function (error, ids) {
if (error) {
callback(error);
} else {
_this[_this.primaryKey] = ids[0];
callback();
}
});
var _ = (0, _incognito2["default"])(this);

if (_.mockNewId) {
this[this.primaryKey] = _.mockNewId;
callback(undefined, _.mockNewId);
} else {
this.updatedAt = now.toDate();
var attributes = this[_symbols2["default"].getFieldAttributes]();
var updateAttributes = {};
if (!this[_symbols2["default"].getDatabase]()) {
callback(new Error("Cannot save without Model.database set."));
} else {
if (this.isNew) {
this.createdAt = now.toDate();
var fieldAttributes = this[_symbols2["default"].getFieldAttributes]();

this[_symbols2["default"].getDatabase]().insert(fieldAttributes).into(this.tableName).results(function (error, ids) {
if (error) {
callback(error);
} else {
_this[_this.primaryKey] = ids[0];
callback();
}
});
} else {
this.updatedAt = now.toDate();
var attributes = this[_symbols2["default"].getFieldAttributes]();
var updateAttributes = {};

for (var attributeName in attributes) {
if (attributeName !== this.primaryKey) {
updateAttributes[attributeName] = attributes[attributeName];
for (var attributeName in attributes) {
if (attributeName !== this.primaryKey) {
updateAttributes[attributeName] = attributes[attributeName];
}
}

this[_symbols2["default"].getDatabase]().update(updateAttributes).into(this.tableName).where(this.primaryKey, "=", this[this.primaryKey]).results(callback);
}
}

this[_symbols2["default"].getDatabase]().update(updateAttributes).into(this.tableName).where(this.primaryKey, "=", this[this.primaryKey]).results(callback);
}
}

Expand Down Expand Up @@ -114,10 +126,6 @@ function validate(callback) {
function save(callback) {
var _this3 = this;

if (!this[_symbols2["default"].getDatabase]()) {
throw new Error("Cannot save without Model.database set.");
}

_flowsync2["default"].series([function (next) {
_this3.beforeValidation(next);
}, function (next) {
Expand All @@ -134,7 +142,7 @@ function save(callback) {
if (errors) {
callback(errors);
} else {
callback(undefined, _this3);
callback(undefined, _this3[_this3.primaryKey]);
}
});
}
Expand Down
Loading

0 comments on commit 4e15d6a

Please sign in to comment.