Sha256: f44a390433fb54653cde43a3285e99db288696dea352d84370f76e23f08149cf

Contents?: true

Size: 829 Bytes

Versions: 57

Compression:

Stored size: 829 Bytes

Contents

module Agilibox::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?

      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 Agilibox::Search

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
agilibox-1.9.17 app/models/concerns/agilibox/search.rb
agilibox-1.9.16 app/models/concerns/agilibox/search.rb
agilibox-1.9.15 app/models/concerns/agilibox/search.rb
agilibox-1.9.14 app/models/concerns/agilibox/search.rb
agilibox-1.9.13 app/models/concerns/agilibox/search.rb
agilibox-1.9.12 app/models/concerns/agilibox/search.rb
agilibox-1.9.11 app/models/concerns/agilibox/search.rb
agilibox-1.9.10 app/models/concerns/agilibox/search.rb
agilibox-1.9.9 app/models/concerns/agilibox/search.rb
agilibox-1.9.8 app/models/concerns/agilibox/search.rb
agilibox-1.9.7 app/models/concerns/agilibox/search.rb
agilibox-1.9.6 app/models/concerns/agilibox/search.rb
agilibox-1.9.5 app/models/concerns/agilibox/search.rb
agilibox-1.9.4 app/models/concerns/agilibox/search.rb
agilibox-1.9.3 app/models/concerns/agilibox/search.rb
agilibox-1.9.1 app/models/concerns/agilibox/search.rb
agilibox-1.9.0 app/models/concerns/agilibox/search.rb
agilibox-1.8.0 app/models/concerns/agilibox/search.rb
agilibox-1.7.4 app/models/concerns/agilibox/search.rb
agilibox-1.7.3 app/models/concerns/agilibox/search.rb