|
1 | | -module Elasticsearch |
2 | | - module Model |
3 | | - module Response |
4 | | - |
5 | | - # Pagination for search results/records |
6 | | - # |
7 | | - module Pagination |
8 | | - # Allow models to be paginated with the "kaminari" gem [https://github.com/amatsuda/kaminari] |
9 | | - # |
10 | | - module Kaminari |
11 | | - def self.included(base) |
12 | | - # Include the Kaminari configuration and paging method in response |
13 | | - # |
14 | | - base.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods |
15 | | - base.__send__ :include, ::Kaminari::PageScopeMethods |
16 | | - |
17 | | - # Include the Kaminari paging methods in results and records |
18 | | - # |
19 | | - Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods |
20 | | - Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::PageScopeMethods |
21 | | - Elasticsearch::Model::Response::Records.__send__ :include, ::Kaminari::PageScopeMethods |
22 | | - |
23 | | - Elasticsearch::Model::Response::Results.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response |
24 | | - Elasticsearch::Model::Response::Records.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response |
25 | | - |
26 | | - base.class_eval <<-RUBY, __FILE__, __LINE__ + 1 |
27 | | - # Define the `page` Kaminari method |
28 | | - # |
29 | | - def #{::Kaminari.config.page_method_name}(num=nil) |
30 | | - @results = nil |
31 | | - @records = nil |
32 | | - @response = nil |
33 | | - @page = [num.to_i, 1].max |
34 | | - @per_page ||= __default_per_page |
35 | | -
|
36 | | - self.search.definition.update size: @per_page, |
37 | | - from: @per_page * (@page - 1) |
38 | | -
|
39 | | - self |
40 | | - end |
41 | | - RUBY |
42 | | - end |
43 | | - |
44 | | - # Returns the current "limit" (`size`) value |
45 | | - # |
46 | | - def limit_value |
47 | | - case |
48 | | - when search.definition[:size] |
49 | | - search.definition[:size] |
50 | | - else |
51 | | - __default_per_page |
52 | | - end |
53 | | - end |
54 | | - |
55 | | - # Returns the current "offset" (`from`) value |
56 | | - # |
57 | | - def offset_value |
58 | | - case |
59 | | - when search.definition[:from] |
60 | | - search.definition[:from] |
61 | | - else |
62 | | - 0 |
63 | | - end |
64 | | - end |
65 | | - |
66 | | - # Set the "limit" (`size`) value |
67 | | - # |
68 | | - def limit(value) |
69 | | - return self if value.to_i <= 0 |
70 | | - @results = nil |
71 | | - @records = nil |
72 | | - @response = nil |
73 | | - @per_page = value.to_i |
74 | | - |
75 | | - search.definition.update :size => @per_page |
76 | | - search.definition.update :from => @per_page * (@page - 1) if @page |
77 | | - self |
78 | | - end |
79 | | - |
80 | | - # Set the "offset" (`from`) value |
81 | | - # |
82 | | - def offset(value) |
83 | | - return self if value.to_i < 0 |
84 | | - @results = nil |
85 | | - @records = nil |
86 | | - @response = nil |
87 | | - @page = nil |
88 | | - search.definition.update :from => value.to_i |
89 | | - self |
90 | | - end |
91 | | - |
92 | | - # Returns the total number of results |
93 | | - # |
94 | | - def total_count |
95 | | - results.total |
96 | | - end |
97 | | - |
98 | | - # Returns the models's `per_page` value or the default |
99 | | - # |
100 | | - # @api private |
101 | | - # |
102 | | - def __default_per_page |
103 | | - klass.respond_to?(:default_per_page) && klass.default_per_page || ::Kaminari.config.default_per_page |
104 | | - end |
105 | | - end |
106 | | - |
107 | | - # Allow models to be paginated with the "will_paginate" gem [https://github.com/mislav/will_paginate] |
108 | | - # |
109 | | - module WillPaginate |
110 | | - def self.included(base) |
111 | | - base.__send__ :include, ::WillPaginate::CollectionMethods |
112 | | - |
113 | | - # Include the paging methods in results and records |
114 | | - # |
115 | | - methods = [:current_page, :offset, :length, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?] |
116 | | - Elasticsearch::Model::Response::Results.__send__ :delegate, *methods, to: :response |
117 | | - Elasticsearch::Model::Response::Records.__send__ :delegate, *methods, to: :response |
118 | | - end |
119 | | - |
120 | | - def offset |
121 | | - (current_page - 1) * per_page |
122 | | - end |
123 | | - |
124 | | - def length |
125 | | - search.definition[:size] |
126 | | - end |
127 | | - |
128 | | - # Main pagination method |
129 | | - # |
130 | | - # @example |
131 | | - # |
132 | | - # Article.search('foo').paginate(page: 1, per_page: 30) |
133 | | - # |
134 | | - def paginate(options) |
135 | | - param_name = options[:param_name] || :page |
136 | | - page = [options[param_name].to_i, 1].max |
137 | | - per_page = (options[:per_page] || __default_per_page).to_i |
138 | | - |
139 | | - search.definition.update size: per_page, |
140 | | - from: (page - 1) * per_page |
141 | | - self |
142 | | - end |
143 | | - |
144 | | - # Return the current page |
145 | | - # |
146 | | - def current_page |
147 | | - search.definition[:from] / per_page + 1 if search.definition[:from] && per_page |
148 | | - end |
149 | | - |
150 | | - # Pagination method |
151 | | - # |
152 | | - # @example |
153 | | - # |
154 | | - # Article.search('foo').page(2) |
155 | | - # |
156 | | - def page(num) |
157 | | - paginate(page: num, per_page: per_page) # shorthand |
158 | | - end |
159 | | - |
160 | | - # Return or set the "size" value |
161 | | - # |
162 | | - # @example |
163 | | - # |
164 | | - # Article.search('foo').per_page(15).page(2) |
165 | | - # |
166 | | - def per_page(num = nil) |
167 | | - if num.nil? |
168 | | - search.definition[:size] |
169 | | - else |
170 | | - paginate(page: current_page, per_page: num) # shorthand |
171 | | - end |
172 | | - end |
173 | | - |
174 | | - # Returns the total number of results |
175 | | - # |
176 | | - def total_entries |
177 | | - results.total |
178 | | - end |
179 | | - |
180 | | - # Returns the models's `per_page` value or the default |
181 | | - # |
182 | | - # @api private |
183 | | - # |
184 | | - def __default_per_page |
185 | | - klass.respond_to?(:per_page) && klass.per_page || ::WillPaginate.per_page |
186 | | - end |
187 | | - end |
188 | | - end |
189 | | - |
190 | | - end |
191 | | - end |
192 | | -end |
| 1 | +require 'elasticsearch/model/response/pagination/kaminari' |
| 2 | +require 'elasticsearch/model/response/pagination/will_paginate' |
0 commit comments