Class: Greedo::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/greedo/paginator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, page:, per_page:, order: nil, order_by: nil) ⇒ Paginator

Returns a new instance of Paginator.



17
18
19
20
21
22
23
# File 'lib/greedo/paginator.rb', line 17

def initialize(scope, page:, per_page:, order: nil, order_by: nil)
  @scope = scope
  @page = page
  @per_page = per_page
  @order = order
  @order_by = order_by
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



6
7
8
# File 'lib/greedo/paginator.rb', line 6

def order
  @order
end

#order_byObject (readonly)

Returns the value of attribute order_by.



6
7
8
# File 'lib/greedo/paginator.rb', line 6

def order_by
  @order_by
end

#pageObject (readonly)

Returns the value of attribute page.



6
7
8
# File 'lib/greedo/paginator.rb', line 6

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



6
7
8
# File 'lib/greedo/paginator.rb', line 6

def per_page
  @per_page
end

#scopeObject (readonly)

Returns the value of attribute scope.



6
7
8
# File 'lib/greedo/paginator.rb', line 6

def scope
  @scope
end

Class Method Details

.build(scope, page: 1, per_page: 20, order_by: nil) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/greedo/paginator.rb', line 8

def self.build(scope,
               page: 1,
               per_page: 20,
               order_by: nil)
  return ArrayPaginator.new(scope, order_by.order, order_by.sort) if scope.is_a?(Array)
  Paginator.new(scope, page: page, per_page: per_page,
                order: order_by.order, order_by: order_by.sort)
end

Instance Method Details

#recordsObject



25
26
27
28
29
# File 'lib/greedo/paginator.rb', line 25

def records
  paginated = scope.paginate(page: page, per_page: per_page)
  order_by_fields = [order_by].flatten.map { |field| "#{field} #{order}" }.join(", ")
  order_by ? paginated.reorder(order_by_fields) : paginated
end

#show?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/greedo/paginator.rb', line 31

def show?
  scope.length > per_page
end