QueryableWith¶ ↑
QueryableWith is an ActiveRecord library for creating and combining reusable sets of scopes. Instead of building up your query using if-conditions checking for the existence of certain parameters–a common use case in reporting and controller query logic–QueryableWith combines them together, and only uses the ones the params you pass need.
For example,
class User < ActiveRecord::Base named_scope :born_after, lambda {|date| { :conditions => ["birthdate >= ?", date] } } named_scope :active, :conditions => { :active => true } query_set :filter do add_scope :active queryable_with :email queryable_with(:born_after) { |d| Date.parse(d) } end end
will add a method, User.filter
, that knows how to filter on the email
and born_after
parameters by, respectively, performing an equality search against that column, and using the defined name scope (after passing the given date value, in this case a string, through the block). It will always add the active
scope. For example,
User.filter #=> User.active User.filter(:email => "gthreepwood@melee.gov") #=> User.active.scoped(:conditions => { :email => "gthreepwood@melee.gov"}) User.filter(:born_after => "10/4/2010") #=> User.active.born_after(#<Date: 4910947/2,0,2299161>)
See QueryableWith::ClassMethods#query_set for more information.
Another Example¶ ↑
class User < ActiveRecord::Base query_set :search do # If queried by :name, execute a LIKE query on both the :first_name and :last_name columns. # User.scoped(:conditions => ["(users.first_name LIKE ?) OR (users.last_name LIKE ?)", ...]) queryable_with :name, :columns => [ :first_name, :last_name ], :wildcard => true # If queried by :username, execute a LIKE query on the the email column. # User.scoped(:conditions => ["(users.email LIKE ?)", ...]) queryable_with :username, :column => :email, :wildcard => true end end User.search(:name => ["Guy", "Three"]) #=> User.scoped(:conditions => ["(users.first_name LIKE ?) OR (users.last_name LIKE ?) OR ...", "%Guy%", "%Three%", "%Guy%", "%Three%"] User.search(:username => "gthree") #=> User.scoped(:conditions => { :email => "%gthree%"})
Installation and usage¶ ↑
QueryableWith is available as a gem. It has a dependency on ActiveRecord, which it will automatically extend with the necessary class methods when required. It is not yet compatible with Rails3.
gem install queryable_with require 'queryable_with'
License¶ ↑
Copyright © 2010 Brian Guthrie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.