tire 0.1.3 → 0.1.4

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.
@@ -8,20 +8,27 @@ module Tire
8
8
  end
9
9
 
10
10
  def total_pages
11
- result = @total.to_f / (@options[:per_page] ? @options[:per_page].to_i : 10 )
12
- result < 1 ? 1 : result.ceil
11
+ ( @total.to_f / (@options[:per_page] || @options[:size] || 10 ).to_i ).ceil
13
12
  end
14
13
 
15
14
  def current_page
16
- @options[:page].to_i
15
+ if @options[:page]
16
+ @options[:page].to_i
17
+ else
18
+ (@options[:size].to_i + @options[:from].to_i) / @options[:size].to_i
19
+ end
17
20
  end
18
21
 
19
22
  def previous_page
20
- @options[:page].to_i - 1
23
+ current_page > 1 ? (current_page - 1) : nil
21
24
  end
22
25
 
23
26
  def next_page
24
- @options[:page].to_i + 1
27
+ current_page < total_pages ? (current_page + 1) : nil
28
+ end
29
+
30
+ def out_of_bounds?
31
+ current_page > total_pages
25
32
  end
26
33
 
27
34
  end
data/lib/tire/search.rb CHANGED
@@ -3,10 +3,10 @@ module Tire
3
3
 
4
4
  class Search
5
5
 
6
- attr_reader :indices, :url, :results, :response, :json, :query, :facets, :filters
6
+ attr_reader :indices, :url, :results, :response, :json, :query, :facets, :filters, :options
7
7
 
8
8
  def initialize(*indices, &block)
9
- @options = indices.pop if indices.last.is_a?(Hash)
9
+ @options = indices.last.is_a?(Hash) ? indices.pop : {}
10
10
  @indices = indices
11
11
  raise ArgumentError, 'Please pass index or indices to search' if @indices.empty?
12
12
  if @options
@@ -49,11 +49,13 @@ module Tire
49
49
 
50
50
  def from(value)
51
51
  @from = value
52
+ @options[:from] = value
52
53
  self
53
54
  end
54
55
 
55
56
  def size(value)
56
57
  @size = value
58
+ @options[:size] = value
57
59
  self
58
60
  end
59
61
 
data/lib/tire/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tire
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -96,7 +96,7 @@ module Tire
96
96
 
97
97
  assert_equal 2, results.total_pages
98
98
  assert_equal 1, results.current_page
99
- assert_equal 0, results.previous_page
99
+ assert_equal nil, results.previous_page
100
100
  assert_equal 2, results.next_page
101
101
 
102
102
  assert_equal 'Test1', results.first.title
@@ -109,7 +109,7 @@ module Tire
109
109
  assert_equal 2, results.total_pages
110
110
  assert_equal 2, results.current_page
111
111
  assert_equal 1, results.previous_page
112
- assert_equal 3, results.next_page
112
+ assert_equal nil, results.next_page
113
113
 
114
114
  assert_equal 'Test6', results.first.title
115
115
  end
@@ -121,7 +121,7 @@ module Tire
121
121
  assert_equal 2, results.total_pages
122
122
  assert_equal 3, results.current_page
123
123
  assert_equal 2, results.previous_page
124
- assert_equal 4, results.next_page
124
+ assert_equal nil, results.next_page
125
125
 
126
126
  assert_nil results.first
127
127
  end
@@ -132,7 +132,7 @@ module Tire
132
132
  setup { @q = 'test*' }
133
133
 
134
134
  should "find first page with five results" do
135
- results = ActiveRecordArticle.search nil, :per_page => 5, :page => 1 do |search|
135
+ results = ActiveRecordArticle.search do |search|
136
136
  search.query { |query| query.string @q }
137
137
  search.sort { title }
138
138
  search.from 0
@@ -142,14 +142,14 @@ module Tire
142
142
 
143
143
  assert_equal 2, results.total_pages
144
144
  assert_equal 1, results.current_page
145
- assert_equal 0, results.previous_page
145
+ assert_equal nil, results.previous_page
146
146
  assert_equal 2, results.next_page
147
147
 
148
148
  assert_equal 'Test1', results.first.title
149
149
  end
150
150
 
151
151
  should "find next page with five results" do
152
- results = ActiveRecordArticle.search nil, :per_page => 5, :page => 2 do |search|
152
+ results = ActiveRecordArticle.search do |search|
153
153
  search.query { |query| query.string @q }
154
154
  search.sort { title }
155
155
  search.from 5
@@ -160,13 +160,13 @@ module Tire
160
160
  assert_equal 2, results.total_pages
161
161
  assert_equal 2, results.current_page
162
162
  assert_equal 1, results.previous_page
163
- assert_equal 3, results.next_page
163
+ assert_equal nil, results.next_page
164
164
 
165
165
  assert_equal 'Test6', results.first.title
166
166
  end
167
167
 
168
- should "find not find missing page" do
169
- results = ActiveRecordArticle.search nil, :per_page => 5, :page => 3 do |search|
168
+ should "not find a missing page" do
169
+ results = ActiveRecordArticle.search do |search|
170
170
  search.query { |query| query.string @q }
171
171
  search.sort { title }
172
172
  search.from 10
@@ -177,7 +177,7 @@ module Tire
177
177
  assert_equal 2, results.total_pages
178
178
  assert_equal 3, results.current_page
179
179
  assert_equal 2, results.previous_page
180
- assert_equal 4, results.next_page
180
+ assert_equal nil, results.next_page
181
181
 
182
182
  assert_nil results.first
183
183
  end
@@ -187,6 +187,28 @@ module Tire
187
187
  assert_equal 3, hash['from']
188
188
  end
189
189
 
190
+ should "set the size value in options" do
191
+ Results::Collection.any_instance.stubs(:total).returns(50)
192
+ s = Search::Search.new('index') do
193
+ size 5
194
+ end
195
+
196
+ assert_equal 5, s.options[:size]
197
+ end
198
+
199
+ should "set the from value in options" do
200
+ Results::Collection.any_instance.stubs(:total).returns(50)
201
+ s = Search::Search.new('index') do
202
+ from 5
203
+ end
204
+
205
+ assert_equal 5, s.options[:from]
206
+ end
207
+
208
+ end
209
+
210
+ context "when limiting returned fields" do
211
+
190
212
  should "set the fields limit in request" do
191
213
  s = Search::Search.new('index') do
192
214
  fields :title
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tire
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Karel Minarik