Commit da93f5b944c5ed2a22550020ae6fa2ab0539b756

Authored by Andrew Kane
1 parent 32d5932f

Added pagination

README.md
... ... @@ -215,7 +215,7 @@ You can also boost by:
215 215 Use a `*` for the query.
216 216  
217 217 ```ruby
218   -Product.search "*"
  218 +Product.search("*")
219 219 ```
220 220  
221 221 ### Pagination
... ... @@ -224,7 +224,7 @@ Plays nicely with kaminari and will_paginate.
224 224  
225 225 ```ruby
226 226 # controller
227   -@products = Product.search "milk", page: params[:page], per_page: 20
  227 +@products = Product.search("milk").page(params[:page]).per_page(20)
228 228 ```
229 229  
230 230 View with kaminari
... ...
lib/searchkick/relation.rb
... ... @@ -7,7 +7,7 @@ module Searchkick
7 7 def_delegators :execute, :map, :each, :any?, :empty?, :size, :length, :slice, :[], :to_ary,
8 8 :records, :results, :suggestions, :each_with_hit, :with_details, :aggregations, :aggs,
9 9 :took, :error, :model_name, :entry_name, :total_count, :total_entries,
10   - :current_page, :per_page, :limit_value, :padding, :total_pages, :num_pages,
  10 + :current_page, :per_page, :limit_value, :total_pages, :num_pages,
11 11 :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?,
12 12 :out_of_range?, :hits, :response, :to_a, :first, :scroll
13 13  
... ... @@ -82,6 +82,33 @@ module Searchkick
82 82 self
83 83 end
84 84  
  85 + def page(value)
  86 + spawn.page!(value)
  87 + end
  88 +
  89 + def page!(value)
  90 + options[:page] = value
  91 + self
  92 + end
  93 +
  94 + def per_page(value)
  95 + spawn.per_page!(value)
  96 + end
  97 +
  98 + def per_page!(value)
  99 + options[:per_page] = value
  100 + self
  101 + end
  102 +
  103 + def padding(value)
  104 + spawn.padding!(value)
  105 + end
  106 +
  107 + def padding!(value)
  108 + options[:padding] = value
  109 + self
  110 + end
  111 +
85 112 # same as Active Record
86 113 def inspect
87 114 entries = results.first(11).map!(&:inspect)
... ... @@ -100,8 +127,9 @@ module Searchkick
100 127 end
101 128 end
102 129  
  130 + # TODO reset when ! methods called
103 131 def execute
104   - Query.new(klass, term, options).execute
  132 + @execute ||= Query.new(klass, term, options).execute
105 133 end
106 134  
107 135 def spawn
... ...
test/pagination_test.rb
... ... @@ -43,6 +43,32 @@ class PaginationTest < Minitest::Test
43 43 assert products.any?
44 44 end
45 45  
  46 + def test_pagination_relation
  47 + store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"]
  48 + products = Product.search("product", relation: true).order(name: :asc).page(2).per_page(2).padding(1)
  49 + assert_equal ["Product D", "Product E"], products.map(&:name)
  50 + assert_equal "product", products.entry_name
  51 + assert_equal 2, products.current_page
  52 + # assert_equal 1, products.padding
  53 + # assert_equal 2, products.per_page
  54 + assert_equal 2, products.size
  55 + assert_equal 2, products.length
  56 + assert_equal 3, products.total_pages
  57 + assert_equal 6, products.total_count
  58 + assert_equal 6, products.total_entries
  59 + assert_equal 2, products.limit_value
  60 + assert_equal 3, products.offset_value
  61 + # assert_equal 3, products.offset
  62 + assert_equal 3, products.next_page
  63 + assert_equal 1, products.previous_page
  64 + assert_equal 1, products.prev_page
  65 + assert !products.first_page?
  66 + assert !products.last_page?
  67 + assert !products.empty?
  68 + assert !products.out_of_range?
  69 + assert products.any?
  70 + end
  71 +
46 72 def test_pagination_body
47 73 store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"]
48 74 products = Product.search("product", body: {query: {match_all: {}}, sort: [{name: "asc"}]}, page: 2, per_page: 2, padding: 1)
... ...