Skip to content

Commit 0d7480a

Browse files
committed
Rename collection option to scope
1 parent 1f1577a commit 0d7480a

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +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 `:collection` option is passed:
268+
Records are loaded when the keys are models, or when `:scope` option is passed:
269269

270270
```ruby
271271
multi_search_results = Meilisearch::Rails.multi_search(
272-
# Collection may be a relation
273-
'books' => { q: 'Harry', collection: Book.all },
272+
# scope may be a relation
273+
'books' => { q: 'Harry', scope: Book.all },
274274
# or a model
275-
'mangas' => { q: 'Attack', collection: Manga }
275+
'mangas' => { q: 'Attack', scope: Manga }
276276
)
277277
```
278278

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

283283
```ruby
284284
multi_search_results = Meilisearch::Rails.multi_search(
285-
'western' => { q: 'Harry', collection: Book, index_uid: 'books_production' },
286-
'japanese' => { q: 'Attack', collection: Manga, index_uid: 'mangas_production' }
285+
'western' => { q: 'Harry', scope: Book, index_uid: 'books_production' },
286+
'japanese' => { q: 'Attack', scope: Manga, index_uid: 'mangas_production' }
287287
)
288288
```
289289

@@ -295,9 +295,9 @@ You can search the same index multiple times by specifying `:index_uid`:
295295
query = 'hero'
296296

297297
multi_search_results = Meilisearch::Rails.multi_search(
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' }
298+
'Isekai Manga' => { q: query, scope: Manga, filters: 'genre:isekai', index_uid: 'mangas_production' }
299+
'Shounen Manga' => { q: query, scope: Manga, filters: 'genre:shounen', index_uid: 'mangas_production' }
300+
'Steampunk Manga' => { q: query, scope: Manga, filters: 'genre:steampunk', index_uid: 'mangas_production' }
301301
)
302302
```
303303

lib/meilisearch/rails/multi_search.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Rails
55
class << self
66
def multi_search(searches)
77
search_parameters = searches.map do |(index_target, options)|
8-
collection_class = options[:collection].respond_to?(:model) ? options[:collection].model : options[:collection]
9-
index_target = options.delete(:index_uid) || collection_class || index_target
8+
model_class = options[:scope].respond_to?(:model) ? options[:scope].model : options[:scope]
9+
index_target = options.delete(:index_uid) || model_class || index_target
1010

1111
paginate(options) if pagination_enabled?
1212
normalize(options, index_target)
@@ -19,7 +19,7 @@ def multi_search(searches)
1919

2020
def normalize(options, index_target)
2121
options
22-
.except(:class_name, :collection)
22+
.except(:class_name, :scope)
2323
.merge!(index_uid: index_uid_from_target(index_target))
2424
end
2525

lib/meilisearch/rails/multi_search/result.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ def initialize(searches, raw_results)
1010
searches.zip(raw_results['results']).each do |(target, search_options), result|
1111
results_class = if search_options[:class_name]
1212
Meilisearch::Rails.logger.warn(
13-
'[meilisearch-rails] The :class_name option in multi search is deprecated, please use :collection instead.'
13+
'[meilisearch-rails] The :class_name option in multi search is deprecated, please use :scope instead.'
1414
)
1515

1616
search_options[:class_name].constantize
1717
elsif target.instance_of?(Class)
1818
target
19-
elsif search_options[:collection]
20-
search_options[:collection]
19+
elsif search_options[:scope]
20+
search_options[:scope]
2121
end
2222

23-
@results[target] = results_class ? load_results(results_class, result, collection: search_options[:collection]) : result['hits']
23+
@results[target] = results_class ? load_results(results_class, result, scope: search_options[:scope]) : result['hits']
2424

2525
@metadata[target] = result.except('hits')
2626
end
@@ -75,8 +75,8 @@ def to_h
7575

7676
private
7777

78-
def load_results(klass, result, collection:)
79-
collection ||= klass
78+
def load_results(klass, result, scope:)
79+
scope ||= klass
8080

8181
pk_method = klass.ms_primary_key_method
8282
pk_method = pk_method.in if Utilities.mongo_model?(klass)
@@ -86,7 +86,7 @@ def load_results(klass, result, collection:)
8686
hits_by_id =
8787
result['hits'].index_by { |hit| hit[condition_key.to_s] }
8888

89-
records = collection.where(condition_key => hits_by_id.keys)
89+
records = scope.where(condition_key => hits_by_id.keys)
9090

9191
if records.respond_to? :in_order_of
9292
records.in_order_of(condition_key, hits_by_id.keys).each do |record|

spec/multi_search_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def reset_indexes
164164
it 'returns a mixture of ORM records and hashes' do
165165
results = Meilisearch::Rails.multi_search(
166166
Book => { q: 'Steve' },
167-
Product.index.uid => { q: 'palm', limit: 1, collection: Product },
167+
Product.index.uid => { q: 'palm', limit: 1, scope: Product },
168168
Color.index.uid => { q: 'bl' }
169169
)
170170

@@ -195,11 +195,11 @@ def reset_indexes
195195
end
196196
end
197197

198-
context 'with collections' do
199-
it 'fetches items from the given collection' do
198+
context 'with scopes' do
199+
it 'fetches items from the given scope' do
200200
results = Meilisearch::Rails.multi_search(
201-
Product => { q: 'palm', collection: Product.where('tags LIKE "%terrible%"') },
202-
Color => { q: 'bl', collection: Color.where(short_name: 'bla') }
201+
Product => { q: 'palm', scope: Product.where('tags LIKE "%terrible%"') },
202+
Color => { q: 'bl', scope: Color.where(short_name: 'bla') }
203203
)
204204

205205
expect(results).to contain_exactly(
@@ -209,15 +209,15 @@ def reset_indexes
209209

210210
it 'infers the model' do
211211
results = Meilisearch::Rails.multi_search(
212-
'colors' => { q: 'bl', collection: Color.all, index_uid: Color.index.uid }
212+
'colors' => { q: 'bl', scope: Color.all, index_uid: Color.index.uid }
213213
)
214214

215215
expect(results.to_h['colors']).to contain_exactly(blue, black)
216216
end
217217

218218
it 'infers the index as well as the model' do
219219
results = Meilisearch::Rails.multi_search(
220-
'colors' => { q: 'bl', collection: Color }
220+
'colors' => { q: 'bl', scope: Color }
221221
)
222222

223223
expect(results.to_h['colors']).to contain_exactly(blue, black)

0 commit comments

Comments
 (0)