The current version of Record Filter is now 0.9.8. If you are using a previous version, see this post for upgrade instructions.
Record Filter gives you the full power of ActiveRecord's query building tools with a heaping helping of DSL thrown in to save your Ruby-loving fingers from writing nasty SQL.
Blog.filter do
any_of do
having(:posts) do
with(:permalink, nil)
having(:comments).with(:offensive, true)
end
with(:created_at).greater_than(3.days.ago)
end
order(:created_at)
limit(10)
end
Record Filter also supports named filters, which work much like named scopes and are even compatible with them. The syntax for using named filters is exactly the same as for creating filters on the fly.
class Post < ActiveRecord::Base
named_scope :with_permalink, lambda { |value|
{ :conditions => { :permalink => value } }
}
named_filter(:with_content) { |value| with(:content, value) }
named_filter(:created_recently) { with(:created_at).greater_than(1.day.ago) }
end
Post.created_recently.with_permalink(nil).with_content('abc')
Record Filter supports all of the following SQL-stomping features:
- Pure ruby API eliminates the need for hard-coded SQL
- Works seamlessly with existing ActiveRecord APIs, including named scopes
- Supports creation of ad-hoc filters as well as named filters that can be associated with object types
- Allows chaining of filters and named filters with each other and with named scopes to create complex queries from simple building blocks
- Takes advantage of the associations in your ActiveRecord objects for a clean implicit join API
- 100% does not silently throw away your joins like named_scopes do
For further documentation, check out the project page for a getting-started guide with plenty of examples or take a peek the RDocs for all the gory details.
Get It
$ sudo gem install aub-record_filter --source=http://gems.github.com
In Rails, you'll need to add this to your environment.rb file:
config.gem 'aub-record_filter', :lib => 'record_filter', :source => 'http://gems.github.com'