Commit 31abe7e2976114d6a5eecb5b922b42dd33935534

Authored by Jacek Tomaszewski
1 parent 6c51a2b1

add should_index? method to control what models will be indexed

README.md
... ... @@ -172,10 +172,14 @@ Product.search "zucini", misspellings: {distance: 2} # zucchini
172 172  
173 173 ### Indexing
174 174  
175   -Control what data is indexed with the `search_data` method. Call `Product.reindex` after changing this method.
  175 +Control which models are indexed with `#should_index?` and what data is indexed with the `#search_data`. Call `Product.reindex` after changing this method.
176 176  
177 177 ```ruby
178 178 class Product < ActiveRecord::Base
  179 + def should_index?
  180 + active
  181 + end
  182 +
179 183 def search_data
180 184 as_json only: [:name, :active]
181 185 # or equivalently
... ...
lib/searchkick/model.rb
... ... @@ -18,12 +18,18 @@ module Searchkick
18 18 extend Searchkick::Reindex
19 19 include Searchkick::Similar
20 20  
  21 + def should_index?
  22 + true
  23 + end
  24 +
21 25 def reindex
22   - index = self.class.searchkick_index
23   - if destroyed?
24   - index.remove self
25   - else
26   - index.store self
  26 + if should_index?
  27 + index = self.class.searchkick_index
  28 + if destroyed?
  29 + index.remove self
  30 + else
  31 + index.store self
  32 + end
27 33 end
28 34 end
29 35  
... ...
lib/searchkick/reindex.rb
... ... @@ -62,14 +62,14 @@ module Searchkick
62 62 scope = scope.search_import if scope.respond_to?(:search_import)
63 63 if scope.respond_to?(:find_in_batches)
64 64 scope.find_in_batches do |batch|
65   - index.import batch
  65 + index.import batch.select{ |item| item.should_index? }
66 66 end
67 67 else
68 68 # https://github.com/karmi/tire/blob/master/lib/tire/model/import.rb
69 69 # use cursor for Mongoid
70 70 items = []
71 71 scope.all.each do |item|
72   - items << item
  72 + items << item if item.should_index?
73 73 if items.length % 1000 == 0
74 74 index.import items
75 75 items = []
... ...
searchkick.gemspec
... ... @@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26 26 spec.add_development_dependency "minitest", "~> 4.7"
27 27 spec.add_development_dependency "activerecord"
28 28 spec.add_development_dependency "pg"
  29 + spec.add_development_dependency "mocha"
29 30 end
... ...
test/model_test.rb 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +require_relative "test_helper"
  2 +
  3 +describe "Model#should_index?" do
  4 + it 'Model.reindex checks for #should_index?' do
  5 + indexed = Animal.new(name: 'should')
  6 + indexed.stubs(:should_index?).returns(true)
  7 +
  8 + not_indexed = Animal.new(name: 'should not')
  9 + not_indexed.stubs(:should_index?).returns(false)
  10 +
  11 + [indexed, not_indexed].each(&:save!)
  12 + Animal.searchkick_index.refresh
  13 +
  14 + assert_search 'should', ['should'], {}, Animal
  15 + end
  16 +
  17 + it 'Model#reindex checks for #should_index?' do
  18 + Animal.any_instance.stubs(:should_index?).returns(false)
  19 + store_names ['should not', 'shouldnt'], Animal
  20 +
  21 + Animal.reindex
  22 + Animal.searchkick_index.refresh
  23 +
  24 + assert_search 'should', [], {}, Animal
  25 + end
  26 +end
... ...
test/test_helper.rb
... ... @@ -2,6 +2,7 @@ require &quot;bundler/setup&quot;
2 2 Bundler.require(:default)
3 3 require "minitest/autorun"
4 4 require "minitest/pride"
  5 +require "mocha/setup"
5 6  
6 7 ENV["RACK_ENV"] = "test"
7 8  
... ... @@ -151,16 +152,16 @@ class Minitest::Unit::TestCase
151 152 end
152 153  
153 154 # no order
154   - def assert_search(term, expected, options = {})
155   - assert_equal expected.sort, Product.search(term, options).map(&:name).sort
  155 + def assert_search(term, expected, options = {}, klass = Product)
  156 + assert_equal expected.sort, klass.search(term, options).map(&:name).sort
156 157 end
157 158  
158   - def assert_order(term, expected, options = {})
159   - assert_equal expected, Product.search(term, options).map(&:name)
  159 + def assert_order(term, expected, options = {}, klass = Product)
  160 + assert_equal expected, klass.search(term, options).map(&:name)
160 161 end
161 162  
162   - def assert_first(term, expected, options = {})
163   - assert_equal expected, Product.search(term, options).map(&:name).first
  163 + def assert_first(term, expected, options = {}, klass = Product)
  164 + assert_equal expected, klass.search(term, options).map(&:name).first
164 165 end
165 166  
166 167 end
... ...