waistband 0.12.2 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/Gemfile +0 -1
- data/README.md +1 -1
- data/lib/waistband/search_results.rb +18 -6
- data/lib/waistband/version.rb +1 -1
- data/spec/lib/index_spec.rb +12 -0
- data/spec/lib/search_results_spec.rb +4 -26
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTJiYTBiZGZiN2FmYWEzZTdlZGFhYmU5NjA3MjAyMDgwZTM2MmVhZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2I2MDAzZTEwNGViZGQ0NDE5MWY1NzAyOGE4YTVmMmEwNzk0YTVjYw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YTU3ZWVjMGMzNDc2NWQzYTRhYmRjYmE3ODgzODk3ZDkzZDk0ZWQ5ZTczNmMx
|
10
|
+
MjA3M2ZlOTU1NGQ4NTI0OTVlZTc1ZDg0MTEyYTlkMjBlMDE3ODgyOGE4YmUw
|
11
|
+
ZjU4OTUyY2Q3YTJmY2RjNmFhZTFhM2ZlMzNkNTkzODUyZDg1OGM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NGMwMjdiZDUwYzE4ZjQxM2RlZjA5YzZlNzQxNWZlYWJkYzRhN2RmYzM4ZTlh
|
14
|
+
YjRkYTJiYmQ5Njg1ZmMyM2I5ODkxOWVlNWViMzliY2Y2YjVlZWQ0OTgwZTQ1
|
15
|
+
ZWVjYzA0NjlkZjFkODZiNDA0YTRhMmJlNDk1NzVlMDY5MTdkNjQ=
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -166,7 +166,7 @@ results.hits # => returns a search results object
|
|
166
166
|
results.total_hits # => 28481
|
167
167
|
```
|
168
168
|
|
169
|
-
For paginating the results, you can use the `#paginated_results` method, which
|
169
|
+
For paginating the results, you can use the `#paginated_results` method, which will provide an array object compatible with the kaminari gem. If you use another gem, you can just override the method, etc.
|
170
170
|
|
171
171
|
For more information and extra methods, take a peek into the class docs.
|
172
172
|
|
@@ -3,11 +3,25 @@ require 'waistband/result'
|
|
3
3
|
module Waistband
|
4
4
|
class SearchResults
|
5
5
|
|
6
|
+
class PaginatedArray < Array
|
7
|
+
|
8
|
+
attr_reader :total_count, :per_page, :num_pages, :total_pages, :current_page
|
9
|
+
|
10
|
+
def initialize(arr, options)
|
11
|
+
@current_page = (options[:current_page] || 1).to_i
|
12
|
+
@total_count = (options[:total_count] || arr.length).to_i
|
13
|
+
@per_page = (options[:per_page] || ::Waistband::SearchResults::DEFAULT_PAGE_SIZE).to_i
|
14
|
+
@num_pages = @total_pages = (options[:num_pages] || (@total_count.to_f / @per_page).ceil)
|
15
|
+
super(arr)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
6
20
|
DEFAULT_PAGE_SIZE = 20
|
7
21
|
|
8
22
|
def initialize(search_hash, options = {})
|
9
|
-
@page = options[:page] || 1
|
10
|
-
@page_size = options[:page_size] || DEFAULT_PAGE_SIZE
|
23
|
+
@page = (options[:page] || 1).to_i
|
24
|
+
@page_size = (options[:page_size] || DEFAULT_PAGE_SIZE).to_i
|
11
25
|
@search_hash = search_hash
|
12
26
|
end
|
13
27
|
|
@@ -25,13 +39,11 @@ module Waistband
|
|
25
39
|
end
|
26
40
|
|
27
41
|
def paginated_hits
|
28
|
-
|
29
|
-
Kaminari.paginate_array(hits, total_count: total_results).page(@page).per(@page_size)
|
42
|
+
::Waistband::SearchResults::PaginatedArray.new(hits, current_page: @page, page_size: @page_size, total_count: total_results)
|
30
43
|
end
|
31
44
|
|
32
45
|
def paginated_results
|
33
|
-
|
34
|
-
Kaminari.paginate_array(results, total_count: total_results).page(@page).per(@page_size)
|
46
|
+
::Waistband::SearchResults::PaginatedArray.new(results, current_page: @page, page_size: @page_size, total_count: total_results)
|
35
47
|
end
|
36
48
|
|
37
49
|
def total_results
|
data/lib/waistband/version.rb
CHANGED
data/spec/lib/index_spec.rb
CHANGED
@@ -183,6 +183,18 @@ describe Waistband::Index do
|
|
183
183
|
expect(query.hits.size).to eql 2
|
184
184
|
end
|
185
185
|
|
186
|
+
it 'returns results beyond page 1 (does not double scope)' do
|
187
|
+
query = index.search(page_size: 2, page: 1)
|
188
|
+
hits1 = query.hits
|
189
|
+
expect(hits1.size).to eql 2
|
190
|
+
|
191
|
+
query = index.search(page_size: 2, page: 2)
|
192
|
+
hits2 = query.hits
|
193
|
+
expect(hits2.size).to eql 2
|
194
|
+
|
195
|
+
expect(hits1 & hits2).to be_empty
|
196
|
+
end
|
197
|
+
|
186
198
|
end
|
187
199
|
|
188
200
|
end
|
@@ -77,48 +77,26 @@ describe ::Waistband::SearchResults do
|
|
77
77
|
|
78
78
|
describe '#paginated_hits' do
|
79
79
|
|
80
|
-
it "provides a paginated array
|
80
|
+
it "provides a paginated array compatible with a readonly Kaminari array" do
|
81
81
|
load 'active_support/concern.rb'
|
82
|
-
load 'kaminari/config.rb'
|
83
|
-
load 'kaminari/models/page_scope_methods.rb'
|
84
|
-
load 'kaminari/models/configuration_methods.rb'
|
85
|
-
load 'kaminari/models/array_extension.rb'
|
86
82
|
|
87
|
-
expect(results.paginated_hits).to
|
83
|
+
expect(results.paginated_hits).to be_a ::Waistband::SearchResults::PaginatedArray
|
88
84
|
expect(results.paginated_hits.total_pages).to eql 1
|
89
85
|
expect(results.paginated_hits.current_page).to eql 1
|
90
86
|
end
|
91
87
|
|
92
|
-
it "blows up when kaminari's not required" do
|
93
|
-
# if we've already required Kaminari from the test above, lets remove
|
94
|
-
Object.send(:remove_const, :Kaminari) if defined?(Kaminari)
|
95
|
-
|
96
|
-
expect { results.paginated_hits }.to raise_error RuntimeError, "Kaminari gem not found for pagination"
|
97
|
-
end
|
98
|
-
|
99
88
|
end
|
100
89
|
|
101
90
|
describe '#paginated_results' do
|
102
91
|
|
103
|
-
it "provides a paginated array
|
92
|
+
it "provides a paginated array compatible with a readonly Kaminari array" do
|
104
93
|
load 'active_support/concern.rb'
|
105
|
-
load 'kaminari/config.rb'
|
106
|
-
load 'kaminari/models/page_scope_methods.rb'
|
107
|
-
load 'kaminari/models/configuration_methods.rb'
|
108
|
-
load 'kaminari/models/array_extension.rb'
|
109
94
|
|
110
|
-
expect(results.paginated_results).to
|
95
|
+
expect(results.paginated_results).to be_a ::Waistband::SearchResults::PaginatedArray
|
111
96
|
expect(results.paginated_results.total_pages).to eql 1
|
112
97
|
expect(results.paginated_results.current_page).to eql 1
|
113
98
|
end
|
114
99
|
|
115
|
-
it "blows up when kaminari's not required" do
|
116
|
-
# if we've already required Kaminari from the test above, lets remove
|
117
|
-
Object.send(:remove_const, :Kaminari) if defined?(Kaminari)
|
118
|
-
|
119
|
-
expect { results.paginated_results }.to raise_error RuntimeError, "Kaminari gem not found for pagination"
|
120
|
-
end
|
121
|
-
|
122
100
|
end
|
123
101
|
|
124
102
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: waistband
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Jairala
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|