Skip to content

Instantly share code, notes, and snippets.

@bguthrie
Created May 6, 2010 11:47
Show Gist options
  • Save bguthrie/392041 to your computer and use it in GitHub Desktop.
Save bguthrie/392041 to your computer and use it in GitHub Desktop.
# Gem idea, extracted from ResourceFull. Thoughts on the API?
class User < ActiveRecord::Base
named_scope :born_after, lambda {|date|
{ :conditions => ["birthdate >= ?", Date.parse(date)] }
}
query_set :filter do
filter_with_scope :active
queryable_with :email
queryable_with :born_after, :scope => :born_after
end
query_set :search do
queryable_with :first_name, :last_name, :fuzzy => true
queryable_with :username, :column => :email, :fuzzy => true
end
end
User.filter
#=> User.active
User.filter(:email => "[email protected]")
#=> User.active.scoped(:conditions => { :email => "[email protected]" })
User.filter(:born_after => "04/03/2010", :email => "[email protected]")
#=> User.active.born_after("04/03/2010").scoped(:conditions => { :email => "[email protected]" })
User.search(:first_name => "Guy", :last_name => "Three")
#=> User.scoped(:conditions => { :first_name => "%Guy%" }).scoped(:conditions => { :last_name => "%Three%" })
User.search(:username => "gthree")
#=> User.scoped(:conditions => { :email => "%gthree%"})
@irfn
Copy link

irfn commented May 6, 2010

or just use searchlogic -> http://github.com/binarylogic/searchlogic
User.search(:awesome => true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment