Sha256: bad30cf77565721c6a84642fcd790585af117da7b9aa36948436bc0ce0511d03

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Macros
  class Search
    class Query < Macros::Base
      # @return [Macros::Search::Results] step macro instance
      #
      # @example searchable is optional
      #   Macros::Search::Query(searchable: Admin)
      def initialize(searchable:)
        @searchable = searchable
      end

      # @param ctx [Trailblazer::Skill] tbl context hash
      #
      # The search params are passed in ctx[:params] and look like this:
      # `{q: 'the query', page: 2}`
      #
      # The orders is passed in ctx[:order] and looks like this:
      # `{created_at: :desc}`
      def call(ctx, params:, order: nil, **)
        return false unless @searchable

        ctx[:searchable] = @searchable
        ransack_search_result = @searchable.ransack params[:q]
        ctx[:query] = ransack_search_result

        temp_search_results = ransack_search_result.result
        search_results = order ? temp_search_results.order(order) : temp_search_results
        ctx[:search_results] = search_results.page params[:page]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ff-tbl-macros-1.0.2 lib/macros/search/query.rb
ff-tbl-macros-1.0.1 lib/macros/search/query.rb
ff-tbl-macros-1.0.0 lib/macros/search/query.rb
ff-tbl-macros-0.1.8 lib/macros/search/query.rb