Skip to content

Commit

Permalink
Add ignore_blank option to field method. Drop support for Ruby 2.5 (
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalina-Vakulchyk authored and Ivan Rabotyaga committed Apr 2, 2021
1 parent 473f7eb commit fe5e411
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 37 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ inherit_from: .rubocop_todo.yml

AllCops:
NewCops: enable
TargetRubyVersion: 2.6

Layout/AccessModifierIndentation:
EnforcedStyle: outdent
Expand Down
23 changes: 11 additions & 12 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2021-03-11 12:44:08 UTC using RuboCop version 1.11.0.
# on 2021-04-02 12:44:05 UTC using RuboCop version 1.11.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -13,16 +13,15 @@ Gemspec/RequiredRubyVersion:
Exclude:
- 'chewy.gemspec'

# Offense count: 3
# Offense count: 2
# Configuration parameters: AllowedMethods.
# AllowedMethods: enums
Lint/ConstantDefinitionInBlock:
Exclude:
- 'lib/chewy.rb'
- 'lib/chewy/rspec/update_index.rb'
- 'spec/chewy/config_spec.rb'

# Offense count: 12
# Offense count: 10
# Configuration parameters: AllowComments, AllowEmptyLambdas.
Lint/EmptyBlock:
Exclude:
Expand All @@ -42,30 +41,30 @@ Lint/MissingSuper:
- 'lib/chewy/type/adapter/object.rb'
- 'lib/chewy/type/adapter/orm.rb'

# Offense count: 36
# Offense count: 35
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 41

# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 269
Max: 267

# Offense count: 13
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 12

# Offense count: 44
# Offense count: 43
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 29

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 174
Max: 158

# Offense count: 18
# Configuration parameters: IgnoredMethods.
Expand All @@ -89,7 +88,7 @@ Style/DocumentDynamicEvalDefinition:
- 'lib/chewy/type/crutch.rb'
- 'lib/chewy/type/witchcraft.rb'

# Offense count: 63
# Offense count: 58
Style/Documentation:
Enabled: false

Expand All @@ -99,7 +98,7 @@ Style/EvalWithLocation:
Exclude:
- 'spec/chewy/index_spec.rb'

# Offense count: 208
# Offense count: 191
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Expand All @@ -113,7 +112,7 @@ Style/GuardClause:
- 'lib/chewy.rb'
- 'spec/support/active_record.rb'

# Offense count: 12
# Offense count: 10
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
Expand All @@ -125,7 +124,7 @@ Style/IfUnlessModifier:
- 'lib/chewy/type/witchcraft.rb'
- 'spec/support/active_record.rb'

# Offense count: 64
# Offense count: 53
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

### New Features

* [#778](https://github.com/toptal/chewy/pull/778): Add `ignore_blank` option to `field` method ([@Vitalina-Vakulchyk][]):
* `true` by default for the `geo_point` type
* `false` by default for other types

### Changes

* [#778](https://github.com/toptal/chewy/pull/778): **(Breaking)** Drop support for Ruby 2.5 ([@Vitalina-Vakulchyk][]):
* [#776](https://github.com/toptal/chewy/pull/776): **(Breaking)** Removal of unnecessary features and integrations ([@Vitalina-Vakulchyk][]):
* `aws-sdk-sqs` / `shoryuken`
* `mongoid`
* `sequel`
* `will_paginate`
* `resque`

* [#769](https://github.com/toptal/chewy/pull/769): **(Breaking)** Removal of deprecated methods and rake tasks ([@Vitalina-Vakulchyk][]):
* `Chewy::Index.index_params` is removed, use `Chewy::Index.specification_hash` instead
* `Chewy::Index.derivable_index_name` is removed, use `Chewy::Index.derivable_name` instead
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Chewy is an ODM (Object Document Mapper), built on top of the [the official Elas
* [Witchcraft™ technology](#witchcraft-technology)
* [Raw Import](#raw-import)
* [Index creation during import](#index-creation-during-import)
* [Skip record fields during import](#skip-record-fields-during-import)
* [Default values for different types](#default-values-for-different-types)
* [Journaling](#journaling)
* [Types access](#types-access)
* [Index manipulation](#index-manipulation)
Expand Down Expand Up @@ -97,7 +99,7 @@ Or install it yourself as:

### Ruby

Chewy is compatible with MRI 2.5-3.0¹.
Chewy is compatible with MRI 2.6-3.0¹.

> ¹ Ruby 3 is only supported with Rails 6.1
Expand Down Expand Up @@ -660,6 +662,25 @@ By default, when you perform import Chewy checks whether an index exists and cre
You can turn off this feature to decrease Elasticsearch hits count.
To do so you need to set `skip_index_creation_on_import` parameter to `false` in your `config/chewy.yml`

### Skip record fields during import

You can use `ignore_blank: true` to skip fields that return `true` for the `.blank?` method:

```ruby
define_type Country do
field :id
field :cities, ignore_blank: true do
field :id
field :name
field :surname, ignore_blank: true
field :description
end
end
```

#### Default values for different types

By default `ignore_blank` is false on every type except `geo_point`.

### Journaling

Expand Down
10 changes: 10 additions & 0 deletions lib/chewy/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def mappings_hash
def compose(*objects)
result = evaluate(objects)

return {} if result.blank? && ignore_blank?

if children.present? && !multi_field?
result = if result.respond_to?(:to_ary)
result.to_ary.map { |item| compose_children(item, *objects) }
Expand All @@ -53,6 +55,14 @@ def compose(*objects)

private

def geo_point?
@options[:type].to_s == 'geo_point'
end

def ignore_blank?
@options.fetch(:ignore_blank) { geo_point? }
end

def evaluate(objects)
object = objects.first

Expand Down
8 changes: 3 additions & 5 deletions lib/chewy/search/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -988,11 +988,9 @@ def reset
def perform(additional = {})
request_body = render.merge(additional)
ActiveSupport::Notifications.instrument 'search_query.chewy', notification_payload(request: request_body) do
begin
Chewy.client.search(request_body)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
{}
end
Chewy.client.search(request_body)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
{}
end
end

Expand Down
30 changes: 13 additions & 17 deletions lib/chewy/type/syncer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ def missing_ids
def outdated_ids
return [] if source_data.blank? || index_data.blank? || !@type.supports_outdated_sync?

@outdated_ids ||= begin
if @parallel
parallel_outdated_ids
else
linear_outdated_ids
end
@outdated_ids ||= if @parallel
parallel_outdated_ids
else
linear_outdated_ids
end
end

Expand All @@ -147,19 +145,17 @@ def index_data
end

def source_and_index_data
@source_and_index_data ||= begin
if @parallel
::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
result = ::Parallel.map(%i[source index], @parallel, &SOURCE_OR_INDEX_DATA_WORKER.curry[self, @type])
::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
if result.first.keys.first == :source
[result.first.values.first, result.second.values.first]
else
[result.second.values.first, result.first.values.first]
end
@source_and_index_data ||= if @parallel
::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
result = ::Parallel.map(%i[source index], @parallel, &SOURCE_OR_INDEX_DATA_WORKER.curry[self, @type])
::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
if result.first.keys.first == :source
[result.first.values.first, result.second.values.first]
else
[fetch_source_data, fetch_index_data]
[result.second.values.first, result.first.values.first]
end
else
[fetch_source_data, fetch_index_data]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/type/witchcraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def replace_lvar(node, old_variable, new_variable)
def replace_send(node, variable)
if node.is_a?(Parser::AST::Node)
if node.type == :send && node.children[0].nil?
node.updated(nil, [Parser::AST::Node.new(:lvar, [variable]), *node.children[1..-1]])
node.updated(nil, [Parser::AST::Node.new(:lvar, [variable]), *node.children[1..]])
else
node.updated(nil, node.children.map { |child| replace_send(child, variable) })
end
Expand Down
Loading

0 comments on commit fe5e411

Please sign in to comment.