Skip to content

Commit

Permalink
inc version and reset associationId on setting the model
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Jul 10, 2015
1 parent 0455f91 commit 22b8230
Show file tree
Hide file tree
Showing 6 changed files with 509 additions and 309 deletions.
66 changes: 59 additions & 7 deletions es5/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,21 +928,32 @@ var Model = (function () {

var privateAssociationName = "_" + associationDetails.name;

var implicitAssociationName = associationDetails.name + "Id";
var privateImplicitAssociationName = "_" + implicitAssociationName;

/* Association Setter By Type */

var setterFunction = undefined;
var setterFunction = undefined,
implicitSetterFunction = undefined;

switch (associationDetails.type) {
case "hasOne":
this[privateAssociationName] = null;

implicitSetterFunction = function (newId) {
//reset the association when assign associationId
_this9[privateImplicitAssociationName] = newId;
_this9[privateAssociationName] = null;
};

setterFunction = function (newModel) {
if (newModel && _this9[privateAssociationName] !== newModel) {
if (!(newModel instanceof Model)) {
throw new Error("Cannot set a non model entity onto this property. It should inherit from Model");
}

_this9[privateAssociationName] = newModel;
_this9[privateImplicitAssociationName] = newModel.id;

newModel[association.foreignName] = _this9;
}
Expand All @@ -951,14 +962,20 @@ var Model = (function () {
case "belongsTo":
this[privateAssociationName] = null;

implicitSetterFunction = function (newId) {
//reset the association when assign associationId
_this9[privateImplicitAssociationName] = newId;
_this9[privateAssociationName] = null;
};

setterFunction = function (newModel) {
if (newModel && _this9[privateAssociationName] !== newModel) {
if (!(newModel instanceof Model)) {
throw new Error("Cannot set a non model entity onto this property. It should inherit from Model");
}

_this9[privateAssociationName] = newModel;
_this9[association.foreignId] = newModel.id;
_this9[privateImplicitAssociationName] = newModel.id;

var pluralForeignName = (0, _jargon2["default"])(association.foreignName).plural.toString();

Expand Down Expand Up @@ -995,18 +1012,53 @@ var Model = (function () {
throw new Error("Unknown association type.");
}

Object.defineProperty(this, privateAssociationName, {
var newProperties = {};
newProperties[privateAssociationName] = {
enumerable: false,
writable: true
});

Object.defineProperty(this, associationDetails.name, {
};
newProperties[associationDetails.name] = {
enumerable: true,
set: setterFunction,
get: function get() {
return _this9[privateAssociationName];
}
});
};
if (implicitSetterFunction) {
newProperties[privateImplicitAssociationName] = {
enumerable: false,
writable: true
};
newProperties[implicitAssociationName] = {
enumerable: true,
set: implicitSetterFunction,
get: function get() {
return _this9[privateImplicitAssociationName];
}
};
}
Object.defineProperties(this, newProperties);

// Object.defineProperty(
// this,
// privateAssociationName,
// {
// enumerable: false,
// writable: true
// }
// );

// Object.defineProperty(
// this,
// associationDetails.name,
// {
// enumerable: true,
// set: setterFunction,
// get: () => {
// return this[privateAssociationName];
// }
// }
// );

this[associationDetails.name] = associationDetails.value;

Expand Down
45 changes: 41 additions & 4 deletions es5/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ describe("Model(attributes, options)", function () {
id: 1,
name: "Bob Builder",
age: 35,
hasChildren: false
hasChildren: false,
addressId: undefined,
primaryPhotoId: undefined,
postalCodeId: undefined
};

user = new User(userAttributes);
Expand Down Expand Up @@ -302,7 +305,7 @@ describe("Model(attributes, options)", function () {

describe(".properties", function () {
it("should return the name of all attributes plus associations on the model", function () {
user.properties.should.eql(["address", "postalCode", "photos", "primaryPhoto", "photoLikes", "likedPhotos", "comments", "deletedComments", "id", "name", "age", "hasChildren"]);
user.properties.should.eql(["address", "addressId", "postalCode", "postalCodeId", "photos", "primaryPhoto", "primaryPhotoId", "photoLikes", "likedPhotos", "comments", "deletedComments", "id", "name", "age", "hasChildren"]);
});
});

Expand Down Expand Up @@ -666,6 +669,20 @@ describe("Model(attributes, options)", function () {
wheel.truckId.should.eql(truck.id);
});

it("should reset the association when a new id is set onto the model", function () {
truck.id = 1;
wheel.truck = truck;
wheel.truckId = 2;
(wheel.truck == null).should.be["true"];
});

it("should reset the associationId when a new model is set onto the model", function () {
truck.id = 1;
wheel.truckId = 2;
wheel.truck = truck;
wheel.truckId.should.not.equal(2);
});

it("should add a model just once on the parent", function () {
truck.id = 1;
wheel.truck = truck;
Expand Down Expand Up @@ -771,6 +788,24 @@ describe("Model(attributes, options)", function () {
truck.hasOne("steeringWheel", SteeringWheel).should.be.instanceOf(_libModelJs.AssociationSetter);
});

it("should reset the association when a new id is set onto the model", function () {
truck.hasOne("steeringWheel", SteeringWheel);
var steeringWheel = new SteeringWheel();
steeringWheel.id = 1;
truck.steeringWheel = steeringWheel;
truck.steeringWheelId = 2;
(truck.steeringWheel == null).should.be["true"];
});

it("should reset the associationId when a new model is set onto the model", function () {
truck.hasOne("steeringWheel", SteeringWheel);
var steeringWheel = new SteeringWheel();
steeringWheel.id = 1;
truck.steeringWheelId = 2;
truck.steeringWheel = steeringWheel;
truck.steeringWheelId.should.not.equal(2);
});

it("should accept a custom error message", function () {
truck.hasOne("steeringWheel", SteeringWheel);
truck.ensure("steeringWheel", _.isPresent, "must be there.");
Expand Down Expand Up @@ -1755,11 +1790,12 @@ describe("Model(attributes, options)", function () {
insertWheels: /insert into `wheels` \(`created_at`, `truck_id`\) values \('19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:00:00\.000', 1\)/,
insertSteeringWheel: /insert into `steering_wheels` \(`created_at`, `truck_id`\) values \('19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:00:00.000', 1\)/,
insertTruck: /insert into `trucks` (`created_at`) values ('19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-24]:00:00.000')/,
updateTruck: /update `trucks` set `updated_at` = '19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:00:00.000' where `id` = 1/,
updateTruck: /update `trucks` set `steering_wheel_id` = 1, `updated_at` = '19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:00:00.000' where `id` = 1/,
updateSteeringWheel: /update `steering_wheels` set `truck_id` = 1, `updated_at` = '19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:00:00.000' where `id` = 1/,
updateWheels: /update `wheels` set `created_at` = '19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:00:00.000', `truck_id` = 1, `updated_at` = '19[0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:00:00.000' where `id` = 1/
};

_libModelJs2["default"].database.mock((_Model$database$mock5 = {}, _defineProperty(_Model$database$mock5, regularExpressions.insertPhotos, [1]), _defineProperty(_Model$database$mock5, regularExpressions.updateUser, []), _defineProperty(_Model$database$mock5, regularExpressions.insertWheels, [1]), _defineProperty(_Model$database$mock5, regularExpressions.insertSteeringWheel, [1]), _defineProperty(_Model$database$mock5, regularExpressions.insertTruck, [1]), _defineProperty(_Model$database$mock5, regularExpressions.updateTruck, [1]), _defineProperty(_Model$database$mock5, regularExpressions.updateWheels, [1]), _Model$database$mock5));
_libModelJs2["default"].database.mock((_Model$database$mock5 = {}, _defineProperty(_Model$database$mock5, regularExpressions.insertPhotos, [1]), _defineProperty(_Model$database$mock5, regularExpressions.updateUser, []), _defineProperty(_Model$database$mock5, regularExpressions.insertWheels, [1]), _defineProperty(_Model$database$mock5, regularExpressions.insertSteeringWheel, [1]), _defineProperty(_Model$database$mock5, regularExpressions.insertTruck, [1]), _defineProperty(_Model$database$mock5, regularExpressions.updateTruck, [1]), _defineProperty(_Model$database$mock5, regularExpressions.updateSteeringWheel, [1]), _defineProperty(_Model$database$mock5, regularExpressions.updateWheels, [1]), _Model$database$mock5));
});

describe("(association operations)", function () {
Expand Down Expand Up @@ -1965,6 +2001,7 @@ describe("Model(attributes, options)", function () {
truck = new Truck({ id: 1 });
wheel = new Wheel();
steeringWheel = new SteeringWheel();
steeringWheel.id = 1;

truck.steeringWheel = steeringWheel;

Expand Down
78 changes: 61 additions & 17 deletions es6/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,21 +870,32 @@ export default class Model {

const privateAssociationName = "_" + associationDetails.name;

const implicitAssociationName = `${associationDetails.name}Id`;
const privateImplicitAssociationName = `_${implicitAssociationName}`;

/* Association Setter By Type */

let setterFunction;
let setterFunction,
implicitSetterFunction;

switch(associationDetails.type) {
case "hasOne":
this[privateAssociationName] = null;

implicitSetterFunction = (newId) => {
//reset the association when assign associationId
this[privateImplicitAssociationName] = newId;
this[privateAssociationName] = null;
}

setterFunction = (newModel) => {
if (newModel && this[privateAssociationName] !== newModel) {
if(!(newModel instanceof Model)) {
throw new Error("Cannot set a non model entity onto this property. It should inherit from Model");
}

this[privateAssociationName] = newModel;
this[privateImplicitAssociationName] = newModel.id;

newModel[association.foreignName] = this;
}
Expand All @@ -893,14 +904,20 @@ export default class Model {
case "belongsTo":
this[privateAssociationName] = null;

implicitSetterFunction = (newId) => {
//reset the association when assign associationId
this[privateImplicitAssociationName] = newId;
this[privateAssociationName] = null;
}

setterFunction = (newModel) => {
if (newModel && this[privateAssociationName] !== newModel) {
if(!(newModel instanceof Model)) {
throw new Error("Cannot set a non model entity onto this property. It should inherit from Model");
}

this[privateAssociationName] = newModel;
this[association.foreignId] = newModel.id;
this[privateImplicitAssociationName] = newModel.id;

const pluralForeignName = inflect(association.foreignName).plural.toString();

Expand Down Expand Up @@ -937,26 +954,53 @@ export default class Model {
throw new Error("Unknown association type.");
}

Object.defineProperty(
this,
privateAssociationName,
{
let newProperties = {};
newProperties[privateAssociationName] = {
enumerable: false,
writable: true
};
newProperties[associationDetails.name] = {
enumerable: true,
set: setterFunction,
get: () => {
return this[privateAssociationName];
}
}
if(implicitSetterFunction) {
newProperties[privateImplicitAssociationName] = {
enumerable: false,
writable: true
}
);

Object.defineProperty(
this,
associationDetails.name,
{
};
newProperties[implicitAssociationName] = {
enumerable: true,
set: setterFunction,
set: implicitSetterFunction,
get: () => {
return this[privateAssociationName];
return this[privateImplicitAssociationName];
}
}
);
};
}
Object.defineProperties(this, newProperties);

// Object.defineProperty(
// this,
// privateAssociationName,
// {
// enumerable: false,
// writable: true
// }
// );

// Object.defineProperty(
// this,
// associationDetails.name,
// {
// enumerable: true,
// set: setterFunction,
// get: () => {
// return this[privateAssociationName];
// }
// }
// );

this[associationDetails.name] = associationDetails.value;

Expand Down
Loading

0 comments on commit 22b8230

Please sign in to comment.