For the second time I’ve had my virtual hand slapped for trumpeting a poor usage of with_scope . For the record, I want to make it clear that one should be using the rich domain model that active record provides to operate on associated objects: user.assets.find() and user.asset.create() instead of scoping to a user as my and Roman’s examples implied. Just a case of not thinking hard enough to come up with an appropriate example. Sorry David, won’t happen again!
I’ve mentioned the Meantime filter plugin by Roman Le Negrate in the past as a very useful little utility – apparently the core team agrees as around filtering is now part of edge Rails.
Usage is pretty much the same as with meantime:
class AssetController < ActionController
before_filter :verify_login
around_filter :benchmark
def assets
@assets = Asset.find(:all)
end
def benchmark
benchmark("Asset benchmark") { yield }
end
end
You can also define a class to handle your filtering needs (whose filter method yields to the action block)
class AssetController < ActionController
around_filter BenchmarkFilter # or BenchmarkFilter.new
...
class BenchmarkFilter
def filter
benchmark("Benchmarking...") { yield }
end
end
end
Or you can directly pass a Proc in as the filter:
class AssetController < ActionController
around_filter (:only => :assets) do |controller,action_block|
controller.benchmark("Benchmarking...") { action_block.call }
end
...
end
Since the around filter explicitly governs the execution of the action being requested, you can use them to skip the action completely. This would act the same as a before_filter that returned false.
tags: rubyonrails, rails

i need to get around the school filters…GAHHH