Class: Macros::Search::Query

Inherits:
Base
  • Object
show all
Includes:
Pagy::Backend
Defined in:
lib/macros/search/query.rb

Instance Method Summary collapse

Methods inherited from Base

register

Constructor Details

#initialize(searchable:, paginate: true) ⇒ Macros::Search::Results

Returns step macro instance.

Examples:

searchable is optional, paginate is true by default

Macros::Search::Query(searchable: Admin)


14
15
16
17
# File 'lib/macros/search/query.rb', line 14

def initialize(searchable:, paginate: true)
  @searchable = searchable
  @paginate = paginate
end

Instance Method Details

#call(ctx, params:, order: nil) ⇒ Object

The search params are passed in ctx and look like this: ‘’the query’, page: 2‘

The orders is passed in ctx and looks like this: ‘:desc`

Parameters:

  • ctx (Trailblazer::Skill)

    tbl context hash



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/macros/search/query.rb', line 26

def call(ctx, params:, order: nil, **)
  return false unless @searchable

  ctx[:searchable] = @searchable

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

  temp_search_results = ransack_search.result

  if @paginate
    page = params[:page] || 1
    pagy, records = pagy(temp_search_results, page: page)
    ctx[:pages] = pagy
    temp_search_results = records
  end

  ctx[:search_results] = order ? temp_search_results.order(order) : temp_search_results
end