From f9681e45542dc23ad0c011395df518ba73737d85 Mon Sep 17 00:00:00 2001 From: Danil Nurgaliev Date: Wed, 13 Dec 2023 18:37:52 +0300 Subject: [PATCH] Remove ruby 2.x (#911) * Remove ruby 2.x * Fix rubocop --- .github/workflows/ruby.yml | 30 ++----------------- .rubocop.yml | 2 +- CHANGELOG.md | 1 + Gemfile | 8 ++--- README.md | 2 +- chewy.gemspec | 20 ++----------- gemfiles/base.gemfile | 12 ++++++++ gemfiles/rails.5.2.activerecord.gemfile | 11 ------- gemfiles/rails.6.0.activerecord.gemfile | 11 ------- gemfiles/rails.6.1.activerecord.gemfile | 3 +- gemfiles/rails.7.0.activerecord.gemfile | 3 +- gemfiles/rails.7.1.activerecord.gemfile | 3 +- lib/chewy.rb | 2 +- lib/chewy/config.rb | 22 +++++++------- lib/chewy/fields/root.rb | 2 +- lib/chewy/index/adapter/active_record.rb | 2 +- lib/chewy/index/adapter/object.rb | 6 ++-- lib/chewy/index/adapter/orm.rb | 4 +-- lib/chewy/index/import.rb | 4 +-- lib/chewy/index/import/bulk_builder.rb | 4 +-- .../index/observe/active_record_methods.rb | 2 +- lib/chewy/index/syncer.rb | 2 +- lib/chewy/minitest/search_index_receiver.rb | 4 ++- lib/chewy/rake_helper.rb | 6 ++-- lib/chewy/rspec/update_index.rb | 6 ++-- lib/chewy/runtime/version.rb | 2 +- lib/chewy/search.rb | 10 ++++--- lib/chewy/search/parameters.rb | 6 ++-- lib/chewy/search/parameters/indices.rb | 2 +- lib/chewy/search/parameters/storage.rb | 2 +- spec/chewy/index/actions_spec.rb | 8 ++--- spec/chewy/index/import/bulk_builder_spec.rb | 6 ++-- spec/chewy/index/import_spec.rb | 6 ++-- spec/chewy/minitest/helpers_spec.rb | 2 +- .../minitest/search_index_receiver_spec.rb | 10 ++++--- spec/chewy/rake_helper_spec.rb | 8 ++--- spec/chewy/rspec/helpers_spec.rb | 2 +- .../search/pagination/kaminari_examples.rb | 2 +- spec/chewy/search/pagination/kaminari_spec.rb | 2 +- spec/chewy/strategy/active_job_spec.rb | 16 +++++----- spec/chewy/strategy/lazy_sidekiq_spec.rb | 20 ++++++------- spec/chewy/strategy/sidekiq_spec.rb | 8 ++--- spec/chewy_spec.rb | 2 +- spec/spec_helper.rb | 2 +- 44 files changed, 125 insertions(+), 163 deletions(-) create mode 100644 gemfiles/base.gemfile delete mode 100644 gemfiles/rails.5.2.activerecord.gemfile delete mode 100644 gemfiles/rails.6.0.activerecord.gemfile diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 9c518a8fa..979dadf97 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -11,39 +11,13 @@ on: ] jobs: - ruby-2: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - ruby: [2.6, 2.7] - gemfile: [rails.5.2.activerecord, rails.6.0.activerecord, rails.6.1.activerecord] - name: ${{ matrix.ruby }}-${{ matrix.gemfile }} - - env: - BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile - - steps: - - uses: actions/checkout@v4 - - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby }} - bundler-cache: true - - name: Run Elasticsearch - uses: elastic/elastic-github-actions/elasticsearch@9de0f78f306e4ebc0838f057e6b754364685e759 - with: - stack-version: 7.10.1 - port: 9250 - - name: Tests - run: bundle exec rspec - ruby-3: runs-on: ubuntu-latest strategy: fail-fast: false matrix: ruby: [ '3.0', '3.1', '3.2' ] - gemfile: [ rails.6.1.activerecord, rails.7.0.activerecord, rails.7.1.activerecord ] + gemfile: [rails.6.1.activerecord, rails.7.0.activerecord, rails.7.1.activerecord] name: ${{ matrix.ruby }}-${{ matrix.gemfile }} env: @@ -69,6 +43,6 @@ jobs: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: - ruby-version: 2.7 + ruby-version: 3.0 bundler-cache: true - run: bundle exec rubocop --format simple diff --git a/.rubocop.yml b/.rubocop.yml index ec0426c0e..e6c827173 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,7 +2,7 @@ inherit_from: .rubocop_todo.yml AllCops: NewCops: enable - TargetRubyVersion: 2.6 + TargetRubyVersion: 3.0 Layout/AccessModifierIndentation: EnforcedStyle: outdent diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cfceae25..fd938af9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### New Features * [#890](https://github.com/toptal/chewy/pull/890): Add the [`knn`](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html) option to the request. ([@jkostolansky][]) +* [#911](https://github.com/toptal/chewy/pull/911): Remove ruby 2.x. ([@konalegi][]) ### Changes diff --git a/Gemfile b/Gemfile index 969f51ad2..e838bbb9d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,5 @@ source 'https://rubygems.org' -gemspec - gem 'activerecord' gem 'activejob', require: false @@ -18,5 +16,7 @@ gem 'guard-rspec' gem 'redcarpet' gem 'yard' -gem 'rexml' if RUBY_VERSION >= '3.0.0' -gem 'ruby2_keywords' if RUBY_VERSION < '2.7' +gem 'rexml' + +eval_gemfile 'gemfiles/base.gemfile' +gemspec diff --git a/README.md b/README.md index b029c9461..bc08c32a8 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Or install it yourself as: ### Ruby -Chewy is compatible with MRI 2.6-3.0¹. +Chewy is compatible with MRI 3.0-3.2¹. > ¹ Ruby 3 is only supported with Rails 6.1 diff --git a/chewy.gemspec b/chewy.gemspec index 0e542ab56..866ed3c36 100644 --- a/chewy.gemspec +++ b/chewy.gemspec @@ -2,7 +2,7 @@ lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'chewy/version' -Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength +Gem::Specification.new do |spec| spec.name = 'chewy' spec.version = Chewy::VERSION spec.authors = ['Toptal, LLC', 'pyromaniac'] @@ -14,24 +14,10 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength spec.files = `git ls-files`.split($RS) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] - spec.add_development_dependency 'database_cleaner' - spec.add_development_dependency 'elasticsearch-extensions' - spec.add_development_dependency 'mock_redis' - spec.add_development_dependency 'rake' - spec.add_development_dependency 'rspec', '>= 3.7.0' - spec.add_development_dependency 'rspec-collection_matchers' - spec.add_development_dependency 'rspec-its' - spec.add_development_dependency 'rubocop', '1.11' - spec.add_development_dependency 'sqlite3' - spec.add_development_dependency 'timecop' - - spec.add_development_dependency 'method_source' - spec.add_development_dependency 'unparser' - - spec.add_dependency 'activesupport', '>= 5.2' + spec.add_dependency 'activesupport', '>= 5.2' # Remove with major version bump, 8.x spec.add_dependency 'elasticsearch', '>= 7.12.0', '< 7.14.0' spec.add_dependency 'elasticsearch-dsl' + spec.metadata['rubygems_mfa_required'] = 'true' end diff --git a/gemfiles/base.gemfile b/gemfiles/base.gemfile new file mode 100644 index 000000000..656b9395e --- /dev/null +++ b/gemfiles/base.gemfile @@ -0,0 +1,12 @@ +gem 'database_cleaner' +gem 'elasticsearch-extensions' +gem 'method_source' +gem 'mock_redis' +gem 'rake' +gem 'rspec', '>= 3.7.0' +gem 'rspec-collection_matchers' +gem 'rspec-its' +gem 'rubocop', '1.48' +gem 'sqlite3' +gem 'timecop' +gem 'unparser' diff --git a/gemfiles/rails.5.2.activerecord.gemfile b/gemfiles/rails.5.2.activerecord.gemfile deleted file mode 100644 index 5838db590..000000000 --- a/gemfiles/rails.5.2.activerecord.gemfile +++ /dev/null @@ -1,11 +0,0 @@ -source 'https://rubygems.org' - -gem 'activejob', '~> 5.2.0' -gem 'activerecord', '~> 5.2.0' -gem 'activesupport', '~> 5.2.0' -gem 'kaminari-core', '~> 1.1.0', require: false -gem 'parallel', require: false -gem 'rspec_junit_formatter', '~> 0.4.1' -gem 'sidekiq', require: false - -gemspec path: '../' diff --git a/gemfiles/rails.6.0.activerecord.gemfile b/gemfiles/rails.6.0.activerecord.gemfile deleted file mode 100644 index 1f0696be9..000000000 --- a/gemfiles/rails.6.0.activerecord.gemfile +++ /dev/null @@ -1,11 +0,0 @@ -source 'https://rubygems.org' - -gem 'activejob', '~> 6.0.0' -gem 'activerecord', '~> 6.0.0' -gem 'activesupport', '~> 6.0.0' -gem 'kaminari-core', '~> 1.1.0', require: false -gem 'parallel', require: false -gem 'rspec_junit_formatter', '~> 0.4.1' -gem 'sidekiq', require: false - -gemspec path: '../' diff --git a/gemfiles/rails.6.1.activerecord.gemfile b/gemfiles/rails.6.1.activerecord.gemfile index cdc7b82cc..526db972f 100644 --- a/gemfiles/rails.6.1.activerecord.gemfile +++ b/gemfiles/rails.6.1.activerecord.gemfile @@ -8,6 +8,7 @@ gem 'parallel', require: false gem 'rspec_junit_formatter', '~> 0.4.1' gem 'sidekiq', require: false -gem 'rexml' if RUBY_VERSION >= '3.0.0' +gem 'rexml' gemspec path: '../' +eval_gemfile 'base.gemfile' diff --git a/gemfiles/rails.7.0.activerecord.gemfile b/gemfiles/rails.7.0.activerecord.gemfile index e90b18cab..1176622ac 100644 --- a/gemfiles/rails.7.0.activerecord.gemfile +++ b/gemfiles/rails.7.0.activerecord.gemfile @@ -8,6 +8,7 @@ gem 'parallel', require: false gem 'rspec_junit_formatter', '~> 0.4.1' gem 'sidekiq', require: false -gem 'rexml' if RUBY_VERSION >= '3.0.0' +gem 'rexml' gemspec path: '../' +eval_gemfile 'base.gemfile' diff --git a/gemfiles/rails.7.1.activerecord.gemfile b/gemfiles/rails.7.1.activerecord.gemfile index af8ec812e..eb39c6ee2 100644 --- a/gemfiles/rails.7.1.activerecord.gemfile +++ b/gemfiles/rails.7.1.activerecord.gemfile @@ -8,6 +8,7 @@ gem 'parallel', require: false gem 'rspec_junit_formatter', '~> 0.4.1' gem 'sidekiq', require: false -gem 'rexml' if RUBY_VERSION >= '3.0.0' +gem 'rexml' gemspec path: '../' +eval_gemfile 'base.gemfile' diff --git a/lib/chewy.rb b/lib/chewy.rb index e95f25c75..cac447724 100644 --- a/lib/chewy.rb +++ b/lib/chewy.rb @@ -48,7 +48,7 @@ def try_require(path) require 'chewy/fields/base' require 'chewy/fields/root' require 'chewy/journal' -require 'chewy/railtie' if defined?(::Rails::Railtie) +require 'chewy/railtie' if defined?(Rails::Railtie) ActiveSupport.on_load(:active_record) do include Chewy::Index::Observe::ActiveRecordMethods diff --git a/lib/chewy/config.rb b/lib/chewy/config.rb index 29f71749b..4d7a695f7 100644 --- a/lib/chewy/config.rb +++ b/lib/chewy/config.rb @@ -127,17 +127,19 @@ def configuration private def yaml_settings - @yaml_settings ||= begin - if defined?(Rails::VERSION) - file = Rails.root.join('config', 'chewy.yml') + @yaml_settings ||= build_yaml_settings || {} + end - if File.exist?(file) - yaml = ERB.new(File.read(file)).result - hash = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(yaml) : YAML.load(yaml) # rubocop:disable Security/YAMLLoad - hash[Rails.env].try(:deep_symbolize_keys) if hash - end - end || {} - end + def build_yaml_settings + return unless defined?(Rails::VERSION) + + file = Rails.root.join('config', 'chewy.yml') + + return unless File.exist?(file) + + yaml = ERB.new(File.read(file)).result + hash = YAML.unsafe_load(yaml) + hash[Rails.env].try(:deep_symbolize_keys) if hash end def build_search_class(base) diff --git a/lib/chewy/fields/root.rb b/lib/chewy/fields/root.rb index c37044bf3..d6e125ba8 100644 --- a/lib/chewy/fields/root.rb +++ b/lib/chewy/fields/root.rb @@ -27,7 +27,7 @@ def mappings_hash mappings[name] end - ruby2_keywords def dynamic_template(*args) + def dynamic_template(*args) options = args.extract_options!.deep_symbolize_keys if args.first template_name = :"template_#{dynamic_templates.count.next}" diff --git a/lib/chewy/index/adapter/active_record.rb b/lib/chewy/index/adapter/active_record.rb index b69e11b9a..05b029aa7 100644 --- a/lib/chewy/index/adapter/active_record.rb +++ b/lib/chewy/index/adapter/active_record.rb @@ -6,7 +6,7 @@ module Adapter class ActiveRecord < Orm def self.accepts?(target) defined?(::ActiveRecord::Base) && ( - target.is_a?(Class) && target < ::ActiveRecord::Base || + (target.is_a?(Class) && target < ::ActiveRecord::Base) || target.is_a?(::ActiveRecord::Relation)) end diff --git a/lib/chewy/index/adapter/object.rb b/lib/chewy/index/adapter/object.rb index f8df7995c..156a10232 100644 --- a/lib/chewy/index/adapter/object.rb +++ b/lib/chewy/index/adapter/object.rb @@ -85,7 +85,7 @@ def identify(collection) # @param args [Array<#to_json>] # @option options [Integer] :batch_size import processing batch size # @return [true, false] - ruby2_keywords def import(*args, &block) + def import(*args, &block) collection, options = import_args(*args) import_objects(collection, options, &block) end @@ -113,7 +113,7 @@ def identify(collection) # end # # @see Chewy::Index::Adapter::Base#import_fields - ruby2_keywords def import_fields(*args, &block) + def import_fields(*args, &block) return enum_for(:import_fields, *args) unless block_given? options = args.extract_options! @@ -139,7 +139,7 @@ def identify(collection) # For the Object adapter returns the objects themselves in batches. # # @see Chewy::Index::Adapter::Base#import_references - ruby2_keywords def import_references(*args, &block) + def import_references(*args, &block) return enum_for(:import_references, *args) unless block_given? collection, options = import_args(*args) diff --git a/lib/chewy/index/adapter/orm.rb b/lib/chewy/index/adapter/orm.rb index 57e53e82b..c86b6d06b 100644 --- a/lib/chewy/index/adapter/orm.rb +++ b/lib/chewy/index/adapter/orm.rb @@ -72,7 +72,7 @@ def identify(collection) # # or # UsersIndex.import users.map(&:id) # user ids will be deleted from index # - ruby2_keywords def import(*args, &block) + def import(*args, &block) collection, options = import_args(*args) if !collection.is_a?(relation_class) || options[:direct_import] @@ -82,7 +82,7 @@ def identify(collection) end end - ruby2_keywords def import_fields(*args, &block) + def import_fields(*args, &block) return enum_for(:import_fields, *args) unless block_given? collection, options = import_args(*args) diff --git a/lib/chewy/index/import.rb b/lib/chewy/index/import.rb index cc50a4056..8de6a877a 100644 --- a/lib/chewy/index/import.rb +++ b/lib/chewy/index/import.rb @@ -72,7 +72,7 @@ module ClassMethods # @option options [true, false] update_failover enables full objects reimport in cases of partial update errors, `true` by default # @option options [true, Integer, Hash] parallel enables parallel import processing with the Parallel gem, accepts the number of workers or any Parallel gem acceptable options # @return [true, false] false in case of errors - ruby2_keywords def import(*args) + def import(*args) intercept_import_using_strategy(*args).blank? end @@ -83,7 +83,7 @@ module ClassMethods # in case of any import errors. # # @raise [Chewy::ImportFailed] in case of errors - ruby2_keywords def import!(*args) + def import!(*args) errors = intercept_import_using_strategy(*args) raise Chewy::ImportFailed.new(self, errors) if errors.present? diff --git a/lib/chewy/index/import/bulk_builder.rb b/lib/chewy/index/import/bulk_builder.rb index 60917b511..52e3fdcfd 100644 --- a/lib/chewy/index/import/bulk_builder.rb +++ b/lib/chewy/index/import/bulk_builder.rb @@ -162,12 +162,12 @@ def load_cache .filter(ids: {values: ids_for_cache}) .order('_doc') .pluck(:_id, :_routing, join_field) - .map do |id, routing, join| + .to_h do |id, routing, join| [ id, {routing: routing, parent_id: join['parent']} ] - end.to_h + end end def existing_routing(id) diff --git a/lib/chewy/index/observe/active_record_methods.rb b/lib/chewy/index/observe/active_record_methods.rb index ca5834efc..7f4897705 100644 --- a/lib/chewy/index/observe/active_record_methods.rb +++ b/lib/chewy/index/observe/active_record_methods.rb @@ -71,7 +71,7 @@ def initialize_chewy_callbacks end end - ruby2_keywords def update_index(type_name, *args, &block) + def update_index(type_name, *args, &block) callback_options = Observe.extract_callback_options!(args) update_proc = Observe.update_proc(type_name, *args, &block) callback = Chewy::Index::Observe::Callback.new(update_proc, callback_options) diff --git a/lib/chewy/index/syncer.rb b/lib/chewy/index/syncer.rb index 4016989da..b8365bb50 100644 --- a/lib/chewy/index/syncer.rb +++ b/lib/chewy/index/syncer.rb @@ -27,7 +27,7 @@ class Index # @see Chewy::Index::Actions::ClassMethods#sync class Syncer DEFAULT_SYNC_BATCH_SIZE = 20_000 - ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/.freeze + ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/ OUTDATED_IDS_WORKER = lambda do |outdated_sync_field_type, source_data_hash, index, total, index_data| ::Process.setproctitle("chewy [#{index}]: sync outdated calculation (#{::Parallel.worker_number + 1}/#{total})") if index index_data.each_with_object([]) do |(id, index_sync_value), result| diff --git a/lib/chewy/minitest/search_index_receiver.rb b/lib/chewy/minitest/search_index_receiver.rb index d8f0bd4f8..69c95b55e 100644 --- a/lib/chewy/minitest/search_index_receiver.rb +++ b/lib/chewy/minitest/search_index_receiver.rb @@ -6,6 +6,8 @@ # The class will capture the data from the *param on the Chewy::Index.bulk method and # aggregate the data for test analysis. class SearchIndexReceiver + MUTATION_FOR_CLASS = Struct.new(:indexes, :deletes, keyword_init: true) + def initialize @mutations = {} end @@ -71,6 +73,6 @@ def updated_indexes # @param index [Chewy::Index] the index to fetch. # @return [#indexes, #deletes] an object with a list of indexes and a list of deletes. def mutation_for(index) - @mutations[index] ||= OpenStruct.new(indexes: [], deletes: []) + @mutations[index] ||= MUTATION_FOR_CLASS.new(indexes: [], deletes: []) end end diff --git a/lib/chewy/rake_helper.rb b/lib/chewy/rake_helper.rb index 0e5cb706d..a59fa02a3 100644 --- a/lib/chewy/rake_helper.rb +++ b/lib/chewy/rake_helper.rb @@ -347,9 +347,9 @@ def warn_missing_index(output) return if journal_exists? output.puts "############################################################\n" \ - "WARN: You are risking to lose some changes during the reset.\n" \ - " Please consider enabling journaling.\n" \ - " See https://github.com/toptal/chewy#journaling\n" \ + "WARN: You are risking to lose some changes during the reset.\n " \ + "Please consider enabling journaling.\n " \ + "See https://github.com/toptal/chewy#journaling\n" \ '############################################################' end diff --git a/lib/chewy/rspec/update_index.rb b/lib/chewy/rspec/update_index.rb index bd6b2a3f2..3a16ece2a 100644 --- a/lib/chewy/rspec/update_index.rb +++ b/lib/chewy/rspec/update_index.rb @@ -108,7 +108,7 @@ def supports_block_expectations? params_matcher = @no_refresh ? has_entry(refresh: false) : any_parameters Chewy::Index::Import::BulkRequest.stubs(:new).with(index, params_matcher).returns(mock_bulk_request) else - mocked_already = ::RSpec::Mocks.space.proxy_for(Chewy::Index::Import::BulkRequest).method_double_if_exists_for_message(:new) + mocked_already = RSpec::Mocks.space.proxy_for(Chewy::Index::Import::BulkRequest).method_double_if_exists_for_message(:new) allow(Chewy::Index::Import::BulkRequest).to receive(:new).and_call_original unless mocked_already params_matcher = @no_refresh ? hash_including(refresh: false) : any_args allow(Chewy::Index::Import::BulkRequest).to receive(:new).with(index, params_matcher).and_return(mock_bulk_request) @@ -220,7 +220,7 @@ def extract_documents(*args) expected_count = options[:times] || options[:count] expected_attributes = (options[:with] || options[:attributes] || {}).deep_symbolize_keys - args.flatten.map do |document| + args.flatten.to_h do |document| id = document.respond_to?(:id) ? document.id.to_s : document.to_s [id, { document: document, @@ -229,7 +229,7 @@ def extract_documents(*args) real_count: 0, real_attributes: {} }] - end.to_h + end end def compare_attributes(expected, real) diff --git a/lib/chewy/runtime/version.rb b/lib/chewy/runtime/version.rb index 1154ec0dc..a96880544 100644 --- a/lib/chewy/runtime/version.rb +++ b/lib/chewy/runtime/version.rb @@ -5,7 +5,7 @@ class Version attr_reader :major, :minor, :patch def initialize(version) - @major, @minor, @patch = *(version.to_s.split('.', 3) + [0] * 3).first(3).map(&:to_i) + @major, @minor, @patch = *(version.to_s.split('.', 3) + ([0] * 3)).first(3).map(&:to_i) end def to_s diff --git a/lib/chewy/search.rb b/lib/chewy/search.rb index 78dc59a1a..be2ba29a3 100644 --- a/lib/chewy/search.rb +++ b/lib/chewy/search.rb @@ -56,7 +56,7 @@ def search_string(query, options = {}) # # @example # PlacesIndex.query(match: {name: 'Moscow'}) - ruby2_keywords def method_missing(name, *args, &block) + def method_missing(name, *args, &block) if search_class::DELEGATED_METHODS.include?(name) all.send(name, *args, &block) else @@ -84,10 +84,12 @@ def build_search_class(base) def delegate_scoped(source, destination, methods) methods.each do |method| destination.class_eval do - define_method method do |*args, &block| - scoping { source.public_send(method, *args, &block) } + define_method method do |*args, **kwargs, &block| + scoping do + source.public_send(method, *args, **kwargs, &block) + end end - ruby2_keywords method + method end end end diff --git a/lib/chewy/search/parameters.rb b/lib/chewy/search/parameters.rb index 1f15e6168..3f55e0384 100644 --- a/lib/chewy/search/parameters.rb +++ b/lib/chewy/search/parameters.rb @@ -1,5 +1,5 @@ -Dir.glob(File.join(File.dirname(__FILE__), 'parameters', 'concerns', '*.rb')).sort.each { |f| require f } -Dir.glob(File.join(File.dirname(__FILE__), 'parameters', '*.rb')).sort.each { |f| require f } +Dir.glob(File.join(File.dirname(__FILE__), 'parameters', 'concerns', '*.rb')).each { |f| require f } +Dir.glob(File.join(File.dirname(__FILE__), 'parameters', '*.rb')).each { |f| require f } module Chewy module Search @@ -53,7 +53,7 @@ def initialize(initial = {}, **kinitial) # @param other [Object] any object # @return [true, false] def ==(other) - super || other.is_a?(self.class) && compare_storages(other) + super || (other.is_a?(self.class) && compare_storages(other)) end # Clones the specified storage, performs the operation diff --git a/lib/chewy/search/parameters/indices.rb b/lib/chewy/search/parameters/indices.rb index dc3dc28ca..d3a59b292 100644 --- a/lib/chewy/search/parameters/indices.rb +++ b/lib/chewy/search/parameters/indices.rb @@ -17,7 +17,7 @@ class Indices < Storage # @param other [Chewy::Search::Parameters::Storage] any storage instance # @return [true, false] the result of comparison def ==(other) - super || other.class == self.class && other.render == render + super || (other.class == self.class && other.render == render) end # Just adds indices to indices. diff --git a/lib/chewy/search/parameters/storage.rb b/lib/chewy/search/parameters/storage.rb index 6fe00d601..e0effe4c3 100644 --- a/lib/chewy/search/parameters/storage.rb +++ b/lib/chewy/search/parameters/storage.rb @@ -35,7 +35,7 @@ def initialize(value = nil) # @param other [Chewy::Search::Parameters::Storage] any storage instance # @return [true, false] the result of comparision def ==(other) - super || other.class == self.class && other.value == value + super || (other.class == self.class && other.value == value) end # Replaces current value with normalized provided one. Doesn't diff --git a/spec/chewy/index/actions_spec.rb b/spec/chewy/index/actions_spec.rb index 0ca812d92..4a0d69893 100644 --- a/spec/chewy/index/actions_spec.rb +++ b/spec/chewy/index/actions_spec.rb @@ -610,7 +610,7 @@ specify 'with journal application' do cities p 'cities created1' - ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base) + ActiveRecord::Base.connection.close if defined?(ActiveRecord::Base) [ parallel_update, Thread.new do @@ -619,7 +619,7 @@ p 'end reset1' end ].map(&:join) - ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base) + ActiveRecord::Base.connection.reconnect! if defined?(ActiveRecord::Base) p 'expect1' expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 NewName1], %w[2 Name2], %w[3 NewName3]) p 'end expect1' @@ -628,7 +628,7 @@ specify 'without journal application' do cities p 'cities created2' - ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base) + ActiveRecord::Base.connection.close if defined?(ActiveRecord::Base) [ parallel_update, Thread.new do @@ -637,7 +637,7 @@ p 'end reset2' end ].map(&:join) - ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base) + ActiveRecord::Base.connection.reconnect! if defined?(ActiveRecord::Base) p 'expect2' expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 Name1], %w[2 Name2], %w[3 Name3]) p 'end expect2' diff --git a/spec/chewy/index/import/bulk_builder_spec.rb b/spec/chewy/index/import/bulk_builder_spec.rb index dd63c442c..1b2eb3a82 100644 --- a/spec/chewy/index/import/bulk_builder_spec.rb +++ b/spec/chewy/index/import/bulk_builder_spec.rb @@ -132,7 +132,7 @@ def derived before do stub_index(:cities) do crutch :names do |collection| - collection.map { |item| [item.id, "Name#{item.id}"] }.to_h + collection.to_h { |item| [item.id, "Name#{item.id}"] } end field :name, value: ->(o, c) { c.names[o.id] } @@ -198,7 +198,7 @@ def derived index_scope Comment crutch :content_with_crutches do |collection| # collection here is a current batch of products - collection.map { |comment| [comment.id, "[crutches] #{comment.content}"] }.to_h + collection.to_h { |comment| [comment.id, "[crutches] #{comment.content}"] } end field :content @@ -272,7 +272,7 @@ def root(comment) default_import_options raw_import: ->(hash) { SimpleComment.new(hash) } crutch :content_with_crutches do |collection| # collection here is a current batch of products - collection.map { |comment| [comment.id, "[crutches] #{comment.content}"] }.to_h + collection.to_h { |comment| [comment.id, "[crutches] #{comment.content}"] } end field :content diff --git a/spec/chewy/index/import_spec.rb b/spec/chewy/index/import_spec.rb index abc0152f3..16593113a 100644 --- a/spec/chewy/index/import_spec.rb +++ b/spec/chewy/index/import_spec.rb @@ -215,8 +215,8 @@ def subscribe_notification payload = subscribe_notification import dummy_cities, batch_size: 2 expect(payload).to eq(index: CitiesIndex, - errors: {index: {mapper_parsing_exception => %w[1 2 3]}}, - import: {index: 3}) + errors: {index: {mapper_parsing_exception => %w[1 2 3]}}, + import: {index: 3}) end end end @@ -567,7 +567,7 @@ def imported_comments before do stub_index(:cities) do crutch :names do |collection| - collection.map { |o| [o.name, "#{o.name}42"] }.to_h + collection.to_h { |o| [o.name, "#{o.name}42"] } end field :name, value: ->(o, c) { c.names[o.name] } field :rating diff --git a/spec/chewy/minitest/helpers_spec.rb b/spec/chewy/minitest/helpers_spec.rb index 41353b928..4d6f2d30e 100644 --- a/spec/chewy/minitest/helpers_spec.rb +++ b/spec/chewy/minitest/helpers_spec.rb @@ -10,7 +10,7 @@ def assert_includes(haystack, needle, _comment) expect(haystack).to include(needle) end - include ::Chewy::Minitest::Helpers + include Chewy::Minitest::Helpers def assert_equal(expected, actual, message) raise message unless expected == actual diff --git a/spec/chewy/minitest/search_index_receiver_spec.rb b/spec/chewy/minitest/search_index_receiver_spec.rb index 213d46de8..d93b97f41 100644 --- a/spec/chewy/minitest/search_index_receiver_spec.rb +++ b/spec/chewy/minitest/search_index_receiver_spec.rb @@ -24,6 +24,8 @@ def parse_request(request) SearchIndexReceiver.new end + let(:dummy_class) { Struct.new(:id) } + before do stub_index(:dummies) do root value: ->(_o) { {} } @@ -82,12 +84,12 @@ def parse_request(request) end specify 'validates that an object was indexed' do - dummy = OpenStruct.new(id: 1) + dummy = dummy_class.new(1) expect(receiver.indexed?(dummy, DummiesIndex)).to be(true) end specify 'doesn\'t validate than unindexed objects were indexed' do - dummy = OpenStruct.new(id: 2) + dummy = dummy_class.new(2) expect(receiver.indexed?(dummy, DummiesIndex)).to be(false) end end @@ -98,12 +100,12 @@ def parse_request(request) end specify 'validates than an object was deleted' do - dummy = OpenStruct.new(id: 1) + dummy = dummy_class.new(1) expect(receiver.deleted?(dummy, DummiesIndex)).to be(true) end specify 'doesn\'t validate than undeleted objects were deleted' do - dummy = OpenStruct.new(id: 2) + dummy = dummy_class.new(2) expect(receiver.deleted?(dummy, DummiesIndex)).to be(false) end end diff --git a/spec/chewy/rake_helper_spec.rb b/spec/chewy/rake_helper_spec.rb index 510e85f1a..f84ad129a 100644 --- a/spec/chewy/rake_helper_spec.rb +++ b/spec/chewy/rake_helper_spec.rb @@ -105,10 +105,10 @@ expect { described_class.reset(only: [CitiesIndex], output: output) } .to update_index(CitiesIndex) expect(output.string).to include( - "############################################################\n"\ - "WARN: You are risking to lose some changes during the reset.\n" \ - " Please consider enabling journaling.\n" \ - ' See https://github.com/toptal/chewy#journaling' + "############################################################\n" \ + "WARN: You are risking to lose some changes during the reset.\n " \ + "Please consider enabling journaling.\n " \ + 'See https://github.com/toptal/chewy#journaling' ) end end diff --git a/spec/chewy/rspec/helpers_spec.rb b/spec/chewy/rspec/helpers_spec.rb index 0b3938dcc..1d2e6cfd3 100644 --- a/spec/chewy/rspec/helpers_spec.rb +++ b/spec/chewy/rspec/helpers_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe :rspec_helper do - include ::Chewy::Rspec::Helpers + include Chewy::Rspec::Helpers before do stub_model(:city) diff --git a/spec/chewy/search/pagination/kaminari_examples.rb b/spec/chewy/search/pagination/kaminari_examples.rb index ccb033bfd..08a285716 100644 --- a/spec/chewy/search/pagination/kaminari_examples.rb +++ b/spec/chewy/search/pagination/kaminari_examples.rb @@ -24,7 +24,7 @@ let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } } before { ProductsIndex.import!(data.map { |h| double(h) }) } - before { allow(::Kaminari.config).to receive_messages(default_per_page: 3) } + before { allow(Kaminari.config).to receive_messages(default_per_page: 3) } describe '#per, #page' do specify { expect(search.map { |e| e.attributes.except(*except_fields) }).to match_array(data) } diff --git a/spec/chewy/search/pagination/kaminari_spec.rb b/spec/chewy/search/pagination/kaminari_spec.rb index c75eacaa8..bd117d4d6 100644 --- a/spec/chewy/search/pagination/kaminari_spec.rb +++ b/spec/chewy/search/pagination/kaminari_spec.rb @@ -6,7 +6,7 @@ let(:data) { Array.new(12) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } } before { ProductsIndex.import!(data.map { |h| double(h) }) } - before { allow(::Kaminari.config).to receive_messages(default_per_page: 17) } + before { allow(Kaminari.config).to receive_messages(default_per_page: 17) } specify { expect(search.objects.class).to eq(Kaminari::PaginatableArray) } specify { expect(search.objects.total_count).to eq(12) } diff --git a/spec/chewy/strategy/active_job_spec.rb b/spec/chewy/strategy/active_job_spec.rb index 71d9563d1..7fda880d4 100644 --- a/spec/chewy/strategy/active_job_spec.rb +++ b/spec/chewy/strategy/active_job_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -if defined?(::ActiveJob) +if defined?(ActiveJob) describe Chewy::Strategy::ActiveJob do around do |example| active_job_settings = Chewy.settings[:active_job] @@ -9,12 +9,12 @@ Chewy.settings[:active_job] = active_job_settings end before(:all) do - ::ActiveJob::Base.logger = Chewy.logger + ActiveJob::Base.logger = Chewy.logger end before do - ::ActiveJob::Base.queue_adapter = :test - ::ActiveJob::Base.queue_adapter.enqueued_jobs.clear - ::ActiveJob::Base.queue_adapter.performed_jobs.clear + ActiveJob::Base.queue_adapter = :test + ActiveJob::Base.queue_adapter.enqueued_jobs.clear + ActiveJob::Base.queue_adapter.performed_jobs.clear end before do @@ -39,7 +39,7 @@ Chewy.strategy(:active_job) do [city, other_city].map(&:save!) end - enqueued_job = ::ActiveJob::Base.queue_adapter.enqueued_jobs.first + enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.first expect(enqueued_job[:job]).to eq(Chewy::Strategy::ActiveJob::Worker) expect(enqueued_job[:queue]).to eq('low') end @@ -48,12 +48,12 @@ Chewy.strategy(:active_job) do [city, other_city].map(&:save!) end - enqueued_job = ::ActiveJob::Base.queue_adapter.enqueued_jobs.first + enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.first expect(enqueued_job[:queue]).to eq('low') end specify do - ::ActiveJob::Base.queue_adapter = :inline + ActiveJob::Base.queue_adapter = :inline expect { [city, other_city].map(&:save!) } .to update_index(CitiesIndex, strategy: :active_job) .and_reindex(city, other_city).only diff --git a/spec/chewy/strategy/lazy_sidekiq_spec.rb b/spec/chewy/strategy/lazy_sidekiq_spec.rb index e221c67ab..4078fb1a3 100644 --- a/spec/chewy/strategy/lazy_sidekiq_spec.rb +++ b/spec/chewy/strategy/lazy_sidekiq_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -if defined?(::Sidekiq) +if defined?(Sidekiq) require 'sidekiq/testing' describe Chewy::Strategy::LazySidekiq do @@ -10,7 +10,7 @@ Chewy.strategy(:bypass) { example.run } Chewy.settings[:sidekiq] = sidekiq_settings end - before { ::Sidekiq::Worker.clear_all } + before { Sidekiq::Worker.clear_all } context 'strategy' do before do @@ -32,14 +32,14 @@ end it 'updates indices asynchronously on record save' do - expect(::Sidekiq::Client).to receive(:push) + expect(Sidekiq::Client).to receive(:push) .with(hash_including( 'class' => Chewy::Strategy::LazySidekiq::IndicesUpdateWorker, 'queue' => 'low' )) .and_call_original .once - ::Sidekiq::Testing.inline! do + Sidekiq::Testing.inline! do expect { [city, other_city].map(&:save!) } .to update_index(CitiesIndex, strategy: :lazy_sidekiq) .and_reindex(city, other_city).only @@ -47,12 +47,12 @@ end it 'updates indices asynchronously with falling back to sidekiq strategy on record destroy' do - expect(::Sidekiq::Client).not_to receive(:push) + expect(Sidekiq::Client).not_to receive(:push) .with(hash_including( 'class' => Chewy::Strategy::LazySidekiq::IndicesUpdateWorker, 'queue' => 'low' )) - expect(::Sidekiq::Client).to receive(:push) + expect(Sidekiq::Client).to receive(:push) .with(hash_including( 'class' => Chewy::Strategy::Sidekiq::Worker, 'queue' => 'low', @@ -60,7 +60,7 @@ )) .and_call_original .once - ::Sidekiq::Testing.inline! do + Sidekiq::Testing.inline! do expect { [city, other_city].map(&:destroy) }.to update_index(CitiesIndex, strategy: :sidekiq) end end @@ -71,7 +71,7 @@ expect(other_city).to receive(:run_chewy_callbacks).and_call_original expect do - ::Sidekiq::Testing.inline! do + Sidekiq::Testing.inline! do Chewy::Strategy::LazySidekiq::IndicesUpdateWorker.new.perform({'City' => [city.id, other_city.id]}) end end.to update_index(CitiesIndex).and_reindex(city, other_city).only @@ -88,7 +88,7 @@ expect(other_city).to receive(:run_chewy_callbacks).and_call_original expect do - ::Sidekiq::Testing.inline! do + Sidekiq::Testing.inline! do Chewy::Strategy::LazySidekiq::IndicesUpdateWorker.new.perform({'City' => [city.id, other_city.id]}) end end.to update_index(CitiesIndex).and_reindex(city, other_city).only.no_refresh @@ -97,7 +97,7 @@ end context 'integration' do - around { |example| ::Sidekiq::Testing.inline! { example.run } } + around { |example| Sidekiq::Testing.inline! { example.run } } let(:update_condition) { true } diff --git a/spec/chewy/strategy/sidekiq_spec.rb b/spec/chewy/strategy/sidekiq_spec.rb index 943ac6f7e..13ca55e90 100644 --- a/spec/chewy/strategy/sidekiq_spec.rb +++ b/spec/chewy/strategy/sidekiq_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -if defined?(::Sidekiq) +if defined?(Sidekiq) require 'sidekiq/testing' describe Chewy::Strategy::Sidekiq do @@ -10,7 +10,7 @@ Chewy.strategy(:bypass) { example.run } Chewy.settings[:sidekiq] = sidekiq_settings end - before { ::Sidekiq::Worker.clear_all } + before { Sidekiq::Worker.clear_all } before do stub_model(:city) do update_index('cities') { self } @@ -30,8 +30,8 @@ end specify do - expect(::Sidekiq::Client).to receive(:push).with(hash_including('queue' => 'low')).and_call_original - ::Sidekiq::Testing.inline! do + expect(Sidekiq::Client).to receive(:push).with(hash_including('queue' => 'low')).and_call_original + Sidekiq::Testing.inline! do expect { [city, other_city].map(&:save!) } .to update_index(CitiesIndex, strategy: :sidekiq) .and_reindex(city, other_city).only diff --git a/spec/chewy_spec.rb b/spec/chewy_spec.rb index 4dcf2718a..3023b71e4 100644 --- a/spec/chewy_spec.rb +++ b/spec/chewy_spec.rb @@ -59,7 +59,7 @@ Chewy.current[:chewy_client] = nil allow(Chewy).to receive_messages(configuration: {transport_options: {proc: faraday_block}}) - allow(::Elasticsearch::Client).to receive(:new).with(expected_client_config) do |*_args, &passed_block| + allow(Elasticsearch::Client).to receive(:new).with(expected_client_config) do |*_args, &passed_block| # RSpec's `with(..., &block)` was used previously, but doesn't actually do # any verification of the passed block (even of its presence). expect(passed_block.source_location).to eq(faraday_block.source_location) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 48e901d6c..be66332f8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,7 +9,7 @@ require 'timecop' -Kaminari::Hooks.init if defined?(::Kaminari::Hooks) +Kaminari::Hooks.init if defined?(Kaminari::Hooks) require 'support/fail_helpers' require 'support/class_helpers'