Class: RailsPaginate::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/rails_paginate/collection.rb

Overview

method modules

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array_or_relation, page = nil, per_page = nil) ⇒ Collection

initialize collection

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rails_paginate/collection.rb', line 7

def initialize(array_or_relation, page = nil, per_page = nil)
  # validate
  raise ArgumentError, "per_page is not valid" if not per_page.nil? and per_page <= 0 
  raise ArgumentError, "result is not an array or relation" unless array_or_relation.respond_to? :count

  # array_or_relation
  @array_or_relation = array_or_relation

  # save meta data
  @per_page          = per_page || relation_per_page || RailsPaginate.per_page

  # load page with result
  load_page(page) unless page.nil?
end

Instance Attribute Details

#array_or_relationObject (readonly)

Returns the value of attribute array_or_relation.



4
5
6
# File 'lib/rails_paginate/collection.rb', line 4

def array_or_relation
  @array_or_relation
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



4
5
6
# File 'lib/rails_paginate/collection.rb', line 4

def current_page
  @current_page
end

#pagesObject (readonly)

count of pages



59
60
61
# File 'lib/rails_paginate/collection.rb', line 59

def pages
  @pages
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



4
5
6
# File 'lib/rails_paginate/collection.rb', line 4

def per_page
  @per_page
end

Instance Method Details

#first_pageObject

first page



74
75
76
# File 'lib/rails_paginate/collection.rb', line 74

def first_page
  1
end

#first_page?Boolean

is current page the first page?

Returns:

  • (Boolean)


89
90
91
# File 'lib/rails_paginate/collection.rb', line 89

def first_page?
  current_page == first_page
end

#last_pageObject

last page



79
80
81
# File 'lib/rails_paginate/collection.rb', line 79

def last_page
  pages
end

#last_page?Boolean

is current page the last page?

Returns:

  • (Boolean)


84
85
86
# File 'lib/rails_paginate/collection.rb', line 84

def last_page?
  current_page == last_page
end

#load_page(page) ⇒ Object

switch page



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_paginate/collection.rb', line 28

def load_page(page)
  # jump to correct page
  page = page.to_i if page.is_a? String
  page = first_page unless page.is_a? Fixnum
  page = first_page unless page > 0
  page = last_page if page > pages

  # save page
  @current_page = page

  # load result
  load_result
end

#load_resultObject

load result from input array_or_relation to internal array



43
44
45
46
47
48
49
50
51
# File 'lib/rails_paginate/collection.rb', line 43

def load_result
  if array_or_relation.is_a? Array
    result = array_or_relation[offset..(offset + per_page - 1)]
  else
    result = array_or_relation.limit(per_page).offset(offset).all
  end

  self.replace result.nil? ? [] : result
end

#need_paginate?Boolean

need paginate

Returns:

  • (Boolean)


69
70
71
# File 'lib/rails_paginate/collection.rb', line 69

def need_paginate?
  total > per_page
end

#next_pageObject

next page



94
95
96
# File 'lib/rails_paginate/collection.rb', line 94

def next_page
  current_page < pages ? (current_page + 1) : nil
end

#offsetObject

get offset



64
65
66
# File 'lib/rails_paginate/collection.rb', line 64

def offset
  (current_page - 1) * per_page
end

#previous_pageObject

previous page



99
100
101
# File 'lib/rails_paginate/collection.rb', line 99

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#relation_per_pageObject

check if the relation has a per_page



23
24
25
# File 'lib/rails_paginate/collection.rb', line 23

def relation_per_page
  (array_or_relation.respond_to?(:per_page) ? array_or_relation.per_page : nil) unless array_or_relation.is_a? Array
end

#totalObject

total count of array or relation



54
55
56
# File 'lib/rails_paginate/collection.rb', line 54

def total
  @total ||= array_or_relation.count
end