Module: Searchgasm::Search::Pagination

Included in:
Base
Defined in:
lib/searchgasm/search/pagination.rb

Overview

Searchgasm Pagination

Adds in pagination functionality to searchgasm

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/searchgasm/search/pagination.rb', line 7

def self.included(klass)
  klass.class_eval do
    alias_method_chain :limit=, :pagination
    alias_method_chain :offset=, :pagination
    alias_method :per_page, :limit
    alias_method :per_page=, :limit=
  end
end

Instance Method Details

#first_pageObject

Always returns 1, this is a convenience method



71
72
73
# File 'lib/searchgasm/search/pagination.rb', line 71

def first_page
  1
end

#first_page!Object

Changes the page to 1 and then runs the “all” search. What’s different about this method is that it does not raise an exception if you are on the first page. Unlike prev_page! and next_page! I don’t think an exception raised is warranted, because you are expecting the same results each time it is ran.



77
78
79
80
# File 'lib/searchgasm/search/pagination.rb', line 77

def first_page!
  self.page = first_page
  all
end

#last_pageObject

Always returns the page_count, this is a convenience method



109
110
111
# File 'lib/searchgasm/search/pagination.rb', line 109

def last_page
  page_count
end

#last_page!Object

Changes the page to the last page and runs the “all” search. What’s different about this method is that it does not raise an exception if you are on the last page. Unlike prev_page! and next_page! I don’t think an exception raised is warranted, because you are expecting the same results each time it is ran.



115
116
117
118
# File 'lib/searchgasm/search/pagination.rb', line 115

def last_page!
  self.page = last_page
  all
end

#limit_with_pagination=(value) ⇒ Object

:nodoc:



16
17
18
19
20
21
22
23
24
25
# File 'lib/searchgasm/search/pagination.rb', line 16

def limit_with_pagination=(value) # :nodoc:
  r_value = self.limit_without_pagination = value
  @page_count = nil
  if @set_page
    self.page = (@queued_page || @page) # retry setting page
  else
    @page = nil # the memoized page is invalid, so reset it
  end
  r_value
end

#next_pageObject

Change the page to page + 1



96
97
98
# File 'lib/searchgasm/search/pagination.rb', line 96

def next_page
  self.page + 1
end

#next_page!Object

Changes the page to page + 1 and calls the “all” method. Be careful with this method because if you are on the last page an exception is raised telling you that you are on the last page. I thought about just running the lat page search again, but that seems confusing and unexpected.



102
103
104
105
106
# File 'lib/searchgasm/search/pagination.rb', line 102

def next_page!
  raise("You are on the last page") if page == last_page
  self.page = next_page
  all
end

#offset_with_pagination=(value) ⇒ Object

:nodoc



27
28
29
30
31
# File 'lib/searchgasm/search/pagination.rb', line 27

def offset_with_pagination=(value) #:nodoc
  r_value = self.offset_without_pagination = value
  @set_page = @queued_page = @page = nil
  r_value
end

#pageObject Also known as: current_page

The current page that the search is on



34
35
36
# File 'lib/searchgasm/search/pagination.rb', line 34

def page
  @page ||= (offset.blank? || limit.blank?) ? 1 : (offset.to_f / limit).floor + 1
end

#page=(value) ⇒ Object

Lets you change the page for the next search



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/searchgasm/search/pagination.rb', line 40

def page=(value)
  @set_page = true
  
  if value.blank?
    value = nil
    @page = value
    return @offset = @page
  end
  
  v = value.to_i
  
  if limit.blank?
    @queued_page = v
    @page = 1
    @offset = nil
  else
    @queued_page = nil
    @page = v
    v -= 1 unless v == 0
    @offset = v * limit
  end
  value
end

#page_countObject Also known as: page_total

The total number of pages in your next search



65
66
67
# File 'lib/searchgasm/search/pagination.rb', line 65

def page_count
  @page_count ||= (per_page.blank? || per_page <= 0) ? 1 : (count / per_page.to_f).ceil
end

#prev_pageObject

Changes the page to the page - 1



83
84
85
# File 'lib/searchgasm/search/pagination.rb', line 83

def prev_page
  self.page - 1
end

#prev_page!Object

Changes the page to page - 1 and runs the “all” search. Be careful with this method because if you are on the first page an exception is raised telling you that you are on the first page. I thought about just running the first page search again, but that seems confusing and unexpected.



89
90
91
92
93
# File 'lib/searchgasm/search/pagination.rb', line 89

def prev_page!
  raise("You are on the first page") if page == first_page
  self.page = prev_page
  all
end