Sha256: 30558758425b3f454d22dfd6f85604a125f5ddde803d89f4ef2ba938024c6988

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true
module GoodJob
  # Shared methods for filtering Execution/Job records from the +good_jobs+ table.
  module Filterable
    extend ActiveSupport::Concern

    included do
      # Get records in display order with optional keyset pagination.
      # @!method display_all(after_scheduled_at: nil, after_id: nil)
      # @!scope class
      # @param after_scheduled_at [DateTime, String, nil]
      #   Display records scheduled after this time for keyset pagination
      # @param after_id [Numeric, String, nil]
      #   Display records after this ID for keyset pagination
      # @return [ActiveRecord::Relation]
      scope :display_all, (lambda do |after_scheduled_at: nil, after_id: nil|
        query = order(Arel.sql('COALESCE(scheduled_at, created_at) DESC, id DESC'))
        if after_scheduled_at.present? && after_id.present?
          query = query.where(Arel.sql('(COALESCE(scheduled_at, created_at), id) < (:after_scheduled_at, :after_id)'), after_scheduled_at: after_scheduled_at, after_id: after_id)
        elsif after_scheduled_at.present?
          query = query.where(Arel.sql('(COALESCE(scheduled_at, created_at)) < (:after_scheduled_at)'), after_scheduled_at: after_scheduled_at)
        end
        query
      end)

      # Search records by text query.
      # @!method search(query)
      # @!scope class
      # @param query [String]
      #   Search Query
      # @return [ActiveRecord::Relation]
      scope :search, (lambda do |query|
        query = query.to_s.strip
        next if query.blank?

        tsvector = "(to_tsvector('english', serialized_params) || to_tsvector('english', id::text) || to_tsvector('english', COALESCE(error, '')::text))"
        where("#{tsvector} @@ to_tsquery(?)", query)
          .order(sanitize_sql_for_order([Arel.sql("ts_rank(#{tsvector}, to_tsquery(?))"), query]) => 'DESC')
      end)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
good_job-2.6.1 lib/good_job/filterable.rb