chewy 0.8.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +34 -0
  3. data/.rubocop_todo.yml +44 -0
  4. data/.travis.yml +20 -60
  5. data/Appraisals +15 -40
  6. data/CHANGELOG.md +42 -0
  7. data/Gemfile +1 -0
  8. data/Guardfile +5 -5
  9. data/README.md +155 -6
  10. data/Rakefile +11 -1
  11. data/chewy.gemspec +5 -7
  12. data/gemfiles/rails.3.2.activerecord.gemfile +1 -0
  13. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +1 -0
  14. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +1 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +1 -0
  16. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +1 -0
  17. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +1 -0
  18. data/gemfiles/{rails.4.2.mongoid.4.0.0.gemfile → rails.4.2.mongoid.4.0.gemfile} +2 -1
  19. data/gemfiles/{rails.4.2.mongoid.4.0.0.kaminari.gemfile → rails.4.2.mongoid.4.0.kaminari.gemfile} +2 -1
  20. data/gemfiles/{rails.4.2.mongoid.4.0.0.will_paginate.gemfile → rails.4.2.mongoid.4.0.will_paginate.gemfile} +2 -1
  21. data/gemfiles/{rails.4.2.mongoid.5.1.0.gemfile → rails.4.2.mongoid.5.1.gemfile} +2 -1
  22. data/gemfiles/{rails.4.2.mongoid.5.1.0.kaminari.gemfile → rails.4.2.mongoid.5.1.kaminari.gemfile} +2 -1
  23. data/gemfiles/{rails.4.2.mongoid.5.1.0.will_paginate.gemfile → rails.4.2.mongoid.5.1.will_paginate.gemfile} +2 -1
  24. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.5.0.activerecord.gemfile} +4 -2
  25. data/gemfiles/{rails.4.0.mongoid.4.0.0.kaminari.gemfile → rails.5.0.activerecord.kaminari.gemfile} +4 -2
  26. data/gemfiles/{rails.4.0.mongoid.4.0.0.will_paginate.gemfile → rails.5.0.activerecord.will_paginate.gemfile} +4 -2
  27. data/gemfiles/{sequel.4.31.gemfile → sequel.4.38.gemfile} +3 -2
  28. data/lib/chewy.rb +24 -16
  29. data/lib/chewy/backports/deep_dup.rb +1 -1
  30. data/lib/chewy/backports/duplicable.rb +1 -0
  31. data/lib/chewy/config.rb +13 -7
  32. data/lib/chewy/errors.rb +4 -4
  33. data/lib/chewy/fields/base.rb +19 -14
  34. data/lib/chewy/fields/root.rb +11 -9
  35. data/lib/chewy/index.rb +38 -25
  36. data/lib/chewy/index/actions.rb +17 -17
  37. data/lib/chewy/index/settings.rb +3 -4
  38. data/lib/chewy/journal.rb +107 -0
  39. data/lib/chewy/journal/apply.rb +31 -0
  40. data/lib/chewy/journal/clean.rb +24 -0
  41. data/lib/chewy/journal/entry.rb +83 -0
  42. data/lib/chewy/journal/query.rb +87 -0
  43. data/lib/chewy/log_subscriber.rb +8 -8
  44. data/lib/chewy/minitest.rb +1 -0
  45. data/lib/chewy/minitest/helpers.rb +77 -0
  46. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  47. data/lib/chewy/query.rb +116 -60
  48. data/lib/chewy/query/compose.rb +5 -6
  49. data/lib/chewy/query/criteria.rb +26 -16
  50. data/lib/chewy/query/filters.rb +9 -9
  51. data/lib/chewy/query/loading.rb +2 -2
  52. data/lib/chewy/query/nodes/and.rb +3 -3
  53. data/lib/chewy/query/nodes/base.rb +1 -1
  54. data/lib/chewy/query/nodes/bool.rb +6 -4
  55. data/lib/chewy/query/nodes/equal.rb +6 -6
  56. data/lib/chewy/query/nodes/exists.rb +2 -2
  57. data/lib/chewy/query/nodes/expr.rb +2 -2
  58. data/lib/chewy/query/nodes/field.rb +35 -31
  59. data/lib/chewy/query/nodes/has_child.rb +1 -0
  60. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  61. data/lib/chewy/query/nodes/has_relation.rb +11 -13
  62. data/lib/chewy/query/nodes/match_all.rb +1 -1
  63. data/lib/chewy/query/nodes/missing.rb +2 -2
  64. data/lib/chewy/query/nodes/not.rb +3 -3
  65. data/lib/chewy/query/nodes/or.rb +3 -3
  66. data/lib/chewy/query/nodes/prefix.rb +4 -3
  67. data/lib/chewy/query/nodes/query.rb +3 -3
  68. data/lib/chewy/query/nodes/range.rb +11 -11
  69. data/lib/chewy/query/nodes/raw.rb +1 -1
  70. data/lib/chewy/query/nodes/regexp.rb +15 -11
  71. data/lib/chewy/query/nodes/script.rb +6 -6
  72. data/lib/chewy/query/pagination/will_paginate.rb +2 -2
  73. data/lib/chewy/railtie.rb +3 -3
  74. data/lib/chewy/rake_helper.rb +51 -30
  75. data/lib/chewy/repository.rb +2 -2
  76. data/lib/chewy/rspec.rb +1 -1
  77. data/lib/chewy/rspec/update_index.rb +46 -47
  78. data/lib/chewy/runtime/version.rb +4 -4
  79. data/lib/chewy/search.rb +7 -5
  80. data/lib/chewy/strategy.rb +10 -8
  81. data/lib/chewy/strategy/atomic.rb +2 -2
  82. data/lib/chewy/strategy/base.rb +4 -4
  83. data/lib/chewy/strategy/bypass.rb +1 -2
  84. data/lib/chewy/strategy/sidekiq.rb +2 -0
  85. data/lib/chewy/strategy/urgent.rb +1 -1
  86. data/lib/chewy/type.rb +51 -45
  87. data/lib/chewy/type/adapter/active_record.rb +23 -12
  88. data/lib/chewy/type/adapter/base.rb +4 -4
  89. data/lib/chewy/type/adapter/mongoid.rb +6 -6
  90. data/lib/chewy/type/adapter/object.rb +15 -12
  91. data/lib/chewy/type/adapter/orm.rb +24 -15
  92. data/lib/chewy/type/adapter/sequel.rb +11 -7
  93. data/lib/chewy/type/crutch.rb +4 -3
  94. data/lib/chewy/type/import.rb +51 -32
  95. data/lib/chewy/type/mapping.rb +17 -17
  96. data/lib/chewy/type/observe.rb +9 -7
  97. data/lib/chewy/type/witchcraft.rb +62 -23
  98. data/lib/chewy/type/wrapper.rb +20 -14
  99. data/lib/chewy/version.rb +1 -1
  100. data/lib/generators/chewy/install_generator.rb +3 -3
  101. data/lib/tasks/chewy.rake +28 -23
  102. data/spec/chewy/config_spec.rb +33 -12
  103. data/spec/chewy/fields/base_spec.rb +143 -154
  104. data/spec/chewy/fields/root_spec.rb +22 -20
  105. data/spec/chewy/fields/time_fields_spec.rb +11 -9
  106. data/spec/chewy/index/actions_spec.rb +27 -4
  107. data/spec/chewy/index/aliases_spec.rb +2 -2
  108. data/spec/chewy/index/settings_spec.rb +72 -50
  109. data/spec/chewy/index_spec.rb +95 -43
  110. data/spec/chewy/journal/apply_spec.rb +120 -0
  111. data/spec/chewy/journal/entry_spec.rb +237 -0
  112. data/spec/chewy/journal_spec.rb +173 -0
  113. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  114. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  115. data/spec/chewy/query/criteria_spec.rb +504 -237
  116. data/spec/chewy/query/filters_spec.rb +94 -66
  117. data/spec/chewy/query/loading_spec.rb +76 -40
  118. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  119. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  120. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  121. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  122. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  123. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  124. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  125. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  126. data/spec/chewy/query/nodes/not_spec.rb +3 -7
  127. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  128. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  129. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  130. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  131. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  132. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  133. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  134. data/spec/chewy/query/pagination/kaminari_spec.rb +2 -2
  135. data/spec/chewy/query/pagination/will_paginage_spec.rb +6 -7
  136. data/spec/chewy/query/pagination_spec.rb +2 -3
  137. data/spec/chewy/query_spec.rb +208 -145
  138. data/spec/chewy/repository_spec.rb +8 -8
  139. data/spec/chewy/rspec/update_index_spec.rb +180 -111
  140. data/spec/chewy/search_spec.rb +8 -8
  141. data/spec/chewy/strategy/active_job_spec.rb +2 -2
  142. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  143. data/spec/chewy/strategy/resque_spec.rb +2 -2
  144. data/spec/chewy/strategy/sidekiq_spec.rb +2 -2
  145. data/spec/chewy/type/actions_spec.rb +1 -1
  146. data/spec/chewy/type/adapter/active_record_spec.rb +255 -149
  147. data/spec/chewy/type/adapter/mongoid_spec.rb +169 -108
  148. data/spec/chewy/type/adapter/object_spec.rb +56 -40
  149. data/spec/chewy/type/adapter/sequel_spec.rb +248 -163
  150. data/spec/chewy/type/import_spec.rb +78 -47
  151. data/spec/chewy/type/mapping_spec.rb +6 -6
  152. data/spec/chewy/type/observe_spec.rb +20 -14
  153. data/spec/chewy/type/witchcraft_spec.rb +89 -43
  154. data/spec/chewy/type_spec.rb +4 -3
  155. data/spec/chewy_spec.rb +10 -8
  156. data/spec/spec_helper.rb +3 -0
  157. data/spec/support/active_record.rb +1 -1
  158. data/spec/support/class_helpers.rb +10 -11
  159. data/spec/support/mongoid.rb +2 -2
  160. data/spec/support/sequel.rb +1 -1
  161. metadata +65 -35
  162. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  163. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  164. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  165. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  166. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  167. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  168. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  169. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  170. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  171. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  172. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  173. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  174. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  175. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  176. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  177. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  178. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  179. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
