chewy 6.0.0 → 7.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.github/dependabot.yml +42 -0
  7. data/.github/workflows/ruby.yml +48 -0
  8. data/.rubocop.yml +16 -8
  9. data/.rubocop_todo.yml +110 -22
  10. data/CHANGELOG.md +385 -105
  11. data/CODE_OF_CONDUCT.md +14 -0
  12. data/CONTRIBUTING.md +63 -0
  13. data/Gemfile +4 -10
  14. data/Guardfile +3 -1
  15. data/README.md +494 -275
  16. data/chewy.gemspec +5 -20
  17. data/gemfiles/base.gemfile +12 -0
  18. data/gemfiles/rails.6.1.activerecord.gemfile +10 -15
  19. data/gemfiles/rails.7.0.activerecord.gemfile +14 -0
  20. data/gemfiles/rails.7.1.activerecord.gemfile +14 -0
  21. data/lib/chewy/config.rb +58 -50
  22. data/lib/chewy/elastic_client.rb +31 -0
  23. data/lib/chewy/errors.rb +7 -10
  24. data/lib/chewy/fields/base.rb +79 -13
  25. data/lib/chewy/fields/root.rb +4 -14
  26. data/lib/chewy/index/actions.rb +54 -37
  27. data/lib/chewy/{type → index}/adapter/active_record.rb +30 -6
  28. data/lib/chewy/{type → index}/adapter/base.rb +2 -3
  29. data/lib/chewy/{type → index}/adapter/object.rb +27 -31
  30. data/lib/chewy/{type → index}/adapter/orm.rb +17 -18
  31. data/lib/chewy/index/aliases.rb +14 -5
  32. data/lib/chewy/index/crutch.rb +40 -0
  33. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  34. data/lib/chewy/{type → index}/import/bulk_request.rb +6 -7
  35. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  36. data/lib/chewy/{type → index}/import/routine.rb +18 -17
  37. data/lib/chewy/{type → index}/import.rb +76 -32
  38. data/lib/chewy/{type → index}/mapping.rb +29 -34
  39. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  40. data/lib/chewy/index/observe/callback.rb +34 -0
  41. data/lib/chewy/index/observe.rb +17 -0
  42. data/lib/chewy/index/specification.rb +1 -0
  43. data/lib/chewy/{type → index}/syncer.rb +59 -59
  44. data/lib/chewy/{type → index}/witchcraft.rb +11 -7
  45. data/lib/chewy/{type → index}/wrapper.rb +2 -2
  46. data/lib/chewy/index.rb +67 -94
  47. data/lib/chewy/journal.rb +25 -14
  48. data/lib/chewy/log_subscriber.rb +5 -1
  49. data/lib/chewy/minitest/helpers.rb +86 -13
  50. data/lib/chewy/minitest/search_index_receiver.rb +24 -26
  51. data/lib/chewy/railtie.rb +6 -20
  52. data/lib/chewy/rake_helper.rb +169 -113
  53. data/lib/chewy/rspec/build_query.rb +12 -0
  54. data/lib/chewy/rspec/helpers.rb +55 -0
  55. data/lib/chewy/rspec/update_index.rb +55 -44
  56. data/lib/chewy/rspec.rb +2 -0
  57. data/lib/chewy/runtime/version.rb +1 -1
  58. data/lib/chewy/runtime.rb +1 -1
  59. data/lib/chewy/search/loader.rb +19 -41
  60. data/lib/chewy/search/parameters/collapse.rb +16 -0
  61. data/lib/chewy/search/parameters/concerns/query_storage.rb +2 -2
  62. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  63. data/lib/chewy/search/parameters/indices.rb +13 -58
  64. data/lib/chewy/search/parameters/knn.rb +16 -0
  65. data/lib/chewy/search/parameters/order.rb +6 -19
  66. data/lib/chewy/search/parameters/source.rb +5 -1
  67. data/lib/chewy/search/parameters/storage.rb +1 -1
  68. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  69. data/lib/chewy/search/parameters.rb +6 -4
  70. data/lib/chewy/search/query_proxy.rb +9 -2
  71. data/lib/chewy/search/request.rb +169 -134
  72. data/lib/chewy/search/response.rb +5 -5
  73. data/lib/chewy/search/scoping.rb +7 -8
  74. data/lib/chewy/search/scrolling.rb +13 -13
  75. data/lib/chewy/search.rb +9 -19
  76. data/lib/chewy/stash.rb +19 -30
  77. data/lib/chewy/strategy/active_job.rb +1 -1
  78. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  79. data/lib/chewy/strategy/base.rb +10 -0
  80. data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +151 -0
  81. data/lib/chewy/strategy/delayed_sidekiq/worker.rb +52 -0
  82. data/lib/chewy/strategy/delayed_sidekiq.rb +30 -0
  83. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  84. data/lib/chewy/strategy/sidekiq.rb +2 -1
  85. data/lib/chewy/strategy.rb +6 -19
  86. data/lib/chewy/version.rb +1 -1
  87. data/lib/chewy.rb +39 -86
  88. data/lib/generators/chewy/install_generator.rb +1 -1
  89. data/lib/tasks/chewy.rake +36 -32
  90. data/migration_guide.md +46 -8
  91. data/spec/chewy/config_spec.rb +14 -39
  92. data/spec/chewy/elastic_client_spec.rb +26 -0
  93. data/spec/chewy/fields/base_spec.rb +432 -147
  94. data/spec/chewy/fields/root_spec.rb +20 -28
  95. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  96. data/spec/chewy/index/actions_spec.rb +368 -59
  97. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +156 -40
  98. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  99. data/spec/chewy/index/aliases_spec.rb +3 -3
  100. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  101. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  102. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +9 -19
  103. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  104. data/spec/chewy/{type → index}/import_spec.rb +164 -98
  105. data/spec/chewy/index/mapping_spec.rb +135 -0
  106. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  107. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  108. data/spec/chewy/index/observe_spec.rb +143 -0
  109. data/spec/chewy/index/settings_spec.rb +3 -1
  110. data/spec/chewy/index/specification_spec.rb +20 -30
  111. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  112. data/spec/chewy/{type → index}/witchcraft_spec.rb +20 -22
  113. data/spec/chewy/index/wrapper_spec.rb +100 -0
  114. data/spec/chewy/index_spec.rb +60 -105
  115. data/spec/chewy/journal_spec.rb +25 -74
  116. data/spec/chewy/minitest/helpers_spec.rb +123 -15
  117. data/spec/chewy/minitest/search_index_receiver_spec.rb +28 -30
  118. data/spec/chewy/multi_search_spec.rb +4 -5
  119. data/spec/chewy/rake_helper_spec.rb +315 -55
  120. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  121. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  122. data/spec/chewy/rspec/update_index_spec.rb +74 -71
  123. data/spec/chewy/runtime_spec.rb +2 -2
  124. data/spec/chewy/search/loader_spec.rb +19 -53
  125. data/spec/chewy/search/pagination/kaminari_examples.rb +4 -6
  126. data/spec/chewy/search/pagination/kaminari_spec.rb +2 -2
  127. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  128. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  129. data/spec/chewy/search/parameters/indices_spec.rb +26 -117
  130. data/spec/chewy/search/parameters/knn_spec.rb +5 -0
  131. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  132. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  133. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  134. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  135. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  136. data/spec/chewy/search/parameters_spec.rb +18 -4
  137. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  138. data/spec/chewy/search/request_spec.rb +292 -110
  139. data/spec/chewy/search/response_spec.rb +12 -12
  140. data/spec/chewy/search/scrolling_spec.rb +10 -17
  141. data/spec/chewy/search_spec.rb +40 -34
  142. data/spec/chewy/stash_spec.rb +9 -21
  143. data/spec/chewy/strategy/active_job_spec.rb +16 -16
  144. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  145. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  146. data/spec/chewy/strategy/delayed_sidekiq_spec.rb +202 -0
  147. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  148. data/spec/chewy/strategy/sidekiq_spec.rb +12 -12
  149. data/spec/chewy/strategy_spec.rb +19 -15
  150. data/spec/chewy_spec.rb +24 -107
  151. data/spec/spec_helper.rb +3 -22
  152. data/spec/support/active_record.rb +25 -7
  153. metadata +78 -339
  154. data/.circleci/config.yml +0 -240
  155. data/Appraisals +0 -81
  156. data/gemfiles/rails.5.2.activerecord.gemfile +0 -17
  157. data/gemfiles/rails.5.2.mongoid.6.4.gemfile +0 -17
  158. data/gemfiles/rails.6.0.activerecord.gemfile +0 -17
  159. data/gemfiles/sequel.4.45.gemfile +0 -11
  160. data/lib/chewy/backports/deep_dup.rb +0 -46
  161. data/lib/chewy/backports/duplicable.rb +0 -91
  162. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  163. data/lib/chewy/search/parameters/types.rb +0 -20
  164. data/lib/chewy/strategy/resque.rb +0 -27
  165. data/lib/chewy/strategy/shoryuken.rb +0 -40
  166. data/lib/chewy/type/actions.rb +0 -43
  167. data/lib/chewy/type/adapter/mongoid.rb +0 -67
  168. data/lib/chewy/type/adapter/sequel.rb +0 -93
  169. data/lib/chewy/type/crutch.rb +0 -32
  170. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  171. data/lib/chewy/type/observe.rb +0 -82
  172. data/lib/chewy/type.rb +0 -120
  173. data/lib/sequel/plugins/chewy_observe.rb +0 -63
  174. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  175. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  176. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  177. data/spec/chewy/strategy/resque_spec.rb +0 -46
  178. data/spec/chewy/strategy/shoryuken_spec.rb +0 -70
  179. data/spec/chewy/type/actions_spec.rb +0 -50
  180. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  181. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  182. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -194
  183. data/spec/chewy/type/mapping_spec.rb +0 -175
  184. data/spec/chewy/type/observe_spec.rb +0 -137
  185. data/spec/chewy/type/wrapper_spec.rb +0 -100
  186. data/spec/chewy/type_spec.rb +0 -55
  187. data/spec/support/mongoid.rb +0 -93
  188. data/spec/support/sequel.rb +0 -80
