Skip to content

Commit

Permalink
Skip filter
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Sep 8, 2011
1 parent 28947a7 commit 4e8c594
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
@@ -1,8 +1,10 @@
0.1.8
0.1.8.1
=====

- RIAK datamapper *beta*, thanks Taliesin Sisson (https://github.com/taliesins)
- routes tuning (map.all, map.root)
- controller changes: params, body, session objects published in controller context
- skipBeforeFilter, skipAfterFilter support
- some bugfixes
- minor changes

Expand Down
62 changes: 55 additions & 7 deletions lib/controller.js
@@ -1,4 +1,5 @@
var cache = {}, layoutsCache = {}, fs = require('fs'),
path = require('path'),
// import railway utils
utils = require('./railway_utils'),
safe_merge = utils.safe_merge,
Expand All @@ -9,8 +10,6 @@ var cache = {}, layoutsCache = {}, fs = require('fs'),
pluralize = utils.pluralize,
$ = utils.stylize.$,
log = utils.debug,
path = require('path'),
fs = require('fs'),
runCode = utils.runCode;

function Controller(name, file, basePath) {
Expand Down Expand Up @@ -50,21 +49,70 @@ function Controller(name, file, basePath) {
};

this.before = this.beforeFilter = function (f, params) {
beforeFilters.push([f, params]);
beforeFilters.push(filter(arguments));
};

this.prependBefore = this.prependBeforeFilter = function (f, params) {
beforeFilters.unshift([f, params]);
beforeFilters.unshift(filter(arguments));
};

this.after = this.afterFilter = function (f, params) {
afterFilters.push([f, params]);
afterFilters.push(filter(arguments));
};

this.prependAfter = this.prependAfter = function (f, params) {
afterFilters.unshift([f, params]);
afterFilters.unshift(filter(arguments));
};


/*
* Need to store name of filter to be able to skip it
*/
function filter(args) {
if (typeof args[0] === 'string' && typeof args[1] === 'function') {
// change order
return [args[1], args[2], args[0]];
} else {
// normal order
return Array.prototype.slice.call(args);
}
}

this.skipBeforeFilter = function (name, only) {
beforeFilters.forEach(function (filter, i) {
if (filter && filter[2] && name === filter[2]) {
skipFilter(beforeFilters, i, only ? only.only : null);
}
});
};

this.skipAfterFilter = function (name, only) {
afterFilters.forEach(function (filter, i) {
if (filter && filter[2] && name === filter[2]) {
skipFilter(afterFilters, i, only ? only.only : null);
}
});
};

function skipFilter(filters, index, only) {
if (!only) {
delete filters[x];
} else if (filters[index][1]) {
if (!filters[index][1].except) {
filters[index][1].except = [];
} else if (typeof filters[index][1].except === 'string') {
filters[index][1].except = [filters[index][1].except];
}
if (typeof only === 'string') {
filters[index][1].except.push(only);
} else if (only && only.forEach) {
only.forEach(function (name) {
filters[index][1].except.push(name);
});
}
}
}

this.layout = function (l) {
if (typeof l !== 'undefined') {
layout = l;
Expand Down Expand Up @@ -148,7 +196,7 @@ function Controller(name, file, basePath) {
var params = f[1];
if (!params) {
enqueue();
} else if (params.only && params.only.indexOf(actionName) !== -1) {
} else if (params.only && params.only.indexOf(actionName) !== -1 && (!params.except || params.except.indexOf(actionName) === -1)) {
enqueue();
} else if (params.except && params.except.indexOf(actionName) === -1) {
enqueue();
Expand Down

0 comments on commit 4e8c594

Please sign in to comment.