Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

take fetch and save off the model and present the folder class approa… #5

Merged
merged 10 commits into from
Aug 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .bithoundrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ignore": [
"es5/**/*.js"
]
}
4 changes: 4 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
languages:
JavaScript: true
exclude_paths:
- es5/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Seat extends Model {

# Quality and Compatibility

[![Build Status](https://travis-ci.org/FreeAllMedia/dovima.png?branch=master)](https://travis-ci.org/FreeAllMedia/dovima) [![Coverage Status](https://coveralls.io/repos/FreeAllMedia/dovima/badge.svg)](https://coveralls.io/r/FreeAllMedia/dovima) [![Code Climate](https://codeclimate.com/github/FreeAllMedia/dovima/badges/gpa.svg)](https://codeclimate.com/github/FreeAllMedia/dovima) [![Dependency Status](https://david-dm.org/FreeAllMedia/dovima.png?theme=shields.io)](https://david-dm.org/FreeAllMedia/dovima?theme=shields.io) [![Dev Dependency Status](https://david-dm.org/FreeAllMedia/dovima/dev-status.svg)](https://david-dm.org/FreeAllMedia/dovima?theme=shields.io#info=devDependencies)
[![Build Status](https://travis-ci.org/FreeAllMedia/dovima.png?branch=master)](https://travis-ci.org/FreeAllMedia/dovima) [![Coverage Status](https://coveralls.io/repos/FreeAllMedia/dovima/badge.svg)](https://coveralls.io/r/FreeAllMedia/dovima) [![Code Climate](https://codeclimate.com/github/FreeAllMedia/dovima/badges/gpa.svg)](https://codeclimate.com/github/FreeAllMedia/dovima) [![bitHound Score](https://www.bithound.io/github/FreeAllMedia/dovima/badges/score.svg)](https://www.bithound.io/github/FreeAllMedia/dovima) [![Dependency Status](https://david-dm.org/FreeAllMedia/dovima.png?theme=shields.io)](https://david-dm.org/FreeAllMedia/dovima?theme=shields.io) [![Dev Dependency Status](https://david-dm.org/FreeAllMedia/dovima/dev-status.svg)](https://david-dm.org/FreeAllMedia/dovima?theme=shields.io#info=devDependencies)

*Every build and release is automatically tested on the following platforms:*

Expand Down
95 changes: 95 additions & 0 deletions es5/lib/associationSetter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var ambiguous = Symbol(),
dependent = Symbol();

var AssociationSetter = (function () {
function AssociationSetter(association) {
_classCallCheck(this, AssociationSetter);

this.association = association;

switch (association.type) {
case "belongsTo":
Object.defineProperties(this, {
"ambiguous": {
get: this[ambiguous]
}
});
break;
case "hasOne":
case "hasMany":
Object.defineProperties(this, {
"dependent": {
get: this[dependent]
}
});
break;
}
}

_createClass(AssociationSetter, [{
key: "foreignName",
value: function foreignName(name) {
this.association.foreignName = name;
return this;
}
}, {
key: "where",
value: function where() {
for (var _len = arguments.length, options = Array(_len), _key = 0; _key < _len; _key++) {
options[_key] = arguments[_key];
}

this.association.where = options;
return this;
}
}, {
key: "andWhere",
value: function andWhere() {
this.association.andWhere = this.association.andWhere || [];

for (var _len2 = arguments.length, options = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
options[_key2] = arguments[_key2];
}

this.association.andWhere.push(options);
return this;
}
}, {
key: "through",
value: function through(associationName) {
this.association.through = associationName;
return this;
}
}, {
key: "as",
value: function as(associationName) {
this.association.as = associationName;
return this;
}
}, {
key: ambiguous,
value: function value() {
this.association.ambiguous = true;
}
}, {
key: dependent,
value: function value() {
this.association.dependent = true;
}
}]);

return AssociationSetter;
})();

exports["default"] = AssociationSetter;
module.exports = exports["default"];
74 changes: 34 additions & 40 deletions es5/lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ var _modelFinderJs = require("./modelFinder.js");

var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs);

var _modelJs = require("./model.js");

var _modelJs2 = _interopRequireDefault(_modelJs);

var Collection = (function (_Array) {
function Collection(initialData) {
_classCallCheck(this, Collection);
Expand Down Expand Up @@ -90,7 +86,7 @@ var Collection = (function (_Array) {
modelName = model;
}

throw TypeError("The model " + modelName + " is not an instance of " + _this._modelConstructor.name + ", therefore, it cannot be pushed to this collection.");
throw new TypeError("The model " + modelName + " is not an instance of " + _this._modelConstructor.name + ", therefore, it cannot be pushed to this collection.");
}
}
}
Expand All @@ -101,48 +97,46 @@ var Collection = (function (_Array) {
value: function fetch(callback) {
var _this2 = this;

if (this.association) {
(function () {
var processWhereCondition = function processWhereCondition(value) {
if (typeof value === "string") {
var snakeCasedValue = (0, _jargon2["default"])(value).snake.toString();
return snakeCasedValue;
} else {
return value;
}
};

var modelFinder = new _modelFinderJs2["default"](_this2.association.constructor.database);
function processWhereCondition(value) {
if (typeof value === "string") {
var snakeCasedValue = (0, _jargon2["default"])(value).snake.toString();
return snakeCasedValue;
} else {
return value;
}
}

var query = modelFinder.find(_this2.association.constructor).where(_this2.association.foreignKey, "=", _this2.association.parent.id);
if (this.association) {
var modelFinder = new _modelFinderJs2["default"](this.association.constructor.database);

if (_this2.association.where) {
(function () {
var processedWhereConditions = _this2.association.where.map(processWhereCondition);
var self = _this2;
query.andWhere(function () {
var _this3 = this;
var query = modelFinder.find(this.association.constructor).where(this.association.foreignKey, "=", this.association.parent.id);

this.where.apply(this, _toConsumableArray(processedWhereConditions));
if (this.association.where) {
(function () {
var processedWhereConditions = _this2.association.where.map(processWhereCondition);
var self = _this2;
query.andWhere(function () {
var _this3 = this;

if (Array.isArray(self.association.andWhere)) {
self.association.andWhere.forEach(function (whereConditions) {
var processedAndWhereItem = whereConditions.map(processWhereCondition);
_this3.andWhere.apply(_this3, _toConsumableArray(processedAndWhereItem));
});
}
});
})();
}
this.where.apply(this, _toConsumableArray(processedWhereConditions));

query.results(function (error, models) {
_this2.splice(0, _this2.length);
models.forEach(function (model) {
_this2.push(model);
if (Array.isArray(self.association.andWhere)) {
self.association.andWhere.forEach(function (whereConditions) {
var processedAndWhereItem = whereConditions.map(processWhereCondition);
_this3.andWhere.apply(_this3, _toConsumableArray(processedAndWhereItem));
});
}
});
callback(error);
})();
}

query.results(function (error, models) {
_this2.splice(0, _this2.length);
models.forEach(function (model) {
_this2.push(model);
});
})();
callback(error);
});
} else {
throw new Error("Cannot fetch collection without an association set. Call Model.all instead.");
}
Expand Down
Loading