0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Direct api access for app proxy

- proxy doesn't need a ghost object :)
This commit is contained in:
Hannah Wolfe 2013-11-29 16:26:56 +00:00
parent e4a5356a69
commit ed6455f5a4
3 changed files with 72 additions and 96 deletions

View file

@ -2,43 +2,29 @@
var path = require('path'),
_ = require('underscore'),
when = require('when'),
createProxy = require('./proxy'),
appProxy = require('./proxy'),
config = require('../config'),
ghostInstance,
loader;
function getGhostInstance() {
if (ghostInstance) {
return ghostInstance;
}
var Ghost = require('../../ghost');
ghostInstance = new Ghost();
return ghostInstance;
}
// Get a relative path to the given plugins root, defaults
// to be relative to __dirname
function getPluginRelativePath(name, relativeTo, ghost) {
ghost = ghost || getGhostInstance();
function getPluginRelativePath(name, relativeTo) {
relativeTo = relativeTo || __dirname;
return path.relative(relativeTo, path.join(config.paths().pluginPath, name));
}
function getPluginByName(name, ghost) {
ghost = ghost || getGhostInstance();
function getPluginByName(name) {
// Grab the plugin class to instantiate
var PluginClass = require(getPluginRelativePath(name)),
plugin;
// Check for an actual class, otherwise just use whatever was returned
if (_.isFunction(PluginClass)) {
plugin = new PluginClass(createProxy(ghost));
plugin = new PluginClass(appProxy);
} else {
plugin = PluginClass;
}
@ -49,8 +35,8 @@ function getPluginByName(name, ghost) {
// The loader is responsible for loading plugins
loader = {
// Load a plugin and return the instantiated plugin
installPluginByName: function (name, ghost) {
var plugin = getPluginByName(name, ghost);
installPluginByName: function (name) {
var plugin = getPluginByName(name);
// Check for an install() method on the plugin.
if (!_.isFunction(plugin.install)) {
@ -59,14 +45,14 @@ loader = {
// Wrapping the install() with a when because it's possible
// to not return a promise from it.
return when(plugin.install(createProxy(ghost))).then(function () {
return when(plugin.install(appProxy)).then(function () {
return when.resolve(plugin);
});
},
// Activate a plugin and return it
activatePluginByName: function (name, ghost) {
var plugin = getPluginByName(name, ghost);
activatePluginByName: function (name) {
var plugin = getPluginByName(name);
// Check for an activate() method on the plugin.
if (!_.isFunction(plugin.activate)) {
@ -75,7 +61,7 @@ loader = {
// Wrapping the activate() with a when because it's possible
// to not return a promise from it.
return when(plugin.activate(createProxy(ghost))).then(function () {
return when(plugin.activate(appProxy)).then(function () {
return when.resolve(plugin);
});
}

View file

@ -1,25 +1,24 @@
var _ = require('underscore'),
api = require('../api'),
helpers = require('../helpers'),
filters = require('../filters');
function createProxy(ghost) {
var proxy = {
return {
filters: {
register: filters.registerFilter,
unregister: filters.unregisterFilter
},
helpers: {
register: helpers.registerThemeHelper,
registerAsync: helpers.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')
}
};
}
filters: {
register: filters.registerFilter,
unregister: filters.unregisterFilter
},
helpers: {
register: helpers.registerThemeHelper,
registerAsync: helpers.registerAsyncThemeHelper
},
api: {
posts: _.pick(api.posts, 'browse', 'read'),
tags: api.tags,
notifications: _.pick(api.notifications, 'add'),
settings: _.pick(api.settings, 'read')
}
};
module.exports = createProxy;
module.exports = proxy;

View file

@ -6,42 +6,40 @@ var should = require('should'),
filters = require('../../server/filters'),
// Stuff we are testing
createProxy = require('../../server/plugins/proxy');
appProxy = require('../../server/plugins/proxy');
describe('App Proxy', function () {
var sandbox,
fakeGhost;
fakeApi;
beforeEach(function () {
sandbox = sinon.sandbox.create();
fakeGhost = {
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()
}
fakeApi = {
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()
}
};
});
@ -51,38 +49,31 @@ describe('App Proxy', function () {
});
it('creates a ghost proxy', function () {
var proxy = createProxy(fakeGhost);
should.exist(appProxy.filters);
appProxy.filters.register.should.equal(filters.registerFilter);
appProxy.filters.unregister.should.equal(filters.unregisterFilter);
should.exist(proxy.filters);
proxy.filters.register.should.equal(filters.registerFilter);
proxy.filters.unregister.should.equal(filters.unregisterFilter);
should.exist(appProxy.helpers);
appProxy.helpers.register.should.equal(helpers.registerThemeHelper);
appProxy.helpers.registerAsync.should.equal(helpers.registerAsyncThemeHelper);
should.exist(proxy.helpers);
proxy.helpers.register.should.equal(helpers.registerThemeHelper);
proxy.helpers.registerAsync.should.equal(helpers.registerAsyncThemeHelper);
should.exist(appProxy.api);
should.exist(proxy.api);
should.exist(appProxy.api.posts);
should.not.exist(appProxy.api.posts.edit);
should.not.exist(appProxy.api.posts.add);
should.not.exist(appProxy.api.posts.destroy);
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(appProxy.api.users);
should.not.exist(proxy.api.users);
should.exist(appProxy.api.tags);
should.exist(proxy.api.tags);
proxy.api.tags.all.should.equal(fakeGhost.api.tags.all);
should.exist(appProxy.api.notifications);
should.not.exist(appProxy.api.notifications.destroy);
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);
should.exist(appProxy.api.settings);
should.not.exist(appProxy.api.settings.browse);
should.not.exist(appProxy.api.settings.add);
});
});