@@ -8,16 +8,14 @@ describe Chewy::Search::Response, :orm do
8
8
  stub_model(:country)
9
9
 
10
10
  stub_index(:cities) do
11
- define_type City do
12
- field :name
13
- field :rating, type: 'integer'
14
- end
11
+ index_scope City
12
+ field :name
13
+ field :rating, type: 'integer'
15
14
  end
16
15
  stub_index(:countries) do
17
- define_type Country do
18
- field :name
19
- field :rating, type: 'integer'
20
- end
16
+ index_scope Country
17
+ field :name
18
+ field :rating, type: 'integer'
21
19
  end
22
20
  end
23
21
 
@@ -127,7 +125,9 @@ describe Chewy::Search::Response, :orm do
127
125
  specify { expect(subject.aggs).to eq({}) }
128
126
 
129
127
  context do
130
- let(:request) { Chewy::Search::Request.new(CitiesIndex, CountriesIndex).aggs(avg_rating: {avg: {field: :rating}}) }
128
+ let(:request) do
129
+ Chewy::Search::Request.new(CitiesIndex, CountriesIndex).aggs(avg_rating: {avg: {field: :rating}})
130
+ end
131
131
  specify { expect(subject.aggs).to eq('avg_rating' => {'value' => 1.5}) }
132
132
  end
