elastic_searchable 0.4.0 → 0.5.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{elastic_searchable}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Sonnek"]
12
- s.date = %q{2011-02-22}
12
+ s.date = %q{2011-03-04}
13
13
  s.description = %q{integrate the elastic search engine with rails}
14
14
  s.email = %q{ryan@codecrate.com}
15
15
  s.extra_rdoc_files = [
@@ -2,17 +2,19 @@ require 'will_paginate/collection'
2
2
 
3
3
  module ElasticSearchable
4
4
  module Queries
5
+ PER_PAGE_DEFAULT = 20
6
+
5
7
  # search returns a will_paginate collection of ActiveRecord objects for the search results
6
- # options:
7
- # :per_page/:limit
8
- # :page/:offset
8
+ # supported options:
9
+ # :page - page of results to search for
10
+ # :per_page - number of results per page
9
11
  #
10
12
  # http://www.elasticsearch.com/docs/elasticsearch/rest_api/search/
11
13
  def search(query, options = {})
12
14
  page = (options.delete(:page) || 1).to_i
13
15
  options[:fields] ||= '_id'
14
16
  options[:q] ||= query
15
- options[:size] ||= (options.delete(:per_page) || 20)
17
+ options[:size] ||= per_page_for_search(options)
16
18
  options[:from] ||= options[:size] * (page - 1)
17
19
 
18
20
  response = ElasticSearchable.request :get, index_type_path('_search'), :query => options
@@ -24,5 +26,16 @@ module ElasticSearchable
24
26
  page.replace results
25
27
  page
26
28
  end
29
+
30
+ private
31
+ # determine the number of search results per page
32
+ # supports will_paginate configuration by using:
33
+ # Model.per_page
34
+ # Model.max_per_page
35
+ def per_page_for_search(options = {})
36
+ per_page = options.delete(:per_page) || (self.respond_to?(:per_page) ? self.per_page : nil) || ElasticSearchable::Queries::PER_PAGE_DEFAULT
37
+ per_page = [per_page.to_i, self.max_per_page].min if self.respond_to?(:max_per_page)
38
+ per_page
39
+ end
27
40
  end
28
41
  end
@@ -20,6 +20,9 @@ class TestElasticSearchable < Test::Unit::TestCase
20
20
  create_table :books, :force => true do |t|
21
21
  t.column :title, :string
22
22
  end
23
+ create_table :max_page_size_classes, :force => true do |t|
24
+ t.column :name, :string
25
+ end
23
26
  end
24
27
 
25
28
  def setup
@@ -282,5 +285,34 @@ class TestElasticSearchable < Test::Unit::TestCase
282
285
  end
283
286
  end
284
287
  end
288
+
289
+ class MaxPageSizeClass < ActiveRecord::Base
290
+ elastic_searchable
291
+ def self.max_per_page
292
+ 1
293
+ end
294
+ end
295
+ context 'with 2 MaxPageSizeClass instances' do
296
+ setup do
297
+ MaxPageSizeClass.create_index
298
+ @first = MaxPageSizeClass.create! :name => 'foo one'
299
+ @second = MaxPageSizeClass.create! :name => 'foo two'
300
+ MaxPageSizeClass.refresh_index
301
+ end
302
+ context 'MaxPageSizeClass.search with default options' do
303
+ setup do
304
+ @results = MaxPageSizeClass.search 'foo'
305
+ end
306
+ should 'have one per page' do
307
+ assert_equal 1, @results.per_page
308
+ end
309
+ should 'return one instance' do
310
+ assert_equal 1, @results.length
311
+ end
312
+ should 'have second page' do
313
+ assert_equal 2, @results.total_entries
314
+ end
315
+ end
316
+ end
285
317
  end
286
318
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_searchable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Sonnek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-22 00:00:00 -06:00
18
+ date: 2011-03-04 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency