This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
Original Post: What's New in Edge Rails: Around Filters are Here
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
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.
You can also define a class to handle your filtering needs (whose filter method yields to the action block)
class AssetController < ActionController
around_filter ScopeFilter # or ScopeFilter.new
...
end
class ScopeFilter
def filter
Asset.with_scope({
:find => { :conditions => [ 'user_id = ?', params[:user_id]},
:create => { :user_id => params[:user_id] }
}) { yield }
end
end
Or you can directly pass a Proc in as the filter:
class AssetController < ActionController
around_filter (:only => :assets) do |controller,action_block|
Asset.with_scope({
:find => { :conditions => [ 'user_id = ?', params[:user_id]},
:create => { :user_id => params[:user_id] }
}) { action_block.call }
end
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.