Agile Web Development

Build it. Launch it. Love it.

Filter

Filter let you specify common filtering options, in order to generate a filtered condition, useful for filtering from request params.

Example usage:

   filter = Filter.new
   filter.multiple types, :type_id

   people = People.find(:all, :conditions => filter.conditions)

   Generates: SELECT * FROM people WHERE (type_id IN (1,2))

   filter.equal type, :type_id

   Generates: SELECT * FROM people WHERE (type_id = 1)

Is very beautifull when you whant to add multiple conditions if other (conditions) are verified like:

   conditions = Filter.new
   conditions.great params[:type][:id], :type_id unless params[:type][:id].blank?
   conditions.equal params[:model][:id], :model_id unless params[:model][:id].blank?

   MyModel.find(:all, :conditions => conditions)

Code originally created by Divoxx who I give my special Thanks Code edited by http://blog.lipsiasoft.com

Vitals

Home http://blog.lipsiasoft.com/articles/2007/06/05/filtrare-i-risultati-in-modo-agile-con-ruby-on-rails
Repository http://lipsiasoft.googlecode.com/svn/trunk/filter
License Rails' (MIT)
Tags Tag_red anal filter params
Rating (52 votes)
Owner Davide D'Agostino (DAddYE)
Created 5 June 2007

Comments

  • Avatar
    24 August 2007

    Would you mind to add this? <pre> # Defines a filtering that checks for not equality. # # Example: # @filter = Filter.new # @filter.isnot params[:id], 'an_id' def isnot(key, filtered)

    key = key.to_s
    @filters &lt;&lt; [&quot;#{filtered} != ?&quot;, key] unless key.nil? || key.empty?
    

    end </pre>

    Don't know if the formatting works. If not: it is a "!=" operator/filter.

  • Avatar
    28 December 2007

    Thanks Andreas, sorry but I see now your comment, yes I can do what you say in the next version!

    thanks so much for your feedback!

  • pj
    19 August 2008

    In rails 2.1 you have to use ::Filter.new with this plugin because of the ActionController::Filters::Filter object

Add a comment