Pass proxy Ghost interface to Apps

Closes #1478

- Create new proxy.js that exposes createProxy method
- Pass proxy to App activate/install in lieu of Ghost instance
This commit is contained in:
Jacob Gable 2013-11-14 20:17:33 -06:00
parent 9d55e68689
commit ef9f5dc33f
6 changed files with 120 additions and 122 deletions

View File

@ -1,54 +0,0 @@
var GhostPlugin;
/**
* GhostPlugin is the base class for a standard plugin.
* @class
* @parameter {Ghost} The current Ghost app instance
*/
GhostPlugin = function (ghost) {
this.app = ghost;
};
/**
* A method that will be called on installation.
* Can optionally return a promise if async.
* @parameter {Ghost} The current Ghost app instance
*/
GhostPlugin.prototype.install = function (ghost) {
/*jslint unparam:true*/
return;
};
/**
* A method that will be called on uninstallation.
* Can optionally return a promise if async.
* @parameter {Ghost} The current Ghost app instance
*/
GhostPlugin.prototype.uninstall = function (ghost) {
/*jslint unparam:true*/
return;
};
/**
* A method that will be called when the plugin is enabled.
* Can optionally return a promise if async.
* @parameter {Ghost} The current Ghost app instance
*/
GhostPlugin.prototype.activate = function (ghost) {
/*jslint unparam:true*/
return;
};
/**
* A method that will be called when the plugin is disabled.
* Can optionally return a promise if async.
* @parameter {Ghost} The current Ghost app instance
*/
GhostPlugin.prototype.deactivate = function (ghost) {
/*jslint unparam:true*/
return;
};
module.exports = GhostPlugin;

View File

