The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: Around Filters are Here

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: Around Filters are Here Posted: Sep 22, 2006 6:41 AM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

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 :scope_to_owner

  def assets
    @assets = Asset.find(:all)
  end

  def scope_to_owner
    Asset.with_scope({
      :find => { :conditions => [ 'user_id = ?', @user.id ]},
      :create => { :user => @user }
    }) { 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 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.

tags: rubyonrails, rails

Read: What's New in Edge Rails: Around Filters are Here

Topic: Ruby vs. Python - why no-one should care Previous Topic   Next Topic Topic: Assaulted Battery

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use