@@ -5,10 +5,10 @@ module Chewy
5
5
  attr_reader :analyzers, :tokenizers, :filters, :char_filters
6
6
 
7
7
  def self.delegated
8
- public_instance_methods - self.superclass.public_instance_methods - Singleton.public_instance_methods
8
+ public_instance_methods - superclass.public_instance_methods - Singleton.public_instance_methods
9
9
  end
10
10
 
11
- def self.repository name
11
+ def self.repository(name)
12
12
  plural_name = name.to_s.pluralize
13
13
 
14
14
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
@@ -1 +1 @@
1
- require 'chewy/rspec/update_index'
1
+ require 'chewy/rspec/update_index'
@@ -20,11 +20,10 @@ require 'i18n/core_ext/hash'
20
20
  # specify { expect { user1.destroy!; user2.save! } }
21
21
  # .to update_index(UsersIndex::User).and_reindex(user2).and_delete(user1) }
22
22
  #
23
- RSpec::Matchers.define :update_index do |type_name, options = {}|
24
-
23
+ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disable BlockLength
25
24
  if !respond_to?(:failure_message) && respond_to?(:failure_message_for_should)
26
- alias :failure_message :failure_message_for_should
27
- alias :failure_message_when_negated :failure_message_for_should_not
25
+ alias_method :failure_message, :failure_message_for_should
26
+ alias_method :failure_message_when_negated, :failure_message_for_should_not
28
27
  end
