Skip to content

Implement global auto-indexing switch #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-03-08 11:51:39 UTC using RuboCop version 1.27.0.
# on 2025-03-17 16:23:58 UTC using RuboCop version 1.27.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -30,13 +30,6 @@ Layout/EmptyLinesAroundModuleBody:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand Down Expand Up @@ -66,12 +59,13 @@ Lint/UnusedBlockArgument:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 2
# Offense count: 3
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods.
Lint/UnusedMethodArgument:
Exclude:
- 'lib/meilisearch-rails.rb'
- 'lib/meilisearch/rails/configuration.rb'

# Offense count: 13
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Expand Down Expand Up @@ -102,7 +96,7 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 437
Max: 438

# Offense count: 8
# Configuration parameters: IgnoredMethods.
Expand Down Expand Up @@ -146,7 +140,7 @@ RSpec/ContextWording:
- 'spec/options_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 57
# Offense count: 62
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 16
Expand Down Expand Up @@ -174,7 +168,7 @@ RSpec/MultipleDescribes:
Exclude:
- 'spec/search_spec.rb'

# Offense count: 2
# Offense count: 3
RSpec/NestedGroups:
Max: 4

Expand All @@ -184,13 +178,6 @@ RSpec/VerifiedDoubles:
Exclude:
- 'spec/configuration_spec.rb'

# Offense count: 1
# Configuration parameters: ForbiddenMethods, AllowedMethods.
# ForbiddenMethods: decrement!, decrement_counter, increment!, increment_counter, insert, insert!, insert_all, insert_all!, toggle!, touch, touch_all, update_all, update_attribute, update_column, update_columns, update_counters, upsert, upsert_all
Rails/SkipsModelValidations:
Exclude:
- 'spec/settings_spec.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand Down Expand Up @@ -247,7 +234,7 @@ Style/StringLiterals:
Exclude:
- 'spec/ms_clean_up_job_spec.rb'

# Offense count: 15
# Offense count: 16
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -909,10 +909,18 @@ end
You can temporarily disable auto-indexing using the without_auto_index scope:

```ruby
# disable Book's auto-indexing
Book.without_auto_index do
# Inside this block, auto indexing task will not run.
1.upto(10000) { Book.create! attributes }
end

# disable all auto-indexing
Meilisearch::Rails.without_auto_index do
# Inside this block, auto indexing task will not run.
1.upto(10000) { Book.create! attributes }
1.upto(10000) { User.create! user_attributes }
end
```

## ⚙️ Development workflow & contributing
Expand Down
3 changes: 2 additions & 1 deletion lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ def ms_without_auto_index_scope=(value)
end

def ms_without_auto_index_scope
Thread.current["ms_without_auto_index_scope_for_#{model_name}"]
Thread.current['ms_without_auto_index_scope'] ||
Thread.current["ms_without_auto_index_scope_for_#{model_name}"]
end

def ms_reindex!(batch_size = MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, synchronous = false)
Expand Down
10 changes: 10 additions & 0 deletions lib/meilisearch/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def client
.merge(client_agents: MeiliSearch::Rails.qualified_version)
)
end

def without_auto_index(&block)
Thread.current['ms_without_auto_index_scope'] = true

begin
yield
ensure
Thread.current['ms_without_auto_index_scope'] = false
end
end
Comment on lines +55 to +63

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice

end
end
end
20 changes: 20 additions & 0 deletions spec/model_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@
end
end

# while this is not a model method, it's tested here since it's logically similar
# to the model method by the same name
describe 'Meilisearch::Rails.without_auto_index' do
it 'disables auto indexing for all models' do
TestUtil.reset_colors!
TestUtil.reset_books!

MeiliSearch::Rails.without_auto_index do
Color.create!(name: 'blue', short_name: 'b', hex: 0xFF0000)
Book.create!(
name: 'Frankenstein', author: 'Mary Shelley',
premium: false, released: true
)
end

expect(Color.search('blue')).to be_empty
expect(Book.search('Frankenstein')).to be_empty
end
end

describe '.index_documents' do
it 'updates existing documents' do
TestUtil.reset_colors!
Expand Down