Module: SearchFlip::Paginatable

Included in:
Aggregation, Criteria
Defined in:
lib/search_flip/paginatable.rb

Overview

The SearchFlip::Paginatable mixin provides chainable methods to allow paginating the search results

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
# File 'lib/search_flip/paginatable.rb', line 6

def self.included(base)
  base.class_eval do
    attr_accessor :offset_value, :limit_value
  end
end

Instance Method Details

#limit(value) ⇒ SearchFlip::Criteria

Sets the request limit, ie Elasticsearch’s size parameter that is used to restrict the results that get returned.

Examples:

CommentIndex.limit(100)

Parameters:

  • value (Fixnum)

    The limit value, ie the max number of results that should be returned

Returns:



50
51
52
53
54
# File 'lib/search_flip/paginatable.rb', line 50

def limit(value)
  fresh.tap do |criteria|
    criteria.limit_value = value.to_i
  end
end

#limit_value_with_defaultFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the limit value or, if not yet set, the default limit value (30).

Returns:

  • (Fixnum)

    The limit value



62
63
64
# File 'lib/search_flip/paginatable.rb', line 62

def limit_value_with_default
  (limit_value || 30).to_i
end

#offset(value) ⇒ SearchFlip::Criteria

Sets the request offset, ie SearchFlip’s from parameter that is used to skip results in the result set from being returned.

Examples:

CommentIndex.offset(100)

Parameters:

  • value (Fixnum)

    The offset value, ie the number of results that are skipped in the result set

Returns:



23
24
25
26
27
# File 'lib/search_flip/paginatable.rb', line 23

def offset(value)
  fresh.tap do |criteria|
    criteria.offset_value = value.to_i
  end
end

#offset_value_with_defaultFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the offset value or, if not yet set, the default limit value (0).

Returns:

  • (Fixnum)

    The offset value



35
36
37
# File 'lib/search_flip/paginatable.rb', line 35

def offset_value_with_default
  (offset_value || 0).to_i
end

#page(value) ⇒ Object



85
86
87
# File 'lib/search_flip/paginatable.rb', line 85

def page(value)
  paginate(page: value, per_page: limit_value_with_default)
end

#paginate(page: 1, per_page: 30) ⇒ SearchFlip::Criteria

Sets pagination parameters for the criteria by using offset and limit, ie Elasticsearch’s from and size parameters.

Examples:

CommentIndex.paginate(page: 3)
CommentIndex.paginate(page: 5, per_page: 60)

Parameters:

  • page (#to_i) (defaults to: 1)

    The current page

  • per_page (#to_i) (defaults to: 30)

    The number of results per page

Returns:



78
79
80
81
82
83
# File 'lib/search_flip/paginatable.rb', line 78

def paginate(page: 1, per_page: 30)
  page = [page.to_i, 1].max
  per_page = per_page.to_i

  offset((page - 1) * per_page).limit(per_page)
end

#per(value) ⇒ Object



89
90
91
# File 'lib/search_flip/paginatable.rb', line 89

def per(value)
  paginate(page: 1 + (offset_value_with_default / limit_value_with_default), per_page: value)
end