133
133
  end
@@ -137,7 +137,7 @@ describe Chewy::Search::Response, :orm do
137
137
  specify { expect(subject.wrappers).to have(4).items }
138
138
  specify do
139
139
  expect(subject.wrappers.map(&:class).uniq)
140
- .to contain_exactly(CitiesIndex::City, CountriesIndex::Country)
140
+ .to contain_exactly(CitiesIndex, CountriesIndex)
141
141
  end
142
142
  specify { expect(subject.wrappers.map(&:_data)).to eq(subject.hits) }
143
143
 
@@ -166,7 +166,7 @@ describe Chewy::Search::Response, :orm do
166
166
  '_source' => {'id' => 2, 'rating' => 0}}
167
167
  ]}}
168
168
  end
169
- specify { expect(subject.wrappers.first).to be_a(CitiesIndex::City) }
169
+ specify { expect(subject.wrappers.first).to be_a(CitiesIndex) }
170
170
  specify { expect(subject.wrappers.first.id).to eq(2) }
171
171
  specify { expect(subject.wrappers.first.rating).to eq(0) }
172
172
  specify { expect(subject.wrappers.first._score).to eq(1.3) }
@@ -183,7 +183,7 @@ describe Chewy::Search::Response, :orm do
183
183
  '_explanation' => {foo: 'bar'}}
184
184
  ]}}
185
185
  end
186
- specify { expect(subject.wrappers.first).to be_a(CountriesIndex::Country) }
186
+ specify { expect(subject.wrappers.first).to be_a(CountriesIndex) }
187
187
  specify { expect(subject.wrappers.first.id).to eq('2') }
188
188
  specify { expect(subject.wrappers.first.rating).to be_nil }
189
189
  specify { expect(subject.wrappers.first._score).to eq(1.2) }
@@ -8,16 +8,14 @@ describe Chewy::Search::Scrolling, :orm do
8
8
  stub_model(:country)
9
9
 
10
10
  stub_index(:cities) do
11
- define_type City do
12
- field :name
13
- field :rating, type: 'integer'
14
- end
11
+ index_scope City
12
+ field :name
13
+ field :rating, type: 'integer'
15
14
  end
16
15
  stub_index(:countries) do
17
- define_type Country do
18
- field :name
19
- field :rating, type: 'integer'
20
- end
16
+ index_scope Country
17
+ field :name
18
+ field :rating, type: 'integer'
21
19
  end
22
20
  end
23
21
 
@@ -108,7 +106,7 @@ describe Chewy::Search::Scrolling, :orm do
108
106
  end
109
107
 
110
108
  it 'clears the scroll after completion' do
111
- expect(Chewy.client).to receive(:clear_scroll).with(scroll_id: anything).once.and_call_original
109
+ expect(Chewy.client).to receive(:clear_scroll).with(body: {scroll_id: anything}).once.and_call_original
112
110
  request.scroll_batches(batch_size: 3) {}
113
111
  end
114
112
 
@@ -119,21 +117,16 @@ describe Chewy::Search::Scrolling, :orm do
119
117
  outer_payload << payload
120
118
  end
121
119
  request.scroll_batches(batch_size: 3).to_a
122
-
123
120
  expect(outer_payload).to match_array([
124
121
  hash_including(
125
122
  index: [CitiesIndex, CountriesIndex],
126
123
  indexes: [CitiesIndex, CountriesIndex],
127
- request: {index: %w[cities countries], type: %w[city country], body: {sort: ['rating']}, size: 3, scroll: '1m'},
128
- type: [CitiesIndex::City, CountriesIndex::Country],
129
- types: [CitiesIndex::City, CountriesIndex::Country]
124
+ request: {index: %w[cities countries], body: {sort: ['rating']}, size: 3, scroll: '1m'}
130
125
  ),
131
126
  hash_including(
132
127
  index: [CitiesIndex, CountriesIndex],
133
128
  indexes: [CitiesIndex, CountriesIndex],
134
- request: {scroll: '1m', scroll_id: an_instance_of(String)},
135
- type: [CitiesIndex::City, CountriesIndex::Country],
136
- types: [CitiesIndex::City, CountriesIndex::Country]
129
+ request: {scroll: '1m', scroll_id: an_instance_of(String)}
137
130
  )
138
131
  ])
139
132
  end
@@ -158,7 +151,7 @@ describe Chewy::Search::Scrolling, :orm do
158
151
  end
159
152
  specify do
160
153
  expect(request.scroll_wrappers(batch_size: 2).map(&:class).uniq)
161
- .to eq([CitiesIndex::City, CountriesIndex::Country])
154
+ .to eq([CitiesIndex, CountriesIndex])
162
155
  end
163
156
  end
164
157
 
@@ -4,47 +4,34 @@ describe Chewy::Search do
4
4
  before { Chewy.massacre }
5
5
 
6
6
  before do
7
- stub_index(:products) do
8
- define_type :product
9
- end
7
+ stub_index(:products)
10
8
  end
11
9
 
12
- let(:product) { ProductsIndex::Product }
13
-
14
10
  describe '.all' do
15
11
  specify { expect(ProductsIndex.all).to be_a(Chewy::Search::Request) }
16
- specify { expect(product.all).to be_a(Chewy::Search::Request) }
17
12
 
18
13
  context do
19
14
  before { allow(Chewy).to receive_messages(search_class: Chewy::Search::Request) }
20
15
 
21
16
  specify { expect(ProductsIndex.all).to be_a(Chewy::Search::Request) }
22
- specify { expect(product.all).to be_a(Chewy::Search::Request) }
23
17
  end
24
18
  end
25
19
 
26
20
  describe '.search_string' do
27
21
  specify do
28
- expect(ProductsIndex.client).to receive(:search).with(hash_including(q: 'hello')).twice
22
+ expect(ProductsIndex.client).to receive(:search).with(hash_including(q: 'hello')).once
29
23
  ProductsIndex.search_string('hello')
30
- product.search_string('hello')
31
24
  end
32
25
 
33
26
  specify do
34
- expect(ProductsIndex.client).to receive(:search).with(hash_including(explain: true)).twice
27
+ expect(ProductsIndex.client).to receive(:search).with(hash_including(explain: true)).once
35
28
  ProductsIndex.search_string('hello', explain: true)
36
- product.search_string('hello', explain: true)
37
29
  end
38
30
 
39
31
  specify do
40
- expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'], type: %w[product]))
32
+ expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'])).once
41
33
  ProductsIndex.search_string('hello')
42
34
  end
43
-
44
- specify do
45
- expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'], type: %w[product]))
46
- product.search_string('hello')
47
- end
48
35
  end
49
36
 
50
37
  context 'named scopes' do
@@ -61,10 +48,13 @@ describe Chewy::Search do
61
48
  filter { match name: "Name#{index}" }
62
49
  end
63
50
 
64
- define_type City do
65
- field :name, type: 'keyword'
66
- field :rating, type: :integer
51
+ def self.by_rating_with_kwargs(value, options:) # rubocop:disable Lint/UnusedMethodArgument
52
+ filter { match rating: value }
67
53
  end
54
+
55
+ index_scope City
56
+ field :name, type: 'keyword'
57
+ field :rating, type: :integer
68
58
  end
69
59
 
70
60
  stub_index(:countries) do
@@ -76,10 +66,9 @@ describe Chewy::Search do
76
66
  filter { match name: "Name#{index}" }
77
67
  end
78
68
 
79
- define_type Country do
80
- field :name, type: 'keyword'
81
- field :rating, type: :integer
82
- end
69
+ index_scope Country
70
+ field :name, type: 'keyword'
71
+ field :rating, type: :integer
83
72
  end
84
73
  end
85
74
 
@@ -92,30 +81,47 @@ describe Chewy::Search do
92
81
  end
93
82
 
94
83
  specify { expect(CitiesIndex.indices(CountriesIndex).by_rating(1).map(&:rating)).to eq([1, 1]) }
95
- specify { expect(CitiesIndex.indices(CountriesIndex).by_rating(1).map(&:class)).to match_array([CitiesIndex::City, CountriesIndex::Country]) }
84
+ specify do
85
+ expect(CitiesIndex.indices(CountriesIndex).by_rating(1).map(&:class))
86
+ .to match_array([CitiesIndex, CountriesIndex])
87
+ end
96
88
  specify { expect(CitiesIndex.indices(CountriesIndex).by_rating(1).by_name(2).map(&:rating)).to eq([1]) }
97
- specify { expect(CitiesIndex.indices(CountriesIndex).by_rating(1).by_name(2).map(&:class)).to eq([CitiesIndex::City]) }
89
+ specify do
90
+ expect(CitiesIndex.indices(CountriesIndex).by_rating(1).by_name(2).map(&:class))
91
+ .to eq([CitiesIndex])
92
+ end
98
93
  specify { expect(CitiesIndex.indices(CountriesIndex).by_name(3).map(&:rating)).to eq([2, 1]) }
99
- specify { expect(CitiesIndex.indices(CountriesIndex).by_name(3).map(&:class)).to eq([CitiesIndex::City, CountriesIndex::Country]) }
94
+ specify do
95
+ expect(CitiesIndex.indices(CountriesIndex).by_name(3).map(&:class))
96
+ .to eq([CitiesIndex, CountriesIndex])
97
+ end
100
98
  specify { expect(CitiesIndex.indices(CountriesIndex).order(:name).by_rating(1).map(&:rating)).to eq([1, 1]) }
101
- specify { expect(CitiesIndex.indices(CountriesIndex).order(:name).by_rating(1).map(&:class)).to match_array([CitiesIndex::City, CountriesIndex::Country]) }
99
+ specify do
100
+ expect(CitiesIndex.indices(CountriesIndex).order(:name).by_rating(1).map(&:class))
101
+ .to match_array([CitiesIndex, CountriesIndex])
102
+ end
102
103
 
103
104
  specify { expect(CitiesIndex.by_rating(2).map(&:rating)).to eq([2]) }
104
- specify { expect(CitiesIndex.by_rating(2).map(&:class)).to eq([CitiesIndex::City]) }
105
+ specify { expect(CitiesIndex.by_rating(2).map(&:class)).to eq([CitiesIndex]) }
105
106
  specify { expect(CitiesIndex.by_rating(2).by_name(3).map(&:rating)).to eq([2]) }
106
- specify { expect(CitiesIndex.by_rating(2).by_name(3).map(&:class)).to eq([CitiesIndex::City]) }
107
+ specify { expect(CitiesIndex.by_rating(2).by_name(3).map(&:class)).to eq([CitiesIndex]) }
107
108
  specify { expect(CitiesIndex.by_name(3).map(&:rating)).to eq([2]) }
108
109
  specify { expect(CitiesIndex.by_name(3).map(&:rating)).to eq([2]) }
109
110
  specify { expect(CitiesIndex.order(:name).by_name(3).map(&:rating)).to eq([2]) }
110
111
  specify { expect(CitiesIndex.order(:name).by_name(3).map(&:rating)).to eq([2]) }
