Class: Queryko::Filters::Paginate

Inherits:
Base
  • Object
show all
Defined in:
lib/queryko/filters/paginate.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#as, #column_name, #feature, #field, #query_object, #table_name

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initialize(options = {}, feature) ⇒ Paginate

Returns a new instance of Paginate.



6
7
8
9
10
11
# File 'lib/queryko/filters/paginate.rb', line 6

def initialize(options = {}, feature)
  @upper_limit = options.fetch(:upper) { 100 }
  @lower_limit = options.fetch(:lower) { 10 }

  super(options, feature)
end

Instance Attribute Details

#default_limitObject (readonly)

Returns the value of attribute default_limit.



4
5
6
# File 'lib/queryko/filters/paginate.rb', line 4

def default_limit
  @default_limit
end

#lower_limitObject (readonly)

Returns the value of attribute lower_limit.



4
5
6
# File 'lib/queryko/filters/paginate.rb', line 4

def lower_limit
  @lower_limit
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/queryko/filters/paginate.rb', line 4

def params
  @params
end

#upper_limitObject (readonly)

Returns the value of attribute upper_limit.



4
5
6
# File 'lib/queryko/filters/paginate.rb', line 4

def upper_limit
  @upper_limit
end

Instance Method Details

#get_limitObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/queryko/filters/paginate.rb', line 35

def get_limit
   lim = params[:limit].to_i
   if lower_limit > lim
     lower_limit
   elsif lim > upper_limit
     upper_limit
   else
     lim
   end
end

#limitObject



31
32
33
# File 'lib/queryko/filters/paginate.rb', line 31

def limit
  get_limit
end

#pageObject



27
28
29
# File 'lib/queryko/filters/paginate.rb', line 27

def page
  params[:page] || 1
end

#param_key_formatObject



46
47
48
# File 'lib/queryko/filters/paginate.rb', line 46

def param_key_format
  "paginate"
end

#perform(collection, paginate, query_object) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/queryko/filters/paginate.rb', line 13

def perform(collection, paginate, query_object)
  if paginate
    @params = query_object.params

    if defined?(WillPaginate)
      collection.paginate(page: page, per_page: limit)
    elsif defined?(Kaminari)
      collection.page(page).per(limit)
    else
      raise 'Only kaminari and wil_paginate are supported'
    end
  end
end