Module: Anchor::Pagination

Included in:
TableComponent
Defined in:
app/helpers/anchor/pagination.rb

Constant Summary collapse

PER_PAGE_DEFAULT =
25
INITIAL_PAGE =
1
QUERY_PARAM =
"page".freeze

Instance Method Summary collapse

Instance Method Details

#pageObject



7
8
9
10
11
# File 'app/helpers/anchor/pagination.rb', line 7

def page
  Integer(request.query_parameters[QUERY_PARAM])
rescue TypeError
  INITIAL_PAGE
end

#paginated_dataObject



13
14
15
16
17
18
19
20
# File 'app/helpers/anchor/pagination.rb', line 13

def paginated_data
  case data
  when Array
    data[position, per_page]
  when ActiveRecord::Relation
    data.limit(per_page).offset(position)
  end
end

#per_pageObject



22
23
24
25
26
27
28
# File 'app/helpers/anchor/pagination.rb', line 22

def per_page
  if paginate.is_a?(Integer)
    paginate
  else
    PER_PAGE_DEFAULT
  end
end

#positionObject



30
31
32
# File 'app/helpers/anchor/pagination.rb', line 30

def position
  (page - 1) * per_page
end