chewy 7.5.0 → 7.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b939ad8c84f1499caf873e2db0ba62794b785da0297604a38404c0f4f68a930
4
- data.tar.gz: bba3a09f52a65c3433b385c227c2f900645b145fba18e11737d7a9b2958d51ef
3
+ metadata.gz: 64f061f379d740c56b7af2678ec03e3c80c1bd4c072b2aa3f68bf7803d4bc0fe
4
+ data.tar.gz: 6226557f9cec700d91473f36bb8623ff5bbf838211463188ac0f83eaf0ab227d
5
5
  SHA512:
6
- metadata.gz: cb4f984274557d91d9d4d08306516ca4b0592bf97de3b3432585bfa9da14906d64d809da7803f19f36ba908cd8623843398e30bc78db22951cfb73015bf60a4f
7
- data.tar.gz: 02ecef485783e555bd112240d74c75832779b8415aded37b8eb527357729e78053caead1615928fd1d54fa053a146779bb281d2bdf9f26f448c1ee680fea9bee
6
+ metadata.gz: 8cb278cc5fa838a97e0c9fb94c90ea08d4a84effb041a809f850a267514c61ce78f1a919a8a0fdc5119ca968e217fe9b0aa38c307f9589f8413501f690efb314
7
+ data.tar.gz: 50ad4271e4948b22b626202843be7107fcc720fd3da490c14a372ca018ab0e35c0e151d5dc5b55f23b7c382a0d19cf72f7cc63e40a2dc1fc0ca286c1de717941
data/.rubocop.yml CHANGED
@@ -59,3 +59,6 @@ Metrics/ModuleLength:
59
59
  Exclude:
60
60
  - 'lib/chewy/rake_helper.rb'
61
61
  - '**/*_spec.rb'
62
+
63
+ Style/ArgumentsForwarding:
64
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -8,7 +8,17 @@
8
8
 
9
9
  ### Bugs Fixed
10
10
 
