Skip to content

Commit 2875709

Browse files
committed
Add global without_auto_index_scope method
The implementation is very straightforward, for now it only has a block form. We could add a toggle if that is useful in the future.
1 parent 5450df2 commit 2875709

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/meilisearch-rails.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ def ms_without_auto_index_scope=(value)
496496
end
497497

498498
def ms_without_auto_index_scope
499-
Thread.current["ms_without_auto_index_scope_for_#{model_name}"]
499+
Thread.current['ms_without_auto_index_scope'] ||
500+
Thread.current["ms_without_auto_index_scope_for_#{model_name}"]
500501
end
501502

502503
def ms_reindex!(batch_size = MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, synchronous = false)

lib/meilisearch/rails/configuration.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ def client
5151
.merge(client_agents: MeiliSearch::Rails.qualified_version)
5252
)
5353
end
54+
55+
def without_auto_index(&block)
56+
Thread.current['ms_without_auto_index_scope'] = true
57+
58+
begin
59+
yield
60+
ensure
61+
Thread.current['ms_without_auto_index_scope'] = false
62+
end
63+
end
5464
end
5565
end
5666
end

spec/model_methods_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@
7070
end
7171
end
7272

73+
# while this is not a model method, it's tested here since it's logically similar
74+
# to the model method by the same name
75+
describe 'Meilisearch::Rails.without_auto_index' do
76+
it 'disables auto indexing for all models' do
77+
TestUtil.reset_colors!
78+
TestUtil.reset_books!
79+
80+
MeiliSearch::Rails.without_auto_index do
81+
Color.create!(name: 'blue', short_name: 'b', hex: 0xFF0000)
82+
Book.create!(
83+
name: 'Frankenstein', author: 'Mary Shelley',
84+
premium: false, released: true
85+
)
86+
end
87+
88+
expect(Color.search('blue')).to be_empty
89+
expect(Book.search('Frankenstein')).to be_empty
90+
end
91+
end
92+
7393
describe '.index_documents' do
7494
it 'updates existing documents' do
7595
TestUtil.reset_colors!

0 commit comments

Comments
 (0)