Skip to content

Allow selecting which actions a filter applies to using 'only' and 'except' #74

Open
wants to merge 1 commit into from

3 participants

@ragulka
ragulka commented Jan 30, 2013

This is a feature request.

I find that it would be very useful if we could specify which controller actions we want to apply a filter to, in a similar manner how we can define resourceful routes.

I imagine something like this:

UsersController.before({ only: ['show','create']}, function(next) {
  next();
});

UsersController.before({ except: ['destroy']}, function(next) {
  next();
});

This becomes very useful when you need to apply a filter to more than one, but not all your actions. Keeps things DRY.

EDIT: Looking at the source code I realized that it's already possible to apply a filter to multiple actions when passing an array of actions instead a single action name or wilcard. Still, it would be nice if we could apply filters to all actions, except a few.

@drudge
@ragulka
ragulka commented Jan 30, 2013

Just added a pull request for this. I kept the existing API and added both only and except options.

I am not sure about the after filter tests I wrote though, it's my first time trying vows, so maybe someone could take a look at the,

@ragulka
ragulka commented Jan 31, 2013

Until this pull request is merged (if it will be), it's also possible to use the following code snippet:

var allExceptDestroy = UsersController._actions().filter(function(a) {
  return ['destroy'].indexOf(a) == -1;
});

UsersController.before( allExceptDestory, function (next) {
  // this filter will be applied to all controller actions except the destroy action
  next();
});
@jaredhanson
Owner

I like the idea of the except option. I modified how filters are invoked in commit 9b9d517, to address #68 and #97, which caused a conflict with this patch. As soon as I finish some other clean ups, I'll port this over to the updated code. Feel free to tackle it on your own in the meantime, and I'll merge it in if you beat me to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.