29
28
 
30
29
  # Specify indexed records by passing record itself or id.
@@ -83,7 +82,11 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
83
82
  # specify { expect { [user1, user2].map(&:save!) }
84
83
  # .to update_index(UsersIndex.user).and_reindex(user1).only }
85
84
  #
86
- chain(:only) do |*args|
85
+ chain(:only) do |*_args|
86
+ if @reindex.blank? && @delete.blank?
87
+ raise 'Use `only` in conjunction with `and_reindex` or `and_delete`'
88
+ end
89
+
87
90
  @only = true
88
91
  end
89
92
 
@@ -91,17 +94,16 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
91
94
  true
92
95
  end
93
96
 
94
- match do |block|
97
+ match do |block| # rubocop:disable BlockLength
95
98
  @reindex ||= {}
96
99
  @delete ||= {}
97
100
  @missed_reindex = []
98
101
  @missed_delete = []
99
102
  @updated = []
100
103
 
101
- type = Chewy.derive_type(type_name)
102
-
103
104
  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
104
- #{agnostic_stub} do |bulk_options|
105
+ type = Chewy.derive_type(type_name)
106
+ #{agnostic_stub} do |bulk_options|
105
107
  @updated += bulk_options[:body].map do |updated_document|
106
108
  updated_document.deep_symbolize_keys
