Class: ElasticSearchable::Paginator

Inherits:
Array
  • Object
show all
Defined in:
lib/elastic_searchable/paginator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handlerObject



5
6
7
# File 'lib/elastic_searchable/paginator.rb', line 5

def handler
  @handler ||= ElasticSearchable::Pagination::WillPaginate
end

.handler=(handler) ⇒ Object



9
10
11
# File 'lib/elastic_searchable/paginator.rb', line 9

def handler=(handler)
  @handler = handler
end

Instance Method Details

#replace(array) ⇒ Object

This is a magic wrapper for the original Array#replace method. It serves for populating the paginated collection after initialization.

Why magic? Because it tries to guess the total number of entries judging by the size of given array. If it is shorter than per_page limit, then we know we’re on the last page. This trick is very useful for avoiding unnecessary hits to the database to do the counting after we fetched the data for the current page.

However, after using replace you should always test the value of total_entries and set it to a proper value if it’s nil. See the example in create.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/elastic_searchable/paginator.rb', line 26

def replace(array)
  result = super

  # The collection is shorter then page limit? Rejoice, because
  # then we know that we are on the last page!
  if total_entries.nil? and length < per_page and (current_page == 1 or length > 0)
    self.total_entries = offset + length
  end

  result
end