Sha256: 411a84ab5bb1fe8d43f96c0824860b3f304a5d14225f4e36883830a9c5735bf2
Contents?: true
Size: 853 Bytes
Versions: 15
Compression:
Stored size: 853 Bytes
Contents
module BMC::Search extend ActiveSupport::Concern class_methods do def default_search_fields columns .select { |column| column.type.in?([:string, :text]) } .map { |column| "#{table_name}.#{column.name}" } end # def default_search_fields def search(q, *fields) words = q.to_s.parameterize.split("-") fields = default_search_fields if fields.empty? return all if words.empty? sql_query = words.map.with_index { |_word, index| fields.map { |field| "(UNACCENT(CAST(#{field} AS TEXT)) ILIKE :w#{index})" }.join(" OR ") }.map { |e| "(#{e})" }.join(" AND ") sql_params = words.map.with_index { |word, index| ["w#{index}".to_sym, "%#{word}%"] }.to_h where(sql_query, sql_params) end # def search end # class_methods end # class BMC::Search
Version data entries
15 entries across 15 versions & 1 rubygems