diff --git a/lib/app/Sails.js b/lib/app/Sails.js index 54a1e9031..d44881e31 100644 --- a/lib/app/Sails.js +++ b/lib/app/Sails.js @@ -66,6 +66,7 @@ function Sails() { this.post = _.bind(this.post, this); this.put = _.bind(this.put, this); this['delete'] = _.bind(this['delete'], this); + this.getActions = _.bind(this.getActions, this); this.controller = this.initializeController(this); } @@ -93,6 +94,7 @@ Sails.prototype.getBaseUrl = Sails.prototype.getBaseurl; Sails.prototype.getRouteFor = require('./get-route-for'); Sails.prototype.getUrlFor = require('./get-url-for'); +Sails.prototype.getActions = require('./get-actions'); // Experimental methods diff --git a/lib/app/get-actions.js b/lib/app/get-actions.js new file mode 100644 index 000000000..635021dff --- /dev/null +++ b/lib/app/get-actions.js @@ -0,0 +1,19 @@ +/** + * Module dependencies. + */ +var _ = require('lodash'); + +/** + * Sails.prototype.getActions() + * + * Return a shallow clone of the loaded actions dictionary. + * + * @returns {dictionary} A shallow clone of sails._actions + * + * @api public + */ +module.exports = function getActions() { + + return _.clone(this._actions); + +}; diff --git a/test/integration/controllers.test.js b/test/integration/controllers.test.js index f875df56f..1400c86ea 100644 --- a/test/integration/controllers.test.js +++ b/test/integration/controllers.test.js @@ -243,6 +243,27 @@ describe('controllers :: ', function() { }); }); + it('should return a shallow clone of the actions dictionary when `sails.getActions` is called', function() { + var actions = sailsApp.getActions(); + assert(actions !== sailsApp._actions, 'sails.getActions is supposed to return a shallow clone, but got an exact reference!'); + var expectedActions = [ + 'toplevellegacy.fnaction', + 'toplevellegacy.machineaction', + 'top-level-standalone-fn', + 'top-level-standalone-machine', + 'somefolder.someotherfolder.nestedlegacy.fnaction', + 'somefolder.someotherfolder.nestedlegacy.machineaction', + 'somefolder.someotherfolder.nested-standalone-machine' + ]; + var unexpectedActions = _.difference(_.keys(actions), expectedActions); + assert(!unexpectedActions.length, 'Loaded unexpected actions:\n' + util.inspect(unexpectedActions)); + _.each(expectedActions, function(expectedAction) { + assert(actions[expectedAction], 'Did not load expected action `' + expectedAction + '`'); + assert(_.isFunction(actions[expectedAction]), 'Expected action `' + expectedAction + '` loaded, but instead of a function it\'s a ' + typeof(actions[expectedAction])); + }); + + }); + }); });