Skip to content

Commit

Permalink
policy can be called like factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Sileghem committed Jul 11, 2015
1 parent 77c7f6c commit 1ba3599
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ function parseEsprima(input, fromFactory){

var factoryName = input.callee.name;

var factory = require(sails.config.paths.policyFactories + '/' + factoryName);
try {
var factory = require(sails.config.paths.policyFactories + '/' + factoryName);
} catch (e) {
return require(sails.config.paths.policies + '/' + factoryName);
}

var args = input.arguments.map(function(arg){
return this.parseEsprima(arg, true);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-hook-parametized-policies",
"version": "0.2.3",
"version": "0.3.0",
"description": "",
"main": "index.js",
"directories": {
Expand Down
8 changes: 8 additions & 0 deletions test/basicSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('parametized policies hook - basic', function(){
'GET /admin': 'MainController.admin',
'GET /multiple': 'MainController.multiple',
'GET /json': 'MainController.json',
'GET /okParams': 'MainController.okParams',
},
policies: {
'MainController': {
Expand All @@ -40,6 +41,7 @@ describe('parametized policies hook - basic', function(){
'ok': 'accept',
'multiple': 'multiple(1, \'one\')',
'json': 'json(\'["json"]\')',
'okParams': 'accept()'
}
}
}, function(err, _sails){
Expand Down Expand Up @@ -95,5 +97,11 @@ describe('parametized policies hook - basic', function(){
request(sails.hooks.http.app)
.get('/json')
.expect('json', done);
});

it('should handle policy that look like policy factory', function(done){
request(sails.hooks.http.app)
.get('/ok')
.expect(200, done);
})
})
12 changes: 8 additions & 4 deletions test/controllers/MainController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
// GET /admin
module.exports.admin = function(req, res){
res.send();
}
};


// GET /user
module.exports.user = function(req, res){
res.send();
}
};

module.exports.ok = function(req, res){
res.send();
}
};

module.exports.multiple = function(req, res){
res.send();
};

module.exports.json = function(req, res){
res.send();
}
};

module.exports.okParams = function(req, res){
res.send();
};

0 comments on commit 1ba3599

Please sign in to comment.