Skip to content

Instantly share code, notes, and snippets.

@mattm
Created April 1, 2009 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattm/88488 to your computer and use it in GitHub Desktop.
Save mattm/88488 to your computer and use it in GitHub Desktop.
def self.search(options = {})
length_criteria = (options[:length] == 'all' or options[:length].blank?) ? '' : 'and LENGTH(name) = ' + options[:length] + ' '
registered_criteria = options[:order] == 'views DESC' ? "and registered = false " : ''
viewable_criteria = "and viewable_at < '" + Time.now.utc.to_s(:db) + "' "
category_criteria = options[:category] != 'all' ? 'and category = "' + options[:category].downcase + '" ' : ''
public_criteria = options[:public_only] ? 'and public = true ' : ''
paginate :per_page => DOM_PER_PAGE, :page => options[:page],
:conditions => ["name like ? #{viewable_criteria}#{public_criteria}#{category_criteria}#{length_criteria}#{registered_criteria}", "%#{options[:search_term]}%"],
:order => "#{options[:order]}, id ASC"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment