acts_as_filterable
acts_as_filterable is an ActiveRecord plugin that was hacked together (originally a monkey patch) to avoid repeating the same text filtering logic that was re-implemented all over the place in a legacy domain model.
Background
You might ask why we just didn’t convert the column value. That would be the right way to go but with tens of millions of rows and numerous legacy apps that expect the data to be a character string; the alternative seemed much more appealing for the time being :). So putting this in place would avoid the garbage data coming in moving forward and assure that the logic is implemented in a sane (and re-usable) way. Hopefully someone else can find a use for it as well…
Current filters in place:
-
filter_for_digits: leaves only numeric values
-
filter_for_uppercase: uppercase all alpha characters
-
filter_for_lowercase: lowercase all alpha characters
-
filter_for_whitespace: strips and non-essential whitespace out of a string (leaving only single whitespace characters).
Features I’d like to add in the future:
-
User-defined custom filters configured via DSL.
-
Additional macros that filter decimal values, etc.
-
Allow the developer to opt-in models for filtering and not include the plugin in ActiveRecord::Base
Runtime Dependencies
-
Rails 1.2+
-
Tested on Ruby 1.8.6, 1.8.7, 1.9.1
Install
Rails
config.gem "acts_as_filterable", :source => "http://gemcutter.org"
Bundler
source "http://gemcutter.org"
gem "acts_as_filterable", "0.2.0"
RubyGems
gem install acts_as_filterable --source http://gemcutter.org
Usage
class MyModel < ActiveRecord::Base
filter_for_digits :phone_number, :fax_number
end
Contributing
If something is broken or you need a feature; you know the deal. Don’t be part of the problem. Patches or pull requests are welcome.
Copyright
Copyright © 2009 Rob Ares. See LICENSE for details.