Skip to content

Commit 1f1577a

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

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

README.md

Lines changed: 11 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

@@ -291,10 +293,11 @@ You can search the same index multiple times by specifying `:index_uid`:
291293

292294
```ruby
293295
query = 'hero'
296+
294297
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' }
298+
'Isekai Manga' => { q: query, collection: Manga, filters: 'genre:isekai', index_uid: 'mangas_production' }
299+
'Shounen Manga' => { q: query, collection: Manga, filters: 'genre:shounen', index_uid: 'mangas_production' }
300+
'Steampunk Manga' => { q: query, collection: Manga, filters: 'genre:steampunk', index_uid: 'mangas_production' }
298301
)
299302
```
300303

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: 19 additions & 5 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

@@ -183,8 +197,8 @@ def reset_indexes
183197

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

@@ -194,15 +208,15 @@ def reset_indexes
194208
end
195209

196210
it 'infers the model' do
197-
results = MeiliSearch::Rails.multi_search(
211+
results = Meilisearch::Rails.multi_search(
198212
'colors' => { q: 'bl', collection: Color.all, index_uid: Color.index.uid }
199213
)
200214

201215
expect(results.to_h['colors']).to contain_exactly(blue, black)
202216
end
203217

204218
it 'infers the index as well as the model' do
205-
results = MeiliSearch::Rails.multi_search(
219+
results = Meilisearch::Rails.multi_search(
206220
'colors' => { q: 'bl', collection: Color }
207221
)
208222

0 commit comments

Comments
 (0)