Class: Westfield::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/westfield_paginator/config.rb,
lib/westfield_paginator/paginator.rb

Defined Under Namespace

Classes: Configuration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, page: nil, per_page: nil, max_per_page: nil) ⇒ Paginator

Returns a new instance of Paginator.



6
7
8
9
10
11
12
13
14
# File 'lib/westfield_paginator/paginator.rb', line 6

def initialize(records, page: nil, per_page: nil, max_per_page: nil)
  @page_info = Westfield::PageInfo.new(page: page, per_page: per_page, max_per_page: max_per_page)

  if records.is_a? Array
    @records = Kaminari.paginate_array(records)
  else
    @records  = records
  end
end

Class Method Details

.configObject

Global settings for Westfield::Paginator



15
16
17
# File 'lib/westfield_paginator/config.rb', line 15

def self.config
  @config
end

.configure {|@config ||= Westfield::Paginator::Configuration.new| ... } ⇒ Object

Configures global settings for pagination

Westfield::Paginator.configure do |config|
  config.default_per_page = 10
end


10
11
12
# File 'lib/westfield_paginator/config.rb', line 10

def self.configure(&block)
  yield @config ||= Westfield::Paginator::Configuration.new
end

Instance Method Details

#current_pageObject



24
25
26
# File 'lib/westfield_paginator/paginator.rb', line 24

def current_page
  paginated_records.current_page
end

#metaObject



20
21
22
# File 'lib/westfield_paginator/paginator.rb', line 20

def meta
  PaginatorMeta.new(self).as_json
end

#paginated_recordsObject



16
17
18
# File 'lib/westfield_paginator/paginator.rb', line 16

def paginated_records
  @paginated ||= @records.page(page_info.page).per(page_info.per_page)
end

#per_pageObject



28
29
30
# File 'lib/westfield_paginator/paginator.rb', line 28

def per_page
  page_info.per_page
end

#total_countObject



36
37
38
# File 'lib/westfield_paginator/paginator.rb', line 36

def total_count
  paginated_records.total_count
end

#total_pagesObject



32
33
34
# File 'lib/westfield_paginator/paginator.rb', line 32

def total_pages
  paginated_records.total_pages
end