Skip to content

Commit

Permalink
Merge pull request balderdashy#840 from xdissent/case-insensitive-views
Browse files Browse the repository at this point in the history
Case insensitive views
  • Loading branch information
mikermcneil committed Sep 16, 2013
2 parents 9fb10eb + 15a896c commit 3a410d3
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = function (sails) {
var util = require( '../util' ),
async = require('async'),
startServer = require('./start')(sails),
bodyParserRetry = require('./bodyParserRetry')(sails);
bodyParserRetry = require('./bodyParserRetry')(sails),
View = require('./view');


/**
Expand Down Expand Up @@ -48,6 +49,9 @@ module.exports = function (sails) {
}
else sails.express.server = createServer(sails.express.app);

// Use View subclass to allow case-insensitive view lookups
sails.express.app.set('view', View);

// Set up location of server-side views and their engine
sails.express.app.set('views', sails.config.paths.views);

Expand Down
34 changes: 34 additions & 0 deletions lib/express/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var util = require('util'),
path = require('path'),
glob = require('glob'),
ExpressView = require('express/lib/view'),
utils = require('express/lib/utils'),
basename = path.basename,
dirname = path.dirname,
join = path.join,
globPath = function (path) {
return glob.sync(path, {nocase: true});
},
exists = function (path) {
return globPath(path).length > 0;
};

function SailsView(name, options) {
ExpressView.call(this, name, options);
};

util.inherits(SailsView, ExpressView);

SailsView.prototype.lookup = function(path) {
var ext = this.ext;

// <path>.<engine>
if (!utils.isAbsolute(path)) path = join(this.root, path);
if (exists(path)) return globPath(path)[0];

// <path>/index.<engine>
path = join(dirname(path), basename(path, ext), 'index' + ext);
if (exists(path)) return globPath(path)[0];
};

module.exports = SailsView;
52 changes: 52 additions & 0 deletions test/router/integration/router.specifiedRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,56 @@ describe('Specified routes', function() {
});
});
});

it('should be case-insensitive', function(done) {

httpHelper.writeRoutes({
'get /testRoute': {
controller: 'test',
action: 'verb'
}
});

httpHelper.testRoute('get', 'tEStrOutE', function(err, response) {
if (err) done(new Error(err));

assert(response.body === 'get');
done();
});
});

it('should accept case-insensitive controller key', function(done) {

httpHelper.writeRoutes({
'get /testRoute': {
controller: 'tEsT',
action: 'verb'
}
});

httpHelper.testRoute('get', 'testRoute', function(err, response) {
if (err) done(new Error(err));

assert(response.body === 'get');
done();
});
});

it('should accept case-insensitive action key', function(done) {

httpHelper.writeRoutes({
'get /testRoute': {
controller: 'test',
action: 'capiTalleTTers'
}
});

httpHelper.testRoute('get', 'testRoute', function(err, response) {
if (err) done(new Error(err));

assert(response.body === 'CapitalLetters');
done();
});
});

});

0 comments on commit 3a410d3

Please sign in to comment.