Skip to content

Instantly share code, notes, and snippets.

@mrb
Forked from quirkey/snippet.txt
Created August 23, 2010 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrb/545998 to your computer and use it in GitHub Desktop.
Save mrb/545998 to your computer and use it in GitHub Desktop.
module Searchable
def self.searchable_fields
[]
end
def self.included(klass)
klass.named_scope :by_search, lambda {|q, options|
if q.present?
search_text = [klass.searchable_fields].flatten.collect {|f|
scoped_name = (f =~ /\./ ? f : "#{klass.table_name}.#{f}")
"#{scoped_name} ILIKE :q"
}.join(' OR ')
{:conditions => [search_text, {:q => "%#{q}%"}]}.merge(options)
else
{}
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment