Commit 09f1268ff2901dbe17e0c789d94c886b2dc33d15
1 parent
178dca9d
Exists in
master
and in
2 other branches
Added models option to similar method [skip ci]
Showing
3 changed files
with
13 additions
and
2 deletions
Show diff stats
CHANGELOG.md
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | - Searches now use lazy loading (similar to Active Record) |
4 | 4 | - Added support for `:async` and `:queue` modes for `reindex` on relation |
5 | 5 | - Added basic protection from unfiltered parameters to `where` option |
6 | +- Added `models` option to `similar` method | |
6 | 7 | - Anchor regular expressions by default |
7 | 8 | - Raise error when `search` called on relations |
8 | 9 | - Raise `ArgumentError` (instead of warning) for invalid regular expression modifiers | ... | ... |
lib/searchkick/index.rb
... | ... | @@ -169,9 +169,9 @@ module Searchkick |
169 | 169 | def similar_record(record, **options) |
170 | 170 | options[:per_page] ||= 10 |
171 | 171 | options[:similar] = [RecordData.new(self, record).record_data] |
172 | + options[:models] ||= [record.class] unless options.key?(:model) | |
172 | 173 | |
173 | - # TODO use index class instead of record class | |
174 | - Searchkick.search("*", model: record.class, **options) | |
174 | + Searchkick.search("*", **options) | |
175 | 175 | end |
176 | 176 | |
177 | 177 | def reload_synonyms | ... | ... |
test/inheritance_test.rb
... | ... | @@ -115,4 +115,14 @@ class InheritanceTest < Minitest::Test |
115 | 115 | end |
116 | 116 | assert_includes error.message, "Unknown model" |
117 | 117 | end |
118 | + | |
119 | + def test_similar | |
120 | + store_names ["Dog", "Other dog"], Dog | |
121 | + store_names ["Not dog"], Cat | |
122 | + | |
123 | + dog = Dog.find_by!(name: "Dog") | |
124 | + assert_equal ["Other dog"], dog.similar(fields: [:name]).map(&:name) | |
125 | + assert_equal ["Not dog", "Other dog"], dog.similar(fields: [:name], models: [Animal]).map(&:name).sort | |
126 | + assert_equal ["Not dog"], dog.similar(fields: [:name], models: [Cat]).map(&:name).sort | |
127 | + end | |
118 | 128 | end | ... | ... |