107
109
  end
@@ -113,18 +115,18 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
113
115
  Chewy.strategy(options[:strategy] || :atomic) { block.call }
114
116
 
115
117
  @updated.each do |updated_document|
116
- if body = updated_document[:index]
117
- if document = @reindex[body[:_id].to_s]
118
+ if (body = updated_document[:index])
119
+ if (document = @reindex[body[:_id].to_s])
118
120
  document[:real_count] += 1
119
121
  document[:real_attributes].merge!(body[:data])
120
- else
121
- @missed_reindex.push(body[:_id].to_s) if @only
122
+ elsif @only
123
+ @missed_reindex.push(body[:_id].to_s)
122
124
  end
123
- elsif body = updated_document[:delete]
124
- if document = @delete[body[:_id].to_s]
125
+ elsif (body = updated_document[:delete])
126
+ if (document = @delete[body[:_id].to_s])
125
127
  document[:real_count] += 1
126
- else
127
- @missed_delete.push(body[:_id].to_s) if @only
128
+ elsif @only
129
+ @missed_delete.push(body[:_id].to_s)
128
130
  end
129
131
  end
130
132
  end
@@ -141,11 +143,11 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
141
143
  end
142
144
 
143
145
  @updated.present? && @missed_reindex.none? && @missed_delete.none? &&
144
- @reindex.all? { |_, document| document[:match_count] && document[:match_attributes] } &&
145
- @delete.all? { |_, document| document[:match_count] }
146
+ @reindex.all? { |_, document| document[:match_count] && document[:match_attributes] } &&
147
+ @delete.all? { |_, document| document[:match_count] }
146
148
  end
147
149
 
148
- failure_message do
150
+ failure_message do # rubocop:disable BlockLength
149
151
  output = ''
150
152
 
151
153
  if @updated.none?
@@ -166,28 +168,28 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
166
168
  output << message
167
169
  end
168
170
 
169
- output << @reindex.each.with_object('') do |(id, document), output|
171
+ output << @reindex.each.with_object('') do |(id, document), result|
170
172
  unless document[:match_count] && document[:match_attributes]
171
- output << "Expected document with id `#{id}` to be reindexed"
173
+ result << "Expected document with id `#{id}` to be reindexed"
172
174
  if document[:real_count] > 0
