Commit 94f7ff3763dc93cb970ac84648963265ee07b827
1 parent
c95e7217
Exists in
master
and in
21 other branches
Prepared should_index? for merge
Showing
8 changed files
with
55 additions
and
59 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -172,14 +172,10 @@ Product.search "zucini", misspellings: {distance: 2} # zucchini |
172 | 172 | |
173 | 173 | ### Indexing |
174 | 174 | |
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. | |
175 | +Control what data is indexed with the `search_data` method. 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 | - | |
183 | 179 | def search_data |
184 | 180 | as_json only: [:name, :active] |
185 | 181 | # or equivalently |
... | ... | @@ -199,6 +195,16 @@ class Product < ActiveRecord::Base |
199 | 195 | end |
200 | 196 | ``` |
201 | 197 | |
198 | +[master branch] By default, all records are indexed. To control which records are indexed, use the `should_index?` method. | |
199 | + | |
200 | +```ruby | |
201 | +class Product < ActiveRecord::Base | |
202 | + def should_index? | |
203 | + active # only index active records | |
204 | + end | |
205 | +end | |
206 | +``` | |
207 | + | |
202 | 208 | ### To Reindex, or Not to Reindex |
203 | 209 | |
204 | 210 | #### Reindex | ... | ... |
lib/searchkick/model.rb
lib/searchkick/reindex.rb
... | ... | @@ -62,7 +62,7 @@ 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.select{ |item| item.should_index? } | |
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 | ... | ... |
searchkick.gemspec
test/model_test.rb
... | ... | @@ -1,50 +0,0 @@ |
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 | - | |
27 | - it 'indexes existing model after the #should_index? switched to true' do | |
28 | - subject = Animal.new(name: 'should') | |
29 | - subject.stubs(:should_index?).returns(false) | |
30 | - subject.save! | |
31 | - | |
32 | - subject.stubs(:should_index?).returns(true) | |
33 | - subject.save! | |
34 | - | |
35 | - Animal.searchkick_index.refresh | |
36 | - assert_search 'should', ['should'], {}, Animal | |
37 | - end | |
38 | - | |
39 | - it 'removes the existing model from index after the #should_index? switched to false' do | |
40 | - subject = Animal.new(name: 'should') | |
41 | - subject.stubs(:should_index?).returns(true) | |
42 | - subject.save! | |
43 | - | |
44 | - subject.stubs(:should_index?).returns(false) | |
45 | - subject.save! | |
46 | - | |
47 | - Animal.searchkick_index.refresh | |
48 | - assert_search 'should', [], {}, Animal | |
49 | - end | |
50 | -end |
... | ... | @@ -0,0 +1,34 @@ |
1 | +require_relative "test_helper" | |
2 | + | |
3 | +class TestShouldIndex < Minitest::Unit::TestCase | |
4 | + | |
5 | + def test_basic | |
6 | + store_names ["INDEX", "DO NOT INDEX"] | |
7 | + assert_search "index", ["INDEX"] | |
8 | + end | |
9 | + | |
10 | + def test_default_true | |
11 | + assert Animal.new.should_index? | |
12 | + end | |
13 | + | |
14 | + def test_change_to_true | |
15 | + store_names ["DO NOT INDEX"] | |
16 | + assert_search "index", [] | |
17 | + product = Product.first | |
18 | + product.name = "INDEX" | |
19 | + product.save! | |
20 | + Product.searchkick_index.refresh | |
21 | + assert_search "index", ["INDEX"] | |
22 | + end | |
23 | + | |
24 | + def test_change_to_false | |
25 | + store_names ["INDEX"] | |
26 | + assert_search "index", ["INDEX"] | |
27 | + product = Product.first | |
28 | + product.name = "DO NOT INDEX" | |
29 | + product.save! | |
30 | + Product.searchkick_index.refresh | |
31 | + assert_search "index", [] | |
32 | + end | |
33 | + | |
34 | +end | ... | ... |
test/test_helper.rb
... | ... | @@ -2,7 +2,6 @@ require "bundler/setup" |
2 | 2 | Bundler.require(:default) |
3 | 3 | require "minitest/autorun" |
4 | 4 | require "minitest/pride" |
5 | -require "mocha/setup" | |
6 | 5 | |
7 | 6 | ENV["RACK_ENV"] = "test" |
8 | 7 | |
... | ... | @@ -120,6 +119,10 @@ class Product |
120 | 119 | def search_data |
121 | 120 | serializable_hash.merge conversions: conversions, user_ids: user_ids, location: [latitude, longitude], multiple_locations: [[latitude, longitude], [0, 0]] |
122 | 121 | end |
122 | + | |
123 | + def should_index? | |
124 | + name != "DO NOT INDEX" | |
125 | + end | |
123 | 126 | end |
124 | 127 | |
125 | 128 | class Animal | ... | ... |