Skip to content

Commit

Permalink
Inherit hooks from ancestors
Browse files Browse the repository at this point in the history
closes #2

LGMTs Jake
  • Loading branch information
jonstorer committed Dec 3, 2014
1 parent 380c79f commit 99fc98b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/seed-forge/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,19 @@ Factory.prototype.attrs = function() {
*/

Factory.prototype.extend = function(name) {
this.parent = factories.get(name);
return this;
var self = this;

self.parent = factories.get(name);

self.ancestors().forEach(function(ancestor){
for(key in ancestor.events){
(ancestor.events[key] || []).forEach(function(fn){
self.hook(key, fn);
});
}
});

return self;
};

/**
Expand Down
33 changes: 33 additions & 0 deletions test/seed-forge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,39 @@ describe('Seed Forge', function() {
});
});

it('inherits hooks', function(done) {
var parentHook = false
, childHook = false;

define('Parent', User).extend('User')
.hook('pre:build', function(next) {
setImmediate(function() {
expect(childHook, 'child hook should not have been called at this point').to.be.false;
next.should.be.a('function');

parentHook = true;
next();
});
});

define('Child', User).extend('Parent')
.hook('pre:build', function(next) {
setImmediate(function() {
expect(parentHook, 'parent hook should have been called at this point').to.be.true;
next.should.be.a('function');

childHook = true;
next();
});
});

factory('Child', function() {
expect(parentHook, 'parent hook should have been called').to.be.true;
expect(childHook, 'child hook should have been called').to.be.true;
done();
});
});

it('escalates hook errors', function(done) {
define('Err', User)
.extend('User')
Expand Down

0 comments on commit 99fc98b

Please sign in to comment.