173
- output << "\n #{document[:expected_count]} times, but was reindexed #{document[:real_count]} times" if document[:expected_count] && !document[:match_count]
174
- output << "\n with #{document[:expected_attributes]}, but it was reindexed with #{document[:real_attributes]}" if document[:expected_attributes].present? && !document[:match_attributes]
175
+ result << "\n #{document[:expected_count]} times, but was reindexed #{document[:real_count]} times" if document[:expected_count] && !document[:match_count]
176
+ result << "\n with #{document[:expected_attributes]}, but it was reindexed with #{document[:real_attributes]}" if document[:expected_attributes].present? && !document[:match_attributes]
175
177
  else
176
- output << ", but it was not"
178
+ result << ', but it was not'
177
179
  end
178
- output << "\n"
180
+ result << "\n"
179
181
  end
180
182
  end
181
183
 
182
- output << @delete.each.with_object('') do |(id, document), output|
184
+ output << @delete.each.with_object('') do |(id, document), result|
183
185
  unless document[:match_count]
184
- output << "Expected document with id `#{id}` to be deleted"
185
- if document[:real_count] > 0 && document[:expected_count] && !document[:match_count]
186
- output << "\n #{document[:expected_count]} times, but was deleted #{document[:real_count]} times"
187
- else
188
- output << ", but it was not"
189
- end
190
- output << "\n"
186
+ result << "Expected document with id `#{id}` to be deleted"
187
+ result << if document[:real_count] > 0 && document[:expected_count]
188
+ "\n #{document[:expected_count]} times, but was deleted #{document[:real_count]} times"
189
+ else
190
+ ', but it was not'
191
+ end
192
+ result << "\n"
191
193
  end
192
194
  end
193
195
 
@@ -196,23 +198,21 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
196
198
 
197
199
  failure_message_when_negated do
198
200
  if @updated.present?
199
- "Expected index `#{type_name}` not to be updated, but it was with #{
200
- @updated.map(&:values).flatten.group_by { |documents| documents[:_id] }.map do |id, documents|
201
- "\n document id `#{id}` (#{documents.count} times)"
202
- end.join
203
- }\n"
201
+ "Expected index `#{type_name}` not to be updated, but it was with #{@updated.map(&:values).flatten.group_by { |documents| documents[:_id] }.map do |id, documents|
202
+ "\n document id `#{id}` (#{documents.count} times)"
203
+ end.join}\n"
204
204
  end
205
205
  end
206
206
 
207
207
  def agnostic_stub
208
208
  if defined?(Mocha) && RSpec.configuration.mock_framework.to_s == 'RSpec::Core::MockingAdapters::Mocha'
209
- "type.stubs(:bulk).with"
209
+ 'type.stubs(:bulk).with'
210
210
  else
211
- "allow(type).to receive(:bulk)"
211
+ 'allow(type).to receive(:bulk)'
212
212
  end
213
213
  end
214
214
 
215
- def extract_documents *args
215
+ def extract_documents(*args)
216
216
  options = args.extract_options!
217
217
 
218
218
  expected_count = options[:times] || options[:count]
@@ -230,7 +230,7 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
230
230
  end]
231
231
  end
232
232
 
233
- def compare_attributes expected, real
233
+ def compare_attributes(expected, real)
234
234
  expected.inject(true) do |result, (key, value)|
235
235
  equal = if value.is_a?(Array) && real[key].is_a?(Array)
236
236
  array_difference(value, real[key]) && array_difference(real[key], value)
@@ -243,12 +243,11 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
243
243
  end
244
244
  end
245
245
 
246
- def array_difference first, second
246
+ def array_difference(first, second)
247
247
  difference = first.to_ary.dup
248
248
  second.to_ary.each do |element|
249
- if index = difference.index(element)
250
- difference.delete_at(index)
251
- end
249
+ index = difference.index(element)
250
+ difference.delete_at(index) if index
252
251
  end
253
252
  difference.none?
254
253
  end
@@ -4,21 +4,21 @@ module Chewy
4
4
  include Comparable
5
5
  attr_reader :major, :minor, :patch
6
6
 
