Class: Kaminari::PaginatableArray

Inherits:
Array
  • Object
show all
Includes:
ConfigurationMethods::ClassMethods
Defined in:
lib/kaminari/models/array_extension.rb

Overview

Kind of Array that can paginate

Constant Summary collapse

ENTRY =
'entry'.freeze

Instance Method Summary collapse

Methods included from ConfigurationMethods::ClassMethods

#default_per_page, #max_pages, #max_pages_per, #max_paginates_per, #max_per_page, #paginates_per

Constructor Details

#initialize(original_array = [], limit: nil, offset: nil, total_count: nil, padding: nil) ⇒ PaginatableArray

Options

  • :limit - limit

  • :offset - offset

  • :total_count - total_count

  • :padding - padding



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kaminari/models/array_extension.rb', line 18

def initialize(original_array = [], limit: nil, offset: nil, total_count: nil, padding: nil)
  @_original_array, @_limit_value, @_offset_value, @_total_count, @_padding = original_array, (limit || default_per_page).to_i, offset.to_i, total_count, padding.to_i

  if limit && offset
    extend Kaminari::PageScopeMethods
  end

  if @_total_count && (@_total_count <= original_array.count)
    original_array = original_array.first(@_total_count)[@_offset_value, @_limit_value]
  end

  unless @_total_count
    original_array = original_array[@_offset_value, @_limit_value]
  end

  super(original_array || [])
end

Instance Method Details

#entry_name(options = {}) ⇒ Object

Used for page_entry_info



37
38
39
# File 'lib/kaminari/models/array_extension.rb', line 37

def entry_name(options = {})
  I18n.t('helpers.page_entries_info.entry', **options.reverse_merge(default: ENTRY.pluralize(options[:count])))
end

#limit(num) ⇒ Object

returns another chunk of the original array



49
50
51
# File 'lib/kaminari/models/array_extension.rb', line 49

def limit(num)
  self.class.new @_original_array, limit: num, offset: @_offset_value, total_count: @_total_count, padding: @_padding
end

#offset(num) ⇒ Object

returns another chunk of the original array



59
60
61
# File 'lib/kaminari/models/array_extension.rb', line 59

def offset(num)
  self.class.new @_original_array, limit: @_limit_value, offset: num, total_count: @_total_count, padding: @_padding
end

#total_countObject

total item numbers of the original array



54
55
56
# File 'lib/kaminari/models/array_extension.rb', line 54

def total_count
  @_total_count || @_original_array.length
end