Module: DynamicSearch::ClassMethods
- Defined in:
- lib/dynamic_search.rb
Overview
This module provides the parent search method which will be available to any model which includes DynamicSearch ‘include DynamicSearch`
Instance Method Summary collapse
Instance Method Details
#search(search, columns = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dynamic_search.rb', line 9 def search(search, columns = nil) ## # The search method will search all columns except foreign keys, id, # and timestamps. The columns searched can be overwritten in an individual model # by using the following syntax: # # def search(search) # super(search, ["array", "of", "columns"]) # end if search.present? columns ||= self.column_names.reject{|e| e.end_with?("_id")} - ["id", "created_at", "updated_at"] params = {} sql = [] search.split(' ').each_with_index do |s, i| params["nth#{i}".to_sym] = "%#{sanitize_sql(s)}%" statements = [] for column in columns statements << "UPPER(CAST(#{column} AS text)) LIKE UPPER(:nth#{i})" end sql << "(#{statements.join(" OR ")})" end self.where(sql.join(" AND "), params) else self.all end end |