Skip to content

Commit

Permalink
Implement sails.getActions()
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Sep 22, 2016
1 parent d0e4a3a commit 5598179
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/app/Sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/app/get-actions.js
Original file line number Diff line number Diff line change
@@ -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);

};
21 changes: 21 additions & 0 deletions test/integration/controllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
});

});

});

});

0 comments on commit 5598179

Please sign in to comment.