111
112
  specify { expect(CitiesIndex.order(:name).by_rating(2).map(&:rating)).to eq([2]) }
112
- specify { expect(CitiesIndex.order(:name).by_rating(2).map(&:class)).to eq([CitiesIndex::City]) }
113
+ specify { expect(CitiesIndex.order(:name).by_rating(2).map(&:class)).to eq([CitiesIndex]) }
113
114
 
114
115
  specify { expect(CountriesIndex.by_rating(3).map(&:rating)).to eq([3]) }
115
- specify { expect(CountriesIndex.by_rating(3).map(&:class)).to eq([CountriesIndex::Country]) }
116
+ specify { expect(CountriesIndex.by_rating(3).map(&:class)).to eq([CountriesIndex]) }
116
117
  specify { expect(CountriesIndex.by_rating(3).by_name(5).map(&:rating)).to eq([3]) }
117
- specify { expect(CountriesIndex.by_rating(3).by_name(5).map(&:class)).to eq([CountriesIndex::Country]) }
118
+ specify { expect(CountriesIndex.by_rating(3).by_name(5).map(&:class)).to eq([CountriesIndex]) }
118
119
  specify { expect(CountriesIndex.order(:name).by_rating(3).map(&:rating)).to eq([3]) }
119
- specify { expect(CountriesIndex.order(:name).by_rating(3).map(&:class)).to eq([CountriesIndex::Country]) }
120
+ specify { expect(CountriesIndex.order(:name).by_rating(3).map(&:class)).to eq([CountriesIndex]) }
121
+
122
+ specify 'supports keyword arguments' do
123
+ expect(CitiesIndex.by_rating_with_kwargs(3, options: 'blah blah blah').map(&:rating)).to eq([3])
124
+ expect(CitiesIndex.order(:name).by_rating_with_kwargs(3, options: 'blah blah blah').map(&:rating)).to eq([3])
125
+ end
120
126
  end
121
127
  end
@@ -10,12 +10,10 @@ describe Chewy::Stash::Journal, :orm do
10
10
  before do
11
11
  stub_model(:city)
12
12
  stub_index(:cities) do
13
- define_type City
13
+ index_scope City
14
14
  end
15
- stub_index(:countries) do
16
- define_type :country
17
- end
18
- stub_index(:users) { define_type :user }
15
+ stub_index(:countries)
16
+ stub_index(:users)
19
17
  stub_index(:borogoves)
20
18
  end
21
19
 
@@ -28,7 +26,7 @@ describe Chewy::Stash::Journal, :orm do
28
26
  CountriesIndex.import!([id: 2, name: 'Country'], journal: true)
29
27
  end
30
28
  Timecop.travel(Time.now + 2.minutes) do
31
- UsersIndex::User.import!([id: 3, name: 'User'], journal: true)
29
+ UsersIndex.import!([id: 3, name: 'User'], journal: true)
32
30
  end
33
31
  end
34
32
 
@@ -72,26 +70,16 @@ describe Chewy::Stash::Journal, :orm do
72
70
  specify { expect(fetch_deleted_number(described_class.clean(only: UsersIndex))).to eq(1) }
73
71
  specify { expect(fetch_deleted_number(described_class.clean(only: [CitiesIndex, UsersIndex]))).to eq(2) }
74
72
 
75
- specify { expect(fetch_deleted_number(described_class.clean(Time.now + 30.seconds, only: CountriesIndex))).to eq(0) }
73
+ specify do
74
+ expect(fetch_deleted_number(described_class.clean(Time.now + 30.seconds, only: CountriesIndex))).to eq(0)
75
+ end
76
76
  specify { expect(fetch_deleted_number(described_class.clean(Time.now + 30.seconds, only: CitiesIndex))).to eq(1) }
77
77
  end
78
78
 
79
79
  describe '.for' do
80
80
  specify { expect(described_class.for(UsersIndex).map(&:index_name)).to eq(['users']) }
81
- specify { expect(described_class.for(CitiesIndex, CountriesIndex).map(&:type_name)).to contain_exactly('city', 'country') }
82
- specify { expect(described_class.for(CitiesIndex, UsersIndex).map(&:index_name)).to contain_exactly('cities', 'users') }
83
- end
84
-
85
- describe '#type' do
86
- let(:index_name) { 'users' }
87
- let(:type_name) { 'city' }
88
- subject { described_class::Journal.new('index_name' => index_name, 'type_name' => type_name).type }
89
-
90
- specify { expect { subject }.to raise_error(Chewy::UnderivableType) }
91
-
92
- context do
93
- let(:index_name) { 'cities' }
94
- it { is_expected.to eq(CitiesIndex::City) }
81
+ specify do
82
+ expect(described_class.for(CitiesIndex, UsersIndex).map(&:index_name)).to contain_exactly('cities', 'users')
95
83
  end