7
- def initialize version
8
- @major, @minor, @patch = *(version.to_s.split('.', 3) + [0]*3).first(3).map(&:to_i)
7
+ def initialize(version)
8
+ @major, @minor, @patch = *(version.to_s.split('.', 3) + [0] * 3).first(3).map(&:to_i)
9
9
  end
10
10
 
11
11
  def to_s
12
12
  [major, minor, patch].join('.')
13
13
  end
14
14
 
15
- def <=> other
15
+ def <=>(other)
16
16
  other = self.class.new(other) unless other.is_a?(self.class)
17
17
  [
18
18
  major <=> other.major,
19
19
  minor <=> other.minor,
20
20
  patch <=> other.patch
21
- ].detect { |c| c != 0 } || 0
21
+ ].detect(&:nonzero?) || 0
22
22
  end
23
23
  end
24
24
  end
@@ -7,10 +7,11 @@ module Chewy
7
7
  included do
8
8
  singleton_class.delegate :explain, :query_mode, :filter_mode, :post_filter_mode,
9
9
  :timeout, :limit, :offset, :highlight, :min_score, :rescore, :facets, :script_score,
10
- :boost_factor, :random_score, :field_value_factor, :decay, :aggregations,
10
+ :boost_factor, :weight, :random_score, :field_value_factor, :decay, :aggregations,
11
11
  :suggest, :none, :strategy, :query, :filter, :post_filter, :boost_mode,
12
12
  :score_mode, :order, :reorder, :only, :types, :delete_all, :find, :total,
13
- :total_count, :total_entries, to: :all
13
+ :total_count, :total_entries, :unlimited, :script_fields, :track_scores, :preference,
14
+ to: :all
14
15
  end
15
16
 
16
17
  module ClassMethods
@@ -18,11 +19,12 @@ module Chewy
18
19
  query_class.scopes.last || query_class.new(self)
19
20
  end
20
21
 
21
- def search_string query, options = {}
22
+ def search_string(query, options = {})
22
23
  options = options.merge(
23
24
  index: all._indexes.map(&:index_name),
24
25
  type: all._types.map(&:type_name),
25
- q: query)
26
+ q: query
27
+ )
26
28
  Chewy.client.search(options)
27
29
  end
28
30
 
@@ -42,7 +44,7 @@ module Chewy
42
44
  end
43
45
  end
44
46
 
45
- def delegate_scoped source, destination, methods
47
+ def delegate_scoped(source, destination, methods)
46
48
  methods.each do |method|
47
49
  destination.class_eval do
48
50
  define_method method do |*args, &block|
@@ -7,18 +7,21 @@ begin
7
7
  require 'resque'
8
8
  require 'chewy/strategy/resque'
9
9
  rescue LoadError
10
+ nil
10
11
  end
11
12
 
12
13
  begin
13
14
  require 'sidekiq'
14
15
  require 'chewy/strategy/sidekiq'
15
16
  rescue LoadError
17
+ nil
16
18
  end
17
19
 
18
20
  begin
19
21
  require 'active_job'
20
22
  require 'chewy/strategy/active_job'
21
23
  rescue LoadError
24
+ nil
22
25
  end
23
26
 
24
27
  module Chewy
@@ -42,7 +45,7 @@ module Chewy
42
45
  @stack.last
43
46
  end
44
47
 
45
- def push name
48
+ def push(name)
46
49
  result = @stack.push resolve(name).new
47
50
  debug "[#{@stack.size}] <- #{current.name}"
48
51
  result
@@ -55,7 +58,7 @@ module Chewy
55
58
  result
56
59
  end
57
60
 
58
- def wrap name
61
+ def wrap(name)
59
62
  stack = push(name)
60
63
  yield
61
64
  ensure
@@ -64,14 +67,13 @@ module Chewy
64
67
 
65
68
  private
66
69
 
