Class: Locomotive::Steam::Models::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/models/pager.rb

Constant Summary collapse

DEFAULT_PER_PAGE =
10.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, page, per_page) ⇒ Pager

Returns a new instance of Pager.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/locomotive/steam/models/pager.rb', line 10

def initialize(source, page, per_page)
  @current_page, @per_page = page || 1, per_page || DEFAULT_PER_PAGE

  @current_page   = 1 if @current_page < 1
  @total_entries  = source.count
  @total_pages    = (@total_entries.to_f / @per_page).ceil

  index   = (@current_page - 1) * @per_page
  offset  = (index + @per_page - 1) >= @total_entries ? @total_entries : (index + @per_page - 1)

  @collection = paginate(source, index, offset)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



8
9
10
# File 'lib/locomotive/steam/models/pager.rb', line 8

def collection
  @collection
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



8
9
10
# File 'lib/locomotive/steam/models/pager.rb', line 8

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



8
9
10
# File 'lib/locomotive/steam/models/pager.rb', line 8

def per_page
  @per_page
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



8
9
10
# File 'lib/locomotive/steam/models/pager.rb', line 8

def total_entries
  @total_entries
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



8
9
10
# File 'lib/locomotive/steam/models/pager.rb', line 8

def total_pages
  @total_pages
end

Instance Method Details

#next_pageObject



27
28
29
# File 'lib/locomotive/steam/models/pager.rb', line 27

def next_page
  current_page >= total_pages ? nil : current_page + 1
end

#previous_pageObject



23
24
25
# File 'lib/locomotive/steam/models/pager.rb', line 23

def previous_page
  current_page <= 1 ? nil : current_page - 1
end

#to_liquidObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/locomotive/steam/models/pager.rb', line 31

def to_liquid
  {
    collection:       collection,
    current_page:     current_page,
    per_page:         per_page,
    previous_page:    previous_page,
    next_page:        next_page,
    total_entries:    total_entries,
    total_pages:      total_pages
  }
end