Skip to content

Commit

Permalink
Implement / test sails.registerAction
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Sep 22, 2016
1 parent 816167e commit dd9af88
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/app/Sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function Sails() {
this.put = _.bind(this.put, this);
this['delete'] = _.bind(this['delete'], this);
this.getActions = _.bind(this.getActions, this);
this.registerAction = _.bind(this.registerAction, this);
this.controller = this.initializeController(this);
}

Expand Down Expand Up @@ -95,6 +96,8 @@ Sails.prototype.getRouteFor = require('./get-route-for');
Sails.prototype.getUrlFor = require('./get-url-for');

Sails.prototype.getActions = require('./get-actions');
Sails.prototype.registerAction = require('./register-action');



// Experimental methods
Expand Down
21 changes: 21 additions & 0 deletions lib/app/register-action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Sails.prototype.registerAction()
*
* Register an action with Sails.
*
* Registered actions may be subsequently bound to routes.
* This method will throw an error if an action with the specified
* identity has already been registered.
*
* @param {fn/node-machine def} action The action to register
* @param {string} identity The identity of the action
*
*
* @api public
*/
module.exports = function registerAction(action, identity) {

// Use the private `loadAction` method, without `force`.
this.controller.loadAction(action, identity);

};
22 changes: 21 additions & 1 deletion test/integration/controllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,29 @@ describe('controllers :: ', function() {

});

describe('sails.registerAction() :: ', function() {

it('should allow registering a new action at runtime, if it doesn\'t conflict with an existing action', function() {
sailsApp.registerAction(function(req, res) {return res.ok('ok!');}, 'new-action');
assert(_.isFunction(sailsApp._actions['new-action']), 'registerAction() succeeded, but could not find the registered action in the sails._actions dictionary!');
});

it('should allow not registering a new action at runtime, if it conflicts with an existing action', function() {
try {
sailsApp.registerAction(function(req, res) {return res.ok('ok!');}, 'top-level-standalone-fn');
} catch (err) {
assert.equal(err.code, 'E_CONFLICT');
assert.equal(err.identity, 'top-level-standalone-fn');
return;
}
throw new Error('Expected an E_CONFLICT error, but didn\'t get one!');
});

});

});

describe.only('with conflicting actions in api/controllers', function() {
describe('with conflicting actions in api/controllers', function() {

var curDir, tmpDir, sailsApp;
var warn;
Expand Down

0 comments on commit dd9af88

Please sign in to comment.