Class: Paginate::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/paginate/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, options = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/paginate/base.rb', line 6

def initialize(scope, options = {})
  @scope = scope

  if options.kind_of?(Hash)
    @options = options
  else
    @options = {page: options.to_i}
  end

  @options.reverse_merge!(Paginate.configuration.to_hash)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/paginate/base.rb', line 3

def options
  @options
end

#scopeObject

Returns the value of attribute scope.



4
5
6
# File 'lib/paginate/base.rb', line 4

def scope
  @scope
end

Instance Method Details

#collection_sizeObject



18
19
20
# File 'lib/paginate/base.rb', line 18

def collection_size
  @collection_size ||= options[:collection].size
end

#limitObject



38
39
40
# File 'lib/paginate/base.rb', line 38

def limit
  [options[:size], 10].compact.first.to_i + 1
end

#next_page?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/paginate/base.rb', line 22

def next_page?
  collection_size > options[:size]
end

#offsetObject



34
35
36
# File 'lib/paginate/base.rb', line 34

def offset
  (page - 1) * (limit - 1)
end

#pageObject



30
31
32
# File 'lib/paginate/base.rb', line 30

def page
  [1, options.fetch(:page, 1).to_i].max
end

#previous_page?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/paginate/base.rb', line 26

def previous_page?
  page > 1
end

#to_optionsObject



42
43
44
# File 'lib/paginate/base.rb', line 42

def to_options
  {limit: limit, offset: offset}
end

#to_scopeObject



46
47
48
49
50
# File 'lib/paginate/base.rb', line 46

def to_scope
  scope
    .limit(limit)
    .offset(offset)
end