Skip to content

Commit

Permalink
quirk support v1
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Jul 15, 2015
1 parent 0865848 commit 6529b7f
Show file tree
Hide file tree
Showing 5 changed files with 813 additions and 709 deletions.
15 changes: 12 additions & 3 deletions es5/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ var Model = (function () {
}
});

//add the quirk to this instance
this.additionalAttributes.addAttributes(this);

this.associate();
this.validate();

Expand Down Expand Up @@ -1092,7 +1095,13 @@ var Model = (function () {
var attributeNames = Object.keys(this.attributes);
var fieldAttributes = {};
attributeNames.forEach(function (attributeName) {
fieldAttributes[(0, _jargon2["default"])(attributeName).snake.toString()] = _this10[attributeName];
var found = Object.keys(_this10.additionalAttributes).find(function (additionalAttributeName) {
return additionalAttributeName === attributeName;
});
//is just on db if is not an additional attribute
if (!found) {
fieldAttributes[(0, _jargon2["default"])(attributeName).snake.toString()] = _this10[attributeName];
}
});

//add belongsTo associations and remove others
Expand Down Expand Up @@ -1217,8 +1226,8 @@ Object.defineProperties(Model, {
return modelQuery.find(this);
}
},
//problem here: if it's static on Model, it will be shared for every Model
//if it's static on Item, it will be inaccesible from Model methods to use that on save for eg
//problem here: can't assign property automatically to the concrete model to use it
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe#Browser_compatibility
"attributes": {
value: new _quirk2["default"]()
}
Expand Down
52 changes: 46 additions & 6 deletions es5/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ describe("Model(attributes, options)", function () {

describe("(static properties)", function () {
describe(".attributes", function () {
afterEach(function () {
delete User.attributes.specialAttribute; //so does not affect other tests
user = new User();
});

it("should be an instance of Quirk", function () {
User.attributes.should.be.instanceOf(_quirk2["default"]);
});
Expand All @@ -364,15 +369,50 @@ describe("Model(attributes, options)", function () {

it("should be able to define a new static property to the model", function () {
User.attributes.specialAttribute = 2;
user = new User();
user.specialAttribute.should.equal(2);
});

it("should be able to define a new static property to the model", function () {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({ id: 2 });
user.specialAttribute.should.equal(2);
describe("(read only)", function () {
beforeEach(function () {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({ id: 2 });
});

it("should be able to define a new read only property to the model", function () {
user.specialAttribute.should.equal(2);
});

it("should throw when assign to a new read only property", function () {
(function () {
user.specialAttribute = 3;
}).should["throw"]("Cannot set property");
});
});

describe("(getter and setter)", function () {
beforeEach(function () {
User.attributes.specialAttribute = {
get: function getSpecialAttribute() {
return this.id;
},
set: function setSpecialAttribute(newValue) {
this.id = newValue;
}
};
user = new User({ id: 2 });
});

it("should be able to define a new property with get and set to the model", function () {
user.specialAttribute.should.equal(2);
});

it("should be able to change a property with get and set to the model", function () {
user.specialAttribute = 3;
user.specialAttribute.should.equal(3);
});
});
});

Expand Down
13 changes: 10 additions & 3 deletions es6/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export default class Model {
}
});

//add the quirk to this instance
this.additionalAttributes.addAttributes(this);

this.associate();
this.validate();

Expand Down Expand Up @@ -1024,12 +1027,16 @@ export default class Model {
}

[getFieldAttributes]() {
//this, using quirk, will return target with the new attributes on it, useful to EXCLUDE them from here
//TODO: this.additionalAttributes.addAttributes(target); //magic line
let attributeNames = Object.keys(this.attributes);
let fieldAttributes = {};
attributeNames.forEach((attributeName) => {
fieldAttributes[inflect(attributeName).snake.toString()] = this[attributeName];
let found = Object.keys(this.additionalAttributes).find((additionalAttributeName) => {
return additionalAttributeName === attributeName;
});
//is just on db if is not an additional attribute
if(!found) {
fieldAttributes[inflect(attributeName).snake.toString()] = this[attributeName];
}
});

//add belongsTo associations and remove others
Expand Down
52 changes: 46 additions & 6 deletions es6/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ describe("Model(attributes, options)", () => {

describe("(static properties)", () => {
describe(".attributes", () => {
afterEach(() => {
delete User.attributes.specialAttribute; //so does not affect other tests
user = new User();
});

it("should be an instance of Quirk", () => {
User.attributes.should.be.instanceOf(Quirk);
});
Expand All @@ -260,15 +265,50 @@ describe("Model(attributes, options)", () => {

it("should be able to define a new static property to the model", () => {
User.attributes.specialAttribute = 2;
user = new User();
user.specialAttribute.should.equal(2);
});

it("should be able to define a new static property to the model", () => {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({id: 2});
user.specialAttribute.should.equal(2);
describe("(read only)", () => {
beforeEach(() => {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({id: 2});
});

it("should be able to define a new read only property to the model", () => {
user.specialAttribute.should.equal(2);
});

it("should throw when assign to a new read only property", () => {
() => {
user.specialAttribute = 3;
}.should.throw("Cannot set property");
});
});

describe("(getter and setter)", () => {
beforeEach(() => {
User.attributes.specialAttribute = {
get: function getSpecialAttribute() {
return this.id;
},
set: function setSpecialAttribute(newValue) {
this.id = newValue;
}
};
user = new User({id: 2});
});

it("should be able to define a new property with get and set to the model", () => {
user.specialAttribute.should.equal(2);
});

it("should be able to change a property with get and set to the model", () => {
user.specialAttribute = 3;
user.specialAttribute.should.equal(3);
});
});
});

Expand Down
Loading

0 comments on commit 6529b7f

Please sign in to comment.