Sha256: ca1e5b2782f36c31b96d7f073b912eb8d55e9dabf918b9ae479ecaa4be95cba5

Contents?: true

Size: 1.54 KB

Versions: 62

Compression:

Stored size: 1.54 KB

Contents

# Query class for models to match the API of {Search::Query}.
# Used in exporting models that aren't put into Elasticsearch.
#
module Workarea
  class AdminSearchQueryWrapper
    include Search::Pagination
    include GlobalID::Identification

    attr_reader :params

    def self.find(id)
      new(JSON.parse(id))
    end

    def initialize(params = {})
      @params = params.with_indifferent_access
      @params[:model_type] = @params[:model_type].to_s # ensures ID serialization works for it
    end

    def id
      params.to_json
    end

    def klass
      params[:model_type].constantize
    end

    def results
      criteria = klass_criteria
      criteria = criteria.where(filters) if filters.present?
      criteria.order_by(sort).page(page).per(per_page)
    end

    def scroll(options = {}, &block) # to match Search::Query method arguments
      criteria = results
      criteria.total_pages.times do |page|
        yield criteria.page(page + 1).per(per_page).to_a
      end
    end

    def klass_criteria
      return klass.all unless params[:q].present? && klass.respond_to?(:search)
      klass.search(params[:q])
    end

    def filters
      params[:query_params]
    end

    def total
      results.count
    end

    def sort
      if klass.respond_to?(:sorts)
        result = params[:sort].presence || klass.sorts.first.to_s

        klass.sorts.map do |sortable|
          if sortable.to_s == result.to_s
            return [sortable.field, sortable.direction]
          end
        end
      end

      [:created_at, :desc]
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.26 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.45 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.25 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.23 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.44 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.22 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.43 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.21 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.42 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.20 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.41 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.19 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.40 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.18 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.39 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.17 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.38 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.16 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.37 app/queries/workarea/admin_search_query_wrapper.rb