67
- def debug string
68
- if Chewy.logger && Chewy.logger.debug?
69
- line = caller.detect { |line| line !~ %r{lib/chewy/strategy.rb:|lib/chewy.rb:} }
70
- Chewy.logger.debug(["DEBUG: Chewy strategies stack: #{string}", line.sub(/:in\s.+$/, '')].join(' @ '))
71
- end
70
+ def debug(string)
71
+ return unless Chewy.logger && Chewy.logger.debug?
72
+ line = caller.detect { |l| l !~ %r{lib/chewy/strategy.rb:|lib/chewy.rb:} }
73
+ Chewy.logger.debug(["DEBUG: Chewy strategies stack: #{string}", line.sub(/:in\s.+$/, '')].join(' @ '))
72
74
  end
73
75
 
74
- def resolve name
76
+ def resolve(name)
75
77
  "Chewy::Strategy::#{name.to_s.camelize}".safe_constantize or raise "Can't find update strategy `#{name}`"
76
78
  rescue NameError => ex
77
79
  # WORKAROUND: Strange behavior of `safe_constantize` with mongoid gem
@@ -16,8 +16,8 @@ module Chewy
16
16
  @stash = {}
17
17
  end
18
18
 
19
- def update type, objects, options = {}
20
- ActiveSupport::Deprecation.warn("`urgent: true` option is deprecated and is not effective inside `:atomic` strategy, use `Chewy.strategy(:urgent)` strategy instead") if options.key?(:urgent)
19
+ def update(type, objects, options = {})
20
+ ActiveSupport::Deprecation.warn('`urgent: true` option is deprecated and is not effective inside `:atomic` strategy, use `Chewy.strategy(:urgent)` strategy instead') if options.key?(:urgent)
21
21
 
22
22
  @stash[type] ||= []
23
23
  @stash[type] |= type.send(:build_root).id ? Array.wrap(objects) : type.adapter.identify(objects)
@@ -11,17 +11,17 @@ module Chewy
11
11
  def name
12
12
  self.class.name.demodulize.underscore.to_sym
13
13
  end
14
+
14
15
  # This method called when some model tries to update index
15
16
  #
16
- def update type, objects, options = {}
17
- raise UndefinedUpdateStrategy.new(type)
17
+ def update(type, _objects, _options = {})
18
+ raise UndefinedUpdateStrategy, type
18
19
  end
19
20
 
20
21
  # This method called when strategy pops from the
21
22
  # strategies stack
22
23
  #
23
- def leave
24
- end
24
+ def leave; end
25
25
  end
26
26
  end
27
27
  end
@@ -8,8 +8,7 @@ module Chewy
8
8
  # end
9
9
  #
10
10
  class Bypass < Base
11
- def update type, objects, options = {}
12
- end
11
+ def update(type, objects, options = {}); end
13
12
  end
14
13
  end
15
14
  end
@@ -13,6 +13,8 @@ module Chewy
13
13
  class Worker
14
14
  include ::Sidekiq::Worker
15
15
 
16
+ sidekiq_options queue: :chewy
17
+
16
18
  def perform(type, ids, options = {})
17
19
  type.constantize.import!(ids, options)
18
20
  end
@@ -9,7 +9,7 @@ module Chewy
9
9
  # end
10
10
  #
11
11
  class Urgent < Base
12
- def update type, objects, options = {}
12
+ def update(type, objects, _options = {})
13
13
  type.import!(Array.wrap(objects))
14
14
  end
15
15
  end
@@ -13,7 +13,7 @@ require 'chewy/type/adapter/sequel'
13
13
 
14
14
  module Chewy
15
15
  class Type
16
- IMPORT_OPTIONS_KEYS = [:batch_size, :bulk_size, :refresh, :consistency, :replication]
16
+ IMPORT_OPTIONS_KEYS = [:batch_size, :bulk_size, :refresh, :consistency, :replication, :raw_import, :journal].freeze
17
17
 
18
18
  include Search
19
19
  include Mapping
@@ -24,66 +24,72 @@ module Chewy
24
24
  include Witchcraft
25
25
  include Import
26
26
 
27
- singleton_class.delegate :index_name, :client, to: :index
27
+ singleton_class.delegate :index_name, :_index_name, :derivable_index_name, :client, to: :index
28
28
 
29
29
  class_attribute :_default_import_options
30
30
  self._default_import_options = {}
31
31
 
32
- # Chewy index current type belongs to. Defined inside `Chewy.create_type`
33
- #
34
- def self.index
35
- raise NotImplementedError
36
- end
32
+ class << self
33
+ # Chewy index current type belongs to. Defined inside `Chewy.create_type`
34
+ #
35
+ def index
36
+ raise NotImplementedError
37
+ end
37
38
 
38
- # Current type adapter. Defined inside `Chewy.create_type`, derived from
39
- # `Chewy::Index.define_type` arguments.
40
- #
41
- def self.adapter
42
- raise NotImplementedError
43
- end
39
+ # Current type adapter. Defined inside `Chewy.create_type`, derived from
40
+ # `Chewy::Index.define_type` arguments.
41
+ #
42
+ def adapter
43
+ raise NotImplementedError
44
+ end
44
45
 
45
- # Returns type name string
46
- #
47
- def self.type_name
48
- adapter.type_name
49
- end
46
+ # Returns type name string
47
+ #
48
+ def type_name
49
+ adapter.type_name
50
+ end
50
51
 
51
- # Returns list of public class methods defined in current type
52
- #
53
- def self.scopes
54
- public_methods - Chewy::Type.public_methods
55
- end
52
+ # Returns list of public class methods defined in current type
53
+ #
54
+ def scopes
55
+ public_methods - Chewy::Type.public_methods
56
+ end
56
57
 
57
- def self.default_import_options(params)
58
- params.assert_valid_keys(IMPORT_OPTIONS_KEYS)
59
- self._default_import_options = _default_import_options.merge(params)
60
- end
58
+ def default_import_options(params)
59
+ params.assert_valid_keys(IMPORT_OPTIONS_KEYS)
60
+ self._default_import_options = _default_import_options.merge(params)
61
+ end
61
62
 
62
- def self.method_missing(method, *args, &block)
63
- if index.scopes.include?(method)
64
- define_singleton_method method do |*args, &block|
65
- all.scoping { index.public_send(method, *args, &block) }
63
+ def method_missing(method, *args, &block)
64
+ if index.scopes.include?(method)
65
+ define_singleton_method method do |*method_args, &method_block|
66
+ all.scoping { index.public_send(method, *method_args, &method_block) }
67
+ end
68
+ send(method, *args, &block)
69
+ else
70
+ super
66
71
  end
67
- send(method, *args, &block)
68
- else
69
- super
70
72
  end
71
- end
72
73
 
73
- def self.const_missing(name)
74
- to_resolve = "#{self.to_s}::#{name}"
75
- to_resolve[index.to_s] = ''
74
+ def respond_to_missing?(method, _)
75
+ index.scopes.include?(method) || super
76
+ end
77
+
78
+ def const_missing(name)
79
+ to_resolve = "#{self}::#{name}"
80
+ to_resolve[index.to_s] = ''
76
81
 
77
- @__resolved_constants ||= {}
82
+ @__resolved_constants ||= {}
78
83
 
79
- if to_resolve.empty? || @__resolved_constants[to_resolve]
84
+ if to_resolve.empty? || @__resolved_constants[to_resolve]
85
+ super
86
+ else
87
+ @__resolved_constants[to_resolve] = true
88
+ to_resolve.constantize
89
+ end
90
+ rescue NotImplementedError
80
91
  super
81
- else
82
- @__resolved_constants[to_resolve] = true
83
- to_resolve.constantize
84
92
  end
85
- rescue NotImplementedError
86
- super
87
93
  end
88
94
  end
89
95
  end