Skip to content

Commit

Permalink
Merge 30ef97f into c58eb02
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Jul 15, 2015
2 parents c58eb02 + 30ef97f commit 313d95a
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 318 deletions.
29 changes: 19 additions & 10 deletions es5/lib/modelFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var _collectionJs = require("./collection.js");

var _collectionJs2 = _interopRequireDefault(_collectionJs);

var validateDependencies = Symbol();
var validateDependencies = Symbol(),
transformNames = Symbol();

var ModelFinder = (function () {
function ModelFinder(database) {
Expand Down Expand Up @@ -128,22 +129,26 @@ var ModelQuery = (function () {
return this;
}
}, {
key: "where",
value: function where() {
var _query;

key: transformNames,
value: function value() {
for (var _len = arguments.length, options = Array(_len), _key = 0; _key < _len; _key++) {
options[_key] = arguments[_key];
}

var formattedOptions = options.map(function (option, index) {
return options.map(function (option, index) {
if (typeof option === "string" && index === 0) {
return (0, _jargon2["default"])(option).snake.toString();
} else {
return option;
}
});
}
}, {
key: "where",
value: function where() {
var _query;

var formattedOptions = this[transformNames].apply(this, arguments);
(_query = this._query).where.apply(_query, _toConsumableArray(formattedOptions));
return this;
}
Expand All @@ -152,31 +157,35 @@ var ModelQuery = (function () {
value: function andWhere() {
var _query2;

(_query2 = this._query).andWhere.apply(_query2, arguments);
var formattedOptions = this[transformNames].apply(this, arguments);
(_query2 = this._query).andWhere.apply(_query2, _toConsumableArray(formattedOptions));
return this;
}
}, {
key: "orWhere",
value: function orWhere() {
var _query3;

(_query3 = this._query).orWhere.apply(_query3, arguments);
var formattedOptions = this[transformNames].apply(this, arguments);
(_query3 = this._query).orWhere.apply(_query3, _toConsumableArray(formattedOptions));
return this;
}
}, {
key: "groupBy",
value: function groupBy() {
var _query4;

(_query4 = this._query).groupBy.apply(_query4, arguments);
var formattedOptions = this[transformNames].apply(this, arguments);
(_query4 = this._query).groupBy.apply(_query4, _toConsumableArray(formattedOptions));
return this;
}
}, {
key: "orderBy",
value: function orderBy() {
var _query5;

(_query5 = this._query).orderBy.apply(_query5, arguments);
var formattedOptions = this[transformNames].apply(this, arguments);
(_query5 = this._query).orderBy.apply(_query5, _toConsumableArray(formattedOptions));
return this;
}
}, {
Expand Down
77 changes: 77 additions & 0 deletions es5/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,83 @@ describe("Model(attributes, options)", function () {
User.find.should.be.instanceOf(_libModelFinderJs.ModelQuery);
});

describe("(ModelQuery operations)", function () {
describe(".where", function () {
var querySpy = undefined;

beforeEach(function (done) {
querySpy = _libModelJs2["default"].database.spy("select * from `users` where `some_id` = 1", [1]);
User.find.where("someId", 1).results(function () {
done();
});
});

it("should convert camel case names to snake case names on a where", function () {
querySpy.callCount.should.equal(1);
});
});

describe(".andWhere", function () {
var querySpy = undefined;

beforeEach(function (done) {
querySpy = _libModelJs2["default"].database.spy("select * from `users` where `some_id` = 1", [1]);
User.find.andWhere("someId", 1).results(function () {
done();
});
});

it("should convert camel case names to snake case names on a where", function () {
querySpy.callCount.should.equal(1);
});
});

describe(".orWhere", function () {
var querySpy = undefined;

beforeEach(function (done) {
querySpy = _libModelJs2["default"].database.spy("select * from `users` where `some_id` = 1", [1]);
User.find.orWhere("someId", 1).results(function () {
done();
});
});

it("should convert camel case names to snake case names on a where", function () {
querySpy.callCount.should.equal(1);
});
});

describe(".groupBy", function () {
var querySpy = undefined;

beforeEach(function (done) {
querySpy = _libModelJs2["default"].database.spy("select * from `users` group by `some_id`", [1]);
User.find.groupBy("someId").results(function () {
done();
});
});

it("should convert camel case names to snake case names on a where", function () {
querySpy.callCount.should.equal(1);
});
});

describe(".orderBy", function () {
var querySpy = undefined;

beforeEach(function (done) {
querySpy = _libModelJs2["default"].database.spy("select * from `users` order by `some_id` asc", [1]);
User.find.orderBy("someId").results(function () {
done();
});
});

it("should convert camel case names to snake case names on a where", function () {
querySpy.callCount.should.equal(1);
});
});
});

it("should return a collection", function () {
users.should.be.instanceOf(_libCollectionJs2["default"]);
});
Expand Down
22 changes: 15 additions & 7 deletions es6/lib/modelFinder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const validateDependencies = Symbol();
const validateDependencies = Symbol(),
transformNames = Symbol();
import inflect from "jargon";

export default class ModelFinder {
Expand Down Expand Up @@ -92,36 +93,43 @@ export class ModelQuery {
return this;
}

where(...options) {
const formattedOptions = options.map((option, index) => {
[transformNames](...options) {
return options.map((option, index) => {
if (typeof option === "string" && index === 0) {
return inflect(option).snake.toString();
} else {
return option;
}
});
}

where(...options) {
const formattedOptions = this[transformNames](...options);
this._query.where(...formattedOptions);
return this;
}

andWhere(...options) {
this._query.andWhere(...options);
const formattedOptions = this[transformNames](...options);
this._query.andWhere(...formattedOptions);
return this;
}

orWhere(...options) {
this._query.orWhere(...options);
const formattedOptions = this[transformNames](...options);
this._query.orWhere(...formattedOptions);
return this;
}

groupBy(...options) {
this._query.groupBy(...options);
const formattedOptions = this[transformNames](...options);
this._query.groupBy(...formattedOptions);
return this;
}

orderBy(...options) {
this._query.orderBy(...options);
const formattedOptions = this[transformNames](...options);
this._query.orderBy(...formattedOptions);
return this;
}

Expand Down
92 changes: 92 additions & 0 deletions es6/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,98 @@ describe("Model(attributes, options)", () => {
User.find.should.be.instanceOf(ModelQuery);
});

describe("(ModelQuery operations)", () => {
describe(".where", () => {
let querySpy;

beforeEach(done => {
querySpy = Model.database.spy("select * from `users` where `some_id` = 1", [1]);
User
.find
.where("someId", 1)
.results(() => {
done();
});
});

it("should convert camel case names to snake case names on a where", () => {
querySpy.callCount.should.equal(1);
});
});

describe(".andWhere", () => {
let querySpy;

beforeEach(done => {
querySpy = Model.database.spy("select * from `users` where `some_id` = 1", [1]);
User
.find
.andWhere("someId", 1)
.results(() => {
done();
});
});

it("should convert camel case names to snake case names on a where", () => {
querySpy.callCount.should.equal(1);
});
});

describe(".orWhere", () => {
let querySpy;

beforeEach(done => {
querySpy = Model.database.spy("select * from `users` where `some_id` = 1", [1]);
User
.find
.orWhere("someId", 1)
.results(() => {
done();
});
});

it("should convert camel case names to snake case names on a where", () => {
querySpy.callCount.should.equal(1);
});
});

describe(".groupBy", () => {
let querySpy;

beforeEach(done => {
querySpy = Model.database.spy("select * from `users` group by `some_id`", [1]);
User
.find
.groupBy("someId")
.results(() => {
done();
});
});

it("should convert camel case names to snake case names on a where", () => {
querySpy.callCount.should.equal(1);
});
});

describe(".orderBy", () => {
let querySpy;

beforeEach(done => {
querySpy = Model.database.spy("select * from `users` order by `some_id` asc", [1]);
User
.find
.orderBy("someId")
.results(() => {
done();
});
});

it("should convert camel case names to snake case names on a where", () => {
querySpy.callCount.should.equal(1);
});
});
});

it("should return a collection", () => {
users.should.be.instanceOf(Collection);
});
Expand Down
Loading

0 comments on commit 313d95a

Please sign in to comment.