96
84
  end
97
85
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if defined?(::ActiveJob)
3
+ if defined?(ActiveJob)
4
4
  describe Chewy::Strategy::ActiveJob do
5
5
  around do |example|
6
6
  active_job_settings = Chewy.settings[:active_job]
@@ -9,21 +9,21 @@ if defined?(::ActiveJob)
9
9
  Chewy.settings[:active_job] = active_job_settings
10
10
  end
11
11
  before(:all) do
12
- ::ActiveJob::Base.logger = Chewy.logger
12
+ ActiveJob::Base.logger = Chewy.logger
13
13
  end
14
14
  before do
15
- ::ActiveJob::Base.queue_adapter = :test
16
- ::ActiveJob::Base.queue_adapter.enqueued_jobs.clear
17
- ::ActiveJob::Base.queue_adapter.performed_jobs.clear
15
+ ActiveJob::Base.queue_adapter = :test
16
+ ActiveJob::Base.queue_adapter.enqueued_jobs.clear
17
+ ActiveJob::Base.queue_adapter.performed_jobs.clear
18
18
  end
19
19
 
20
20
  before do
21
21
  stub_model(:city) do
22
- update_index('cities#city') { self }
22
+ update_index('cities') { self }
23
23
  end
24
24
 
25
25
  stub_index(:cities) do
26
- define_type City
26
+ index_scope City
27
27
  end
28
28
  end
29
29
 
@@ -32,14 +32,14 @@ if defined?(::ActiveJob)
32
32
 
33
33
  specify do
34
34
  expect { [city, other_city].map(&:save!) }
35
- .not_to update_index(CitiesIndex::City, strategy: :active_job)
35
+ .not_to update_index(CitiesIndex, strategy: :active_job)
36
36
  end
37
37
 
38
38
  specify do
39
39
  Chewy.strategy(:active_job) do
40
40
  [city, other_city].map(&:save!)
41
41
  end
42
- enqueued_job = ::ActiveJob::Base.queue_adapter.enqueued_jobs.first
42
+ enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.first
43
43
  expect(enqueued_job[:job]).to eq(Chewy::Strategy::ActiveJob::Worker)
44
44
  expect(enqueued_job[:queue]).to eq('low')
45
45
  end
@@ -48,26 +48,26 @@ if defined?(::ActiveJob)
48
48
  Chewy.strategy(:active_job) do
49
49
  [city, other_city].map(&:save!)
50
50
  end
51
- enqueued_job = ::ActiveJob::Base.queue_adapter.enqueued_jobs.first
51
+ enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.first
52
52
  expect(enqueued_job[:queue]).to eq('low')
53
53
  end
54
54
 
55
55
  specify do
56
- ::ActiveJob::Base.queue_adapter = :inline
56
+ ActiveJob::Base.queue_adapter = :inline
57
57
  expect { [city, other_city].map(&:save!) }
58
- .to update_index(CitiesIndex::City, strategy: :active_job)
58
+ .to update_index(CitiesIndex, strategy: :active_job)
59
59
  .and_reindex(city, other_city).only
60
60
  end
61
61
 
62
62
  specify do
63
- expect(CitiesIndex::City).to receive(:import!).with([city.id, other_city.id], suffix: '201601')
64
- Chewy::Strategy::ActiveJob::Worker.new.perform('CitiesIndex::City', [city.id, other_city.id], suffix: '201601')
63
+ expect(CitiesIndex).to receive(:import!).with([city.id, other_city.id], suffix: '201601')
64
+ Chewy::Strategy::ActiveJob::Worker.new.perform('CitiesIndex', [city.id, other_city.id], suffix: '201601')
65
65
  end
66
66
 
67
67
  specify do
68
68
  allow(Chewy).to receive(:disable_refresh_async).and_return(true)
69
- expect(CitiesIndex::City).to receive(:import!).with([city.id, other_city.id], suffix: '201601', refresh: false)
70
- Chewy::Strategy::ActiveJob::Worker.new.perform('CitiesIndex::City', [city.id, other_city.id], suffix: '201601')
69
+ expect(CitiesIndex).to receive(:import!).with([city.id, other_city.id], suffix: '201601', refresh: false)
70
+ Chewy::Strategy::ActiveJob::Worker.new.perform('CitiesIndex', [city.id, other_city.id], suffix: '201601')
71
71
  end
