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

View file

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

View file

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