Skip to content

Commit d969d9b

Browse files
committed
Soft deprecate :class_name in multi_search
In favor of :collection option
1 parent e6e8d19 commit d969d9b

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,14 @@ Use `#each_result` to loop through pairs of your provided keys and the results:
265265
</ul>
266266
```
267267

268-
Records are loaded when the keys are models, or when `:class_name` option is passed:
268+
Records are loaded when the keys are models, or when `:collection` option is passed:
269269

270270
```ruby
271271
multi_search_results = MeiliSearch::Rails.multi_search(
272-
'books' => { q: 'Harry', class_name: 'Book' },
273-
'mangas' => { q: 'Attack', class_name: 'Manga' }
272+
# Collection may be a relation
273+
'books' => { q: 'Harry', collection: Book.all },
274+
# or a model
275+
'mangas' => { q: 'Attack', collection: Manga }
274276
)
275277
```
276278

@@ -280,8 +282,8 @@ The index to search is inferred from the model if the key is a model, if the key
280282

281283
```ruby
282284
multi_search_results = MeiliSearch::Rails.multi_search(
283-
'western' => { q: 'Harry', class_name: 'Book', index_uid: 'books_production' },
284-
'japanese' => { q: 'Attack', class_name: 'Manga', index_uid: 'mangas_production' }
285+
'western' => { q: 'Harry', collection: Book, index_uid: 'books_production' },
286+
'japanese' => { q: 'Attack', collection: Manga, index_uid: 'mangas_production' }
285287
)
286288
```
287289

@@ -292,9 +294,9 @@ You can search the same index multiple times by specifying `:index_uid`:
292294
```ruby
293295
query = 'hero'
294296
multi_search_results = MeiliSearch::Rails.multi_search(
295-
'Isekai Manga' => { q: query, class_name: 'Manga', filters: 'genre:isekai', index_uid: 'mangas_production' }
296-
'Shounen Manga' => { q: query, class_name: 'Manga', filters: 'genre:shounen', index_uid: 'mangas_production' }
297-
'Steampunk Manga' => { q: query, class_name: 'Manga', filters: 'genre:steampunk', index_uid: 'mangas_production' }
297+
'Isekai Manga' => { q: query, collection: Manga, filters: 'genre:isekai', index_uid: 'mangas_production' }
298+
'Shounen Manga' => { q: query, collection: Manga, filters: 'genre:shounen', index_uid: 'mangas_production' }
299+
'Steampunk Manga' => { q: query, collection: Manga, filters: 'genre:steampunk', index_uid: 'mangas_production' }
298300
)
299301
```
300302

lib/meilisearch/rails/multi_search/result.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def initialize(searches, raw_results)
99

1010
searches.zip(raw_results['results']).each do |(target, search_options), result|
1111
results_class = if search_options[:class_name]
12+
MeiliSearch::Rails.logger.warn(
13+
'[meilisearch-rails] The :class_name option in multi search is deprecated, please use :collection instead.'
14+
)
15+
1216
search_options[:class_name].constantize
1317
elsif target.instance_of?(Class)
1418
target

spec/multi_search_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def reset_indexes
9191

9292
context 'when :class_name is also present' do
9393
it 'loads results from the correct models' do
94+
allow(MeiliSearch::Rails.logger).to receive(:warn).and_return(nil)
95+
9496
results = MeiliSearch::Rails.multi_search(
9597
'books' => { q: 'Steve', index_uid: Book.index.uid, class_name: 'Book' },
9698
'products' => { q: 'palm', limit: 1, index_uid: Product.index.uid, class_name: 'Product' },
@@ -122,6 +124,18 @@ def reset_indexes
122124
end
123125

124126
context 'when class_name is specified' do
127+
let(:logger) { instance_double('Logger', warn: nil) }
128+
129+
before do
130+
allow(MeiliSearch::Rails).to receive(:logger).and_return(logger)
131+
end
132+
133+
it 'warns about deprecation' do
134+
results = MeiliSearch::Rails.multi_search(Book.index.uid => { q: 'Steve', class_name: 'Book' })
135+
expect(results.to_h[Book.index.uid]).to contain_exactly(steve_jobs)
136+
expect(logger).to have_received(:warn).with(a_string_matching(':class_name'))
137+
end
138+
125139
it 'returns ORM records' do
126140
results = MeiliSearch::Rails.multi_search(
127141
Book.index.uid => { q: 'Steve', class_name: 'Book' },
@@ -150,7 +164,7 @@ def reset_indexes
150164
it 'returns a mixture of ORM records and hashes' do
151165
results = MeiliSearch::Rails.multi_search(
152166
Book => { q: 'Steve' },
153-
Product.index.uid => { q: 'palm', limit: 1, class_name: 'Product' },
167+
Product.index.uid => { q: 'palm', limit: 1, collection: Product },
154168
Color.index.uid => { q: 'bl' }
155169
)
156170

@@ -184,7 +198,7 @@ def reset_indexes
184198
context 'with collections' do
185199
it 'fetches items from the given collection' do
186200
results = MeiliSearch::Rails.multi_search(
187-
Product.index.uid => { q: 'palm', class_name: 'Product', collection: Product.where('tags LIKE "%terrible%"') },
201+
Product => { q: 'palm', collection: Product.where('tags LIKE "%terrible%"') },
188202
Color => { q: 'bl', collection: Color.where(short_name: 'bla') }
189203
)
190204

0 commit comments

Comments
 (0)