From 94f7ff3763dc93cb970ac84648963265ee07b827 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Thu, 26 Dec 2013 23:16:06 -0800 Subject: [PATCH] Prepared should_index? for merge --- CHANGELOG.md | 4 ++++ README.md | 16 +++++++++++----- lib/searchkick/model.rb | 2 +- lib/searchkick/reindex.rb | 2 +- searchkick.gemspec | 1 - test/model_test.rb | 50 -------------------------------------------------- test/should_index_test.rb | 34 ++++++++++++++++++++++++++++++++++ test/test_helper.rb | 5 ++++- 8 files changed, 55 insertions(+), 59 deletions(-) delete mode 100644 test/model_test.rb create mode 100644 test/should_index_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 2db4217..958b7dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.2 [unreleased] + +- Added `should_index?` method to control which records are indexed + ## 0.4.1 - Fixed issue w/ inheritance mapping diff --git a/README.md b/README.md index 75de7e9..6796aff 100644 --- a/README.md +++ b/README.md @@ -172,14 +172,10 @@ Product.search "zucini", misspellings: {distance: 2} # zucchini ### Indexing -Control which models are indexed with `#should_index?` and what data is indexed with the `#search_data`. Call `Product.reindex` after changing this method. +Control what data is indexed with the `search_data` method. Call `Product.reindex` after changing this method. ```ruby class Product < ActiveRecord::Base - def should_index? - active - end - def search_data as_json only: [:name, :active] # or equivalently @@ -199,6 +195,16 @@ class Product < ActiveRecord::Base end ``` +[master branch] By default, all records are indexed. To control which records are indexed, use the `should_index?` method. + +```ruby +class Product < ActiveRecord::Base + def should_index? + active # only index active records + end +end +``` + ### To Reindex, or Not to Reindex #### Reindex diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 67de2cf..41bff11 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -24,7 +24,7 @@ module Searchkick def reindex index = self.class.searchkick_index - if destroyed? || !should_index? + if destroyed? or !should_index? index.remove self else index.store self diff --git a/lib/searchkick/reindex.rb b/lib/searchkick/reindex.rb index 491ad32..1b5622a 100644 --- a/lib/searchkick/reindex.rb +++ b/lib/searchkick/reindex.rb @@ -62,7 +62,7 @@ module Searchkick scope = scope.search_import if scope.respond_to?(:search_import) if scope.respond_to?(:find_in_batches) scope.find_in_batches do |batch| - index.import batch.select{ |item| item.should_index? } + index.import batch.select{|item| item.should_index? } end else # https://github.com/karmi/tire/blob/master/lib/tire/model/import.rb diff --git a/searchkick.gemspec b/searchkick.gemspec index ab88d95..0e60113 100644 --- a/searchkick.gemspec +++ b/searchkick.gemspec @@ -26,5 +26,4 @@ Gem::Specification.new do |spec| spec.add_development_dependency "minitest", "~> 4.7" spec.add_development_dependency "activerecord" spec.add_development_dependency "pg" - spec.add_development_dependency "mocha" end diff --git a/test/model_test.rb b/test/model_test.rb deleted file mode 100644 index 8273fd8..0000000 --- a/test/model_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -require_relative "test_helper" - -describe "Model#should_index?" do - it 'Model.reindex checks for #should_index?' do - indexed = Animal.new(name: 'should') - indexed.stubs(:should_index?).returns(true) - - not_indexed = Animal.new(name: 'should not') - not_indexed.stubs(:should_index?).returns(false) - - [indexed, not_indexed].each(&:save!) - Animal.searchkick_index.refresh - - assert_search 'should', ['should'], {}, Animal - end - - it 'Model#reindex checks for #should_index?' do - Animal.any_instance.stubs(:should_index?).returns(false) - store_names ['should not', 'shouldnt'], Animal - - Animal.reindex - Animal.searchkick_index.refresh - - assert_search 'should', [], {}, Animal - end - - it 'indexes existing model after the #should_index? switched to true' do - subject = Animal.new(name: 'should') - subject.stubs(:should_index?).returns(false) - subject.save! - - subject.stubs(:should_index?).returns(true) - subject.save! - - Animal.searchkick_index.refresh - assert_search 'should', ['should'], {}, Animal - end - - it 'removes the existing model from index after the #should_index? switched to false' do - subject = Animal.new(name: 'should') - subject.stubs(:should_index?).returns(true) - subject.save! - - subject.stubs(:should_index?).returns(false) - subject.save! - - Animal.searchkick_index.refresh - assert_search 'should', [], {}, Animal - end -end diff --git a/test/should_index_test.rb b/test/should_index_test.rb new file mode 100644 index 0000000..fb79854 --- /dev/null +++ b/test/should_index_test.rb @@ -0,0 +1,34 @@ +require_relative "test_helper" + +class TestShouldIndex < Minitest::Unit::TestCase + + def test_basic + store_names ["INDEX", "DO NOT INDEX"] + assert_search "index", ["INDEX"] + end + + def test_default_true + assert Animal.new.should_index? + end + + def test_change_to_true + store_names ["DO NOT INDEX"] + assert_search "index", [] + product = Product.first + product.name = "INDEX" + product.save! + Product.searchkick_index.refresh + assert_search "index", ["INDEX"] + end + + def test_change_to_false + store_names ["INDEX"] + assert_search "index", ["INDEX"] + product = Product.first + product.name = "DO NOT INDEX" + product.save! + Product.searchkick_index.refresh + assert_search "index", [] + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 2d6e8ee..890d226 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,7 +2,6 @@ require "bundler/setup" Bundler.require(:default) require "minitest/autorun" require "minitest/pride" -require "mocha/setup" ENV["RACK_ENV"] = "test" @@ -120,6 +119,10 @@ class Product def search_data serializable_hash.merge conversions: conversions, user_ids: user_ids, location: [latitude, longitude], multiple_locations: [[latitude, longitude], [0, 0]] end + + def should_index? + name != "DO NOT INDEX" + end end class Animal -- libgit2 0.21.0