@ -3,8 +3,7 @@ var _ = require('underscore'),
when = require('when'),
errors = require('../errorHandling'),
ghostApi,
loader = require('./loader'),
GhostPlugin = require('./GhostPlugin');
loader = require('./loader');
function getInstalledPlugins() {
if (!ghostApi) {
@ -33,8 +32,6 @@ function saveInstalledPlugins(installedPlugins) {
}
module.exports = {
GhostPlugin: GhostPlugin,
init: function (ghost) {
var pluginsToLoad;

View File

@ -2,6 +2,7 @@
var path = require('path'),
_ = require('underscore'),
when = require('when'),
createProxy = require('./proxy'),
ghostInstance,
loader;
@ -36,7 +37,7 @@ function getPluginByName(name, ghost) {
// Check for an actual class, otherwise just use whatever was returned
if (_.isFunction(PluginClass)) {
plugin = new PluginClass(ghost);
plugin = new PluginClass(createProxy(ghost));
} else {
plugin = PluginClass;
}
@ -57,7 +58,7 @@ loader = {
// Wrapping the install() with a when because it's possible
// to not return a promise from it.
return when(plugin.install(ghost)).then(function () {
return when(plugin.install(createProxy(ghost))).then(function () {
return when.resolve(plugin);
});
},
@ -73,7 +74,7 @@ loader = {
// Wrapping the activate() with a when because it's possible
// to not return a promise from it.
return when(plugin.activate(ghost)).then(function () {
return when(plugin.activate(createProxy(ghost))).then(function () {
return when.resolve(plugin);
});
}

View File

@ -0,0 +1,23 @@
var _ = require('underscore');
function createProxy(ghost) {
return {
filters: {
register: ghost.registerFilter,
unregister: ghost.unregisterFilter
},
helpers: {
register: ghost.registerThemeHelper,
registerAsync: ghost.registerAsyncThemeHelper
},
api: {
posts: _.pick(ghost.api.posts, 'browse', 'read'),
tags: ghost.api.tags,
notifications: _.pick(ghost.api.notifications, 'add'),
settings: _.pick(ghost.api.settings, 'read')
}
};
}
module.exports = createProxy;

View File

@ -0,0 +1,92 @@
/*globals describe, beforeEach, afterEach, before, it*/
var should = require('should'),
sinon = require('sinon'),
_ = require("underscore"),
// Stuff we are testing
createProxy = require('../../server/plugins/proxy');
describe('App Proxy', function () {
var sandbox,
fakeGhost;
beforeEach(function () {
sandbox = sinon.sandbox.create();
fakeGhost = {
registerFilter: sandbox.stub(),
unregisterFilter: sandbox.stub(),
registerThemeHelper: sandbox.stub(),
registerAsyncThemeHelper: sandbox.stub(),
api: {
posts: {
browse: sandbox.stub(),
read: sandbox.stub(),
edit: sandbox.stub(),
add: sandbox.stub(),
destroy: sandbox.stub()
},
users: {
browse: sandbox.stub(),
read: sandbox.stub(),
edit: sandbox.stub()
},
tags: {
all: sandbox.stub()
},
notifications: {
destroy: sandbox.stub(),
add: sandbox.stub()
},
settings: {
browse: sandbox.stub(),
read: sandbox.stub(),
add: sandbox.stub()
}
}
};
});
afterEach(function () {
sandbox.restore();
});
it('creates a ghost proxy', function () {
var proxy = createProxy(fakeGhost);
should.exist(proxy.filters);
proxy.filters.register.should.equal(fakeGhost.registerFilter);
proxy.filters.unregister.should.equal(fakeGhost.unregisterFilter);
should.exist(proxy.helpers);
proxy.helpers.register.should.equal(fakeGhost.registerThemeHelper);
proxy.helpers.registerAsync.should.equal(fakeGhost.registerAsyncThemeHelper);
should.exist(proxy.api);
should.exist(proxy.api.posts);
proxy.api.posts.browse.should.equal(fakeGhost.api.posts.browse);
proxy.api.posts.read.should.equal(fakeGhost.api.posts.read);
should.not.exist(proxy.api.posts.edit);
should.not.exist(proxy.api.posts.add);
should.not.exist(proxy.api.posts.destroy);
should.not.exist(proxy.api.users);
should.exist(proxy.api.tags);
proxy.api.tags.all.should.equal(fakeGhost.api.tags.all);
should.exist(proxy.api.notifications);
should.not.exist(proxy.api.notifications.destroy);
proxy.api.notifications.add.should.equal(fakeGhost.api.notifications.add);
should.exist(proxy.api.settings);
should.not.exist(proxy.api.settings.browse);
proxy.api.settings.read.should.equal(fakeGhost.api.settings.read);
should.not.exist(proxy.api.settings.add);
});
});

View File

@ -1,61 +0,0 @@
/*globals describe, beforeEach, afterEach, before, it*/
var testUtils = require('../utils'),
should = require('should'),
sinon = require('sinon'),
_ = require("underscore"),
when = require('when'),
knex = require('../../server/models/base').Knex,
errors = require('../../server/errorHandling'),
// Stuff we are testing
plugins = require('../../server/plugins'),
GhostPlugin = plugins.GhostPlugin,
loader = require('../../server/plugins/loader');
describe('Plugins', function () {
var sandbox;
before(function (done) {
testUtils.clearData().then(function () {
done();
}, done);
});
beforeEach(function (done) {
sandbox = sinon.sandbox.create();
testUtils.initData().then(function () {
done();
}, done);
});
afterEach(function (done) {
sandbox.restore();
testUtils.clearData().then(function () {
done();
}, done);
});
describe('GhostPlugin Class', function () {
should.exist(GhostPlugin);
it('sets app instance', function () {
var fakeGhost = {fake: true},
plugin = new GhostPlugin(fakeGhost);
plugin.app.should.equal(fakeGhost);
});
it('has default install, uninstall, activate and deactivate methods', function () {
var fakeGhost = {fake: true},
plugin = new GhostPlugin(fakeGhost);
_.isFunction(plugin.install).should.equal(true);
_.isFunction(plugin.uninstall).should.equal(true);
_.isFunction(plugin.activate).should.equal(true);
_.isFunction(plugin.deactivate).should.equal(true);
});
});
});