11
- ## 7.5.0 (2023-01-15)
11
+ ## 7.5.1 (2024-01-30)
12
+
13
+ ### New Features
14
+
15
+ * [#925](https://github.com/toptal/chewy/pull/925): Add configuration option for default scope cleanup behavior. ([@barthez][])
16
+
17
+ ### Changes
18
+
19
+ ### Bugs Fixed
20
+
21
+ ## 7.5.0 (2024-01-15)
12
22
 
13
23
  ### New Features
14
24
 
data/README.md CHANGED
@@ -1298,6 +1298,24 @@ While using the `before_es_request_filter`, please consider the following:
1298
1298
  * The return value of the proc is disregarded. This filter is intended for inspection or modification of the query rather than generating a response.
1299
1299
  * Any exception raised inside the callback will propagate upward and halt the execution of the query. It is essential to handle potential errors adequately to ensure the stability of your search functionality.
1300
1300
 
1301
+ ### Import scope clean-up behavior
1302
+
1303
+ Whenever you set the `import_scope` for the index, in the case of ActiveRecord,
1304
+ options for order, offset and limit will be removed. You can set the behavior of
1305
+ chewy, before the clean-up itself.
1306
+
1307
+ The default behavior is a warning sent to the Chewy logger (`:warn`). Another more
1308
+ restrictive option is raising an exception (`:raise`). Both options have a
1309
+ negative impact on performance since verifying whether the code uses any of
1310
+ these options requires building AREL query.
1311
+
1312
+ To avoid the loading time impact, you can ignore the check (`:ignore`) before
1313
+ the clean-up.
1314
+
1315
+ ```
1316
+ Chewy.import_scope_cleanup_behavior = :ignore
1317
+ ```
1318
+
1301
1319
  ## Contributing
1302
1320
 
1303
1321
  1. Fork it (http://github.com/toptal/chewy/fork)
@@ -6,7 +6,7 @@ gem 'rake'
6
6
  gem 'rspec', '>= 3.7.0'
7
7
  gem 'rspec-collection_matchers'
8
8
  gem 'rspec-its'
9
- gem 'rubocop', '1.48'
9
+ gem 'rubocop', '1.60.1'
10
10
  gem 'sqlite3'
11
11
  gem 'timecop'
12
12
  gem 'unparser'
data/lib/chewy/config.rb CHANGED
@@ -40,7 +40,10 @@ module Chewy
40
40
  # Default field type for any field in any Chewy type. Defaults to 'text'.
41
41
  :default_field_type,
42
42
  # Callback called on each search request to be done into ES
43
- :before_es_request_filter
43
+ :before_es_request_filter,
44
+ # Behavior when import scope for index includes order, offset or limit.
45
+ # Can be :ignore, :warn, :raise. Defaults to :warn
46
+ :import_scope_cleanup_behavior
44
47
 
45
48
  attr_reader :transport_logger, :transport_tracer,
46
49
  # Chewy search request DSL base class, used by every index.
@@ -62,6 +65,7 @@ module Chewy
62
65
  @indices_path = 'app/chewy'
63
66
  @default_root_options = {}
64
67
  @default_field_type = 'text'.freeze
68
+ @import_scope_cleanup_behavior = :warn
65
69
  @search_class = build_search_class(Chewy::Search::Request)
66
70
  end
67
71
 
data/lib/chewy/errors.rb CHANGED
@@ -7,7 +7,7 @@ module Chewy
7
7
 
8
8
  class UndefinedUpdateStrategy < Error
9
9
  def initialize(_type)
10
- super <<-MESSAGE
10
+ super(<<-MESSAGE)
11
11
  Index update strategy is undefined for current context.
12
12
  Please wrap your code with `Chewy.strategy(:strategy_name) block.`
13
13
  MESSAGE
@@ -27,7 +27,7 @@ module Chewy
27
27
  message << " on #{documents.count} documents: #{documents}\n"
28
28
  end
29
29
  end
30
- super message
30
+ super(message)
31
31
  end
32
32
  end
33
33
 
@@ -36,4 +36,7 @@ module Chewy
36
36
  super("`#{join_field_type}` set for the join field `#{join_field_name}` is not on the :relations list (#{relations})")
37
37
  end
38
38
  end
39
+
40
+ class ImportScopeCleanupError < Error
41
+ end
39
42
  end
@@ -13,9 +13,19 @@ module Chewy
13
13
  private
14
14
 
15
15
  def cleanup_default_scope!
16
- if Chewy.logger && (@default_scope.arel.orders.present? ||
16
+ behavior = Chewy.config.import_scope_cleanup_behavior
17
+
18
+ if behavior != :ignore && (@default_scope.arel.orders.present? ||
17
19
  @default_scope.arel.limit.present? || @default_scope.arel.offset.present?)
18
- Chewy.logger.warn('Default type scope order, limit and offset are ignored and will be nullified')
20
+ if behavior == :warn && Chewy.logger
21
+ gem_dir = File.realpath('../..', __dir__)
22
+ source = caller.grep_v(Regexp.new(gem_dir)).first
23
+ Chewy.logger.warn(
24
+ "Default type scope order, limit and offset are ignored and will be nullified (called from: #{source})"
25
+ )
26
+ elsif behavior == :raise
27
+ raise ImportScopeCleanupError, 'Default type scope order, limit and offset are ignored and will be nullified'
28
+ end
19
29
  end
20
30
 
21
31
  @default_scope = @default_scope.reorder(nil).limit(nil).offset(nil)
@@ -320,11 +320,7 @@ module Chewy
320
320
  all_indexes
321
321
  end
322
322
 
323
- indexes = if except.present?
324
- indexes - normalize_indexes(Array.wrap(except))
325
- else
326
- indexes
327
- end
323
+ indexes -= normalize_indexes(Array.wrap(except)) if except.present?
328
324
 
329
325
  indexes.sort_by(&:derivable_name)
330
326
  end
data/lib/chewy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chewy
2
- VERSION = '7.5.0'.freeze
2
+ VERSION = '7.5.1'.freeze
3
3
  end
@@ -35,6 +35,68 @@ describe Chewy::Index::Adapter::ActiveRecord, :active_record do
35
35
  specify { expect(described_class.new(City.where(rating: 10)).default_scope).to eq(City.where(rating: 10)) }
36
36
  end
37
37
 
38
+ describe '.new' do
39
+ context 'with logger' do
40
+ let(:test_logger) { Logger.new('/dev/null') }
41
+ let(:default_scope_behavior) { :warn }
42
+
43
+ around do |example|
44
+ previous_logger = Chewy.logger
45
+ Chewy.logger = test_logger
46
+
47
+ previous_default_scope_behavior = Chewy.config.import_scope_cleanup_behavior
48
+ Chewy.config.import_scope_cleanup_behavior = default_scope_behavior
49
+
50
+ example.run
51
+ ensure
52
+ Chewy.logger = previous_logger
53
+ Chewy.config.import_scope_cleanup_behavior = previous_default_scope_behavior
54
+ end
55
+
56
+ specify do
57
+ expect(test_logger).to receive(:warn)
58
+ described_class.new(City.order(:id))
59
+ end
60
+
61
+ specify do
62
+ expect(test_logger).to receive(:warn)
63
+ described_class.new(City.offset(10))
64
+ end
65
+
66
+ specify do
67
+ expect(test_logger).to receive(:warn)
68
+ described_class.new(City.limit(10))
69
+ end
70
+
71
+ context 'ignore import scope warning' do
72
+ let(:default_scope_behavior) { :ignore }
73
+
74
+ specify do
75
+ expect(test_logger).not_to receive(:warn)
76
+ described_class.new(City.order(:id))
77
+ end
78
+
79
+ specify do
80
+ expect(test_logger).not_to receive(:warn)
81
+ described_class.new(City.offset(10))
82
+ end
83
+
84
+ specify do
85
+ expect(test_logger).not_to receive(:warn)
86
+ described_class.new(City.limit(10))
87
+ end
88
+ end
89
+
90
+ context 'raise exception on import scope with order/limit/offset' do
91
+ let(:default_scope_behavior) { :raise }
92
+
93
+ specify { expect { described_class.new(City.order(:id)) }.to raise_error(Chewy::ImportScopeCleanupError) }
94
+ specify { expect { described_class.new(City.limit(10)) }.to raise_error(Chewy::ImportScopeCleanupError) }
95
+ specify { expect { described_class.new(City.offset(10)) }.to raise_error(Chewy::ImportScopeCleanupError) }
96
+ end
97
+ end
98
+ end
99
+
38
100
  describe '#type_name' do
39
101
  specify { expect(described_class.new(City).type_name).to eq('city') }
40
102
  specify { expect(described_class.new(City.order(:id)).type_name).to eq('city') }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chewy
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.5.0
4
+ version: 7.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toptal, LLC
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-01-15 00:00:00.000000000 Z
12
+ date: 2024-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -320,7 +320,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
320
320
  - !ruby/object:Gem::Version
321
321
  version: '0'
322
322
  requirements: []
323
- rubygems_version: 3.4.10
323
+ rubygems_version: 3.2.33
324
324
  signing_key:
325
325
  specification_version: 4
326
326
  summary: Elasticsearch ODM client wrapper