72
72
  end
73
73
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Strategy::AtomicNoRefresh, :orm do
4
+ around { |example| Chewy.strategy(:bypass) { example.run } }
5
+
6
+ before do
7
+ stub_model(:country) do
8
+ update_index('countries') { self }
9
+ end
10
+
11
+ stub_index(:countries) do
12
+ index_scope Country
13
+ end
14
+ end
15
+
16
+ let(:country) { Country.create!(name: 'hello', country_code: 'HL') }
17
+ let(:other_country) { Country.create!(name: 'world', country_code: 'WD') }
18
+
19
+ specify do
20
+ expect { [country, other_country].map(&:save!) }
21
+ .to update_index(CountriesIndex, strategy: :atomic_no_refresh)
22
+ .and_reindex(country, other_country).only.no_refresh
23
+ end
24
+
25
+ specify do
26
+ expect { [country, other_country].map(&:destroy) }
27
+ .to update_index(CountriesIndex, strategy: :atomic_no_refresh)
28
+ .and_delete(country, other_country).only.no_refresh
29
+ end
30
+
31
+ context do
32
+ before do
33
+ stub_index(:countries) do
34
+ index_scope Country
35
+ root id: -> { country_code }
36
+ end
37
+ end
38
+
39
+ specify do
40
+ expect { [country, other_country].map(&:save!) }
41
+ .to update_index(CountriesIndex, strategy: :atomic_no_refresh)
42
+ .and_reindex('HL', 'WD').only.no_refresh
43
+ end
44
+
45
+ specify do
46
+ expect { [country, other_country].map(&:destroy) }
47
+ .to update_index(CountriesIndex, strategy: :atomic_no_refresh)
48
+ .and_delete('HL', 'WD').only.no_refresh
49
+ end
50
+
51
+ specify do
52
+ expect do
53
+ country.save!
54
+ other_country.destroy
55
+ end
56
+ .to update_index(CountriesIndex, strategy: :atomic_no_refresh)
57
+ .and_reindex('HL').and_delete('WD').only.no_refresh
58
+ end
59
+ end
60
+ end
@@ -5,11 +5,11 @@ describe Chewy::Strategy::Atomic, :orm do
5
5
 
6
6
  before do
7
7
  stub_model(:country) do
8
- update_index('countries#country') { self }
8
+ update_index('countries') { self }
9
9
  end
10
10
 
11
11
  stub_index(:countries) do
12
- define_type Country
12
+ index_scope Country
13
13
  end
14
14
  end
15
15
 
@@ -18,35 +18,34 @@ describe Chewy::Strategy::Atomic, :orm do
18
18
 
19
19
  specify do
20
20
  expect { [country, other_country].map(&:save!) }
21
- .to update_index(CountriesIndex::Country, strategy: :atomic)
21
+ .to update_index(CountriesIndex, strategy: :atomic)
22
22
  .and_reindex(country, other_country).only
23
23
  end
24
24
 
25
25
  specify do
26
26
  expect { [country, other_country].map(&:destroy) }
27
- .to update_index(CountriesIndex::Country, strategy: :atomic)
27
+ .to update_index(CountriesIndex, strategy: :atomic)
28
28
  .and_delete(country, other_country).only
29
29
  end
30
30
 
31
31
  context do
32
32
  before do
33
33
  stub_index(:countries) do
34
- define_type Country do
35
- root id: -> { country_code } do
36
- end
34
+ index_scope Country
35
+ root id: -> { country_code } do
37
36
  end
38
37
  end
39
38
  end
40
39
 
41
40
  specify do
42
41
  expect { [country, other_country].map(&:save!) }
43
- .to update_index(CountriesIndex::Country, strategy: :atomic)
42
+ .to update_index(CountriesIndex, strategy: :atomic)
44
43
  .and_reindex('HL', 'WD').only
45
44
  end
46
45
 
47
46
  specify do
48
47
  expect { [country, other_country].map(&:destroy) }
49
- .to update_index(CountriesIndex::Country, strategy: :atomic)
48
+ .to update_index(CountriesIndex, strategy: :atomic)
50
49
  .and_delete('HL', 'WD').only
51
50
  end
52
51
 
@@ -55,7 +54,7 @@ describe Chewy::Strategy::Atomic, :orm do
55
54
  country.save!
56
55
  other_country.destroy
57
56
  end
58
- .to update_index(CountriesIndex::Country, strategy: :atomic)
57
+ .to update_index(CountriesIndex, strategy: :atomic)
59
58
  .and_reindex('HL').and_delete('WD').only
60
59
  end
61
60
  end