Sha256: 801a47bcd8c7fe136faa1babd9c6bccded8825c4d22ab890a015a7d386ebbc7e

Contents?: true

Size: 864 Bytes

Versions: 1

Compression:

Stored size: 864 Bytes

Contents

module SearchRedux
  class Postgres
    class << self
      attr_accessor :sanitized_query

      def compatible_search(options)
        query        = to_ts_query(options[:query])
        rank_query   = rank_sql(options[:rank], query)
        search_query = search_sql(options[:columns])

        ->(obj) { obj.where(Arel.sql(search_query), q: Arel.sql(query)).order(Arel.sql(rank_query)) }
      end

      def to_ts_query(query)
        query.split.map { |ts| "#{ts}:*" }.join(" & ")
      end

      def rank_sql(rank_column, query)
        "ts_rank(to_tsvector(#{rank_column}), to_tsquery('#{query}')) desc"
      end

      def search_sql(columns)
        template = ->(column_name) {
          return "to_tsvector('english', #{column_name}) @@ to_tsquery(:q)"
        }

        columns.map { |c| template.call(c) }.join(' OR ')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
search_redux-1.0.2 lib/search_redux/rdbms/postgres.rb