Skip to content

Instantly share code, notes, and snippets.

@joshrendek
Created December 16, 2010 07:26
Show Gist options
  • Save joshrendek/743156 to your computer and use it in GitHub Desktop.
Save joshrendek/743156 to your computer and use it in GitHub Desktop.
def self.search(term, per_page, page, subdomain_id)
conditions, conditional = Array.new, Array.new
# made prettier by an array and a join
self.column_names.collect { |c| c != "subdomain" ? conditional << "#{c} LIKE '%%%s%%'" : "" }
# wrap around SQL logic for making sure search only returns results pertinent to domain
conditions.push("(" + conditional.join(' OR ') + ") AND subdomain = '#{subdomain_id}'")
# push the search term into the conditions -- replacing the %%s%%
self.column_names.size.times { conditions.push(term) }
#clean the sql
conds = sanitize_sql_array(conditions)
#paginate the result
self.paginate(:all, :conditions => conds, :per_page => per_page, :page => page)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment