Skip to content

Commit

Permalink
Added .options() method
Browse files Browse the repository at this point in the history
  • Loading branch information
saschagehlich committed Jan 6, 2014
1 parent ea03cf6 commit c479299
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ function Product(s) {

// Indices
this.index(["name", "isPublished"], { indexName: "NameIsPublished" });

// Model options
this.options({
timestamps: false
});
}

// This will be called before a Product is created
Expand Down
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Sequenice.prototype._loadModel = function(modelPath) {
var indices = [];
var instanceMethods = {};
var classMethods = {};
var options = {};
var model, modelName, modelOptions;

// Avoid that our helpers will be added as
Expand All @@ -124,6 +125,7 @@ Sequenice.prototype._loadModel = function(modelPath) {
this._attachAssociationHelpersToModel(Model);
this._attachHookHelpersToModel(Model, hooks);
this._attachIndexHelperToModel(Model, indices);
this._attachOptionsHelperToModel(Model, options);
this._extractMethodsFromModel(Model, instanceMethods, classMethods);

// Call the model constructor so that our
Expand All @@ -132,14 +134,14 @@ Sequenice.prototype._loadModel = function(modelPath) {

// Define the sequelize model
modelName = model.constructor.name;
modelOptions = {
modelOptions = _.extend({
instanceMethods: instanceMethods,
classMethods: classMethods,
validate: validators,
getterMethods: getters,
setterMethods: setters,
hooks: hooks
};
}, options);
model._model = this.sequelize.define(modelName, fields, modelOptions);

// Override the sync method so that it automatically
Expand Down Expand Up @@ -272,6 +274,19 @@ Sequenice.prototype._attachIndexHelperToModel = function(modelClass, target) {
};
};

/**
* Adds a `options` prototype method to modelClass
* which will add options to the target
* @param {Class} modelClass
* @param {Object} target
* @private
*/
Sequenice.prototype._attachOptionsHelperToModel = function(modelClass, target) {
modelClass.prototype.options = function (options) {
_.extend(target, options);
};
};

/**
* Extracts instance methods and class methods from the given
* model class, excluding all methods in `modelClass._methodBlacklist`
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ describe("sequenice example", function () {
done();
});

/**
* Options
*/
it("should apply the options", function (done) {
sequelize.queryInterface.describeTable("Projects").success(function (columns) {
should.not.exist(columns.createdAt);
should.not.exist(columns.updatedAt);

done();
});
});

/**
* Indices
*/
Expand Down
8 changes: 8 additions & 0 deletions test/models/project.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
function Project(s) {
/**
* Field declarations
Expand All @@ -8,6 +9,13 @@ function Project(s) {
* Associations
*/
this.belongsTo("User");

/**
* Options
*/
this.options({
timestamps: false
});
}

module.exports = Project;

0 comments on commit c479299

Please sign in to comment.