Skip to content

Commit

Permalink
Adds beforeDefine to plugins (#263)
Browse files Browse the repository at this point in the history
Prototype: beforeDefine(name, properties, options)
  • Loading branch information
dresende committed Aug 1, 2013
1 parent 394944f commit 1a4067d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/ORM.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ ORM.prototype.define = function (name, properties, opts) {
properties = properties || {};
opts = opts || {};

for (var i = 0; i < this.plugins.length; i++) {
if (typeof this.plugins[i].beforeDefine == "function") {
this.plugins[i].beforeDefine(name, properties, opts);
}
}

this.models[name] = new Model({
db : this,
settings : this.settings,
Expand Down
22 changes: 22 additions & 0 deletions test/integration/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ describe("db.use()", function () {
return done();
});

it("a plugin should be able to catch models before defining them", function (done) {
var MyPlugin = require("../support/my_plugin");
var opts = {
option : true,
calledDefine : false,
beforeDefine : function (name, props, opts) {
props.otherprop = Number;
}
};

db.use(MyPlugin, opts);

var MyModel = db.define("my_model", { // db.define should call plugin.define method
property: String
});

opts.calledDefine.should.be.true;
MyModel.properties.should.have.property("otherprop");

return done();
});

it("should be able to register a plugin as string", function (done) {
var opts = {
option : true,
Expand Down
8 changes: 7 additions & 1 deletion test/support/my_plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = function MyPlugin(DB, opts) {
opts.should.eql({ option: true, calledDefine: false });
opts.option.should.be.true;
opts.calledDefine.should.be.false;

return {
define: function (Model) {
Expand All @@ -8,6 +9,11 @@ module.exports = function MyPlugin(DB, opts) {
Model.id[0].should.be.a("string");

opts.calledDefine = true;
},
beforeDefine: function (model_name, model_props, model_opts) {
if (opts.beforeDefine) {
opts.beforeDefine(model_name, model_props, model_opts);
}
}
};
};

0 comments on commit 1a4067d

Please sign in to comment.