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.4.27 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.4 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.26 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.3 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.25 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.2 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.24 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.1 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.23 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.22 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.0 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.21 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.5.0.beta.1 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.20 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.19 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.18 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.17 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.16 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.15 app/queries/workarea/admin_search_query_wrapper.rb
workarea-core-3.4.14 app/queries/workarea/admin_search_query_wrapper.rb