diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e70bcb1..e54d7a5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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. @@ -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. @@ -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. @@ -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 @@ -174,7 +168,7 @@ RSpec/MultipleDescribes: Exclude: - 'spec/search_spec.rb' -# Offense count: 2 +# Offense count: 3 RSpec/NestedGroups: Max: 4 @@ -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. @@ -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 diff --git a/README.md b/README.md index 2b2a928..f761e24 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/meilisearch-rails.rb b/lib/meilisearch-rails.rb index c8dda0e..e528db8 100644 --- a/lib/meilisearch-rails.rb +++ b/lib/meilisearch-rails.rb @@ -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) diff --git a/lib/meilisearch/rails/configuration.rb b/lib/meilisearch/rails/configuration.rb index 6912b35..592ee2d 100644 --- a/lib/meilisearch/rails/configuration.rb +++ b/lib/meilisearch/rails/configuration.rb @@ -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 end end end diff --git a/spec/model_methods_spec.rb b/spec/model_methods_spec.rb index 9853aaf..eeadf86 100644 --- a/spec/model_methods_spec.rb +++ b/spec/model_methods_spec.rb @@ -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!