Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2241 (reopened enhancement)

Opened 3 years ago

Last modified 1 year ago

[PATCH] Filters and callbacks need a more obvious way to stop processing

Reported by: fbeausoleil@ftml.net Assigned to: David
Priority: normal Milestone: 2.x
Component: ActionPack Version: 0.13.1
Severity: normal Keywords: filter callback risky
Cc:

Description

I always have a very hard time remembering if I should return true or false to cancel the request. If only I could throw :abort...

Attachments

throw-to-abort-filters.patch (2.1 kB) - added by fbeausoleil@ftml.net on 09/21/05 01:15:14.
Test, documentation and implementation to throw :abort in before filters

Change History

09/21/05 01:15:14 changed by fbeausoleil@ftml.net

  • attachment throw-to-abort-filters.patch added.

Test, documentation and implementation to throw :abort in before filters

09/21/05 05:48:10 changed by david

  • status changed from new to closed.
  • resolution set to wontfix.

I don't think its going to be easier to remember to introduce another way as well. Returning false breaks the chain, so does raising an exception.

09/21/05 10:25:02 changed by fbeausoleil@ftml.net

I left the other methods in for backwards compatibility. I was thinking that post 1.0, we would remove all methods, save for the throw(:abort) one. Isn't it much more explicit ?

09/21/05 10:28:16 changed by david

We're going to be in Rails 1.x mode for quite a while (where backwards compatibility is to be ensured). This may indeed a be interesting alternative for Rails 2.0. But write it down as an idea for then.

09/29/05 08:48:04 changed by François Beausoleil <fbeausoleil@ftml.net>

  • status changed from closed to reopened.
  • resolution deleted.
  • milestone set to 2.x.

Moving to 2.x milestone.

09/29/05 13:58:04 changed by bitsweat

  • keywords changed from before_filter filters to filter callback.
  • summary changed from [PATCH] Allow before filters to throw :abort to cancel the request to [PATCH] Filters and callbacks need a more obvious way to stop processing.

Doing throw(:continue) if you don't want to stop processing is also explicit, but that doesn't make it any good. I think throwing a symbol is cumbersome and error-prone.

I agree that it can be hard to remember whether a true return value means continue or stop, but would prefer to see a method call: e.g. stop! if stop_condition

09/29/05 14:06:01 changed by François Beausoleil <fbeausoleil@ftml.net>

Then, stop! would simply throw a Symbol, not unlike Find.prune ?

09/29/05 15:17:12 changed by bitsweat

Sure :-) I think throwing a symbol is good implementation but poor API choice. Wrapping it in a nice, documented method is all that's needed.

12/05/06 22:20:26 changed by gunark

After spending 3 hours tracing through active record code (callbacks.rb is pretty much unreadable at first glance by the way... reflection overkill ftw!) I realized that I was accidentally returning false in one of my callbacks. I think it's way too easy to slip up on this, and it's way too easy to not realize where you slipped up.

throw :abort, or throw :abort via stop! is an excellent idea, and I would strongly suggest pushing this up to 1.2 and generating a deprecation warning when a callback returns false (at the very least this would save some of us a few hours of banging our heads against hard surfaces -- judging from the many tickets on this, this is not a rare problem).

12/05/06 22:25:15 changed by bitsweat

Gunark, your development.log indicates when a filter chain halts due to returning false.

02/22/07 21:14:42 changed by josh

  • keywords changed from filter callback to filter callback risky.