Plugins - scan_fields
Add to favoritesAlleviate’s the need for long and ugly ActiveRecord::find search conditions by automatically searching all columns of a model.
Features:
* Scans all fields of a model, excluding types of binary of boolean. * Customizable - allows you to exclude certain fields or trim the edges of the search string for broader searches. * Date and time friendly - can automatically transform dates or times into their native DB format. * Searches can be case sensitive or case insensitive
Usage:
scan_fields(search_string, options)
Examples:
User.find(:all, :conditions => User.scan_fields("michael"))
User.find(:all, :conditions => User.scan_fields("11:11PM"))
User.find(:all, :conditions => User.scan_fields("2/30/80"))
User.find(:all, :conditions => User.scan_fields("03-13-2002"))
User.find(:all, :conditions => User.scan_fields(32, :exclude => ‘id’))
User.find(:all, :conditions => User.scan_fields("michael", :exclude => [‘zipcode’, ‘address’], :trim => 2))
You may also use the + operator to combine conditions. For example: User.find(:all, :conditions => User.scan_fields("ruby")+[‘some_id=?’,32])
Options:
- :exclude — Columns to exclude from the query. This can be either a string (1 column) or an array of column names.
- :trim — Specifies how many characters to trim from the beginning and end of the search string before searching. The default is 0.
Note: If the the value would result in an empty string, the search string will be unaffeted.
- :case — Specifies whether the pattern match should be case sensitive. The possible values are true, and false (default).
Todo:
- Integrate with ActiveRecord’s find method
- Add association handling
- Support support for more RDBMS specific proprietary comparison operators
