module Searchable
  extend ActiveSupport::Concern

  included do
    include PgSearch
  end

  class_methods do
    def searchable_on(fields)
      search_opts = {
        against: fields,
        associated_against: {
          notes: [:content],
          tags: [:name],
          internal_tags: [:name]
        },
        using: {
          tsearch: { any_word: true, prefix: true },
          trigram: {},
          dmetaphone: { any_word: true },
        }
      }

      send(:pg_search_scope, :search, search_opts)
    end
  end
end
