Skip to content

Commit

Permalink
Add support for Ruby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
lowang committed Jan 28, 2021
1 parent ac0e5ef commit 07a6b41
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 25 deletions.
15 changes: 13 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ workflows:
- rspec-ruby-27-activerecord60
- rspec-ruby-27-activerecord52
- rspec-ruby-27-mongoid
# TODO elastic 7
# TODO sequel?
- rspec-ruby-30-activerecord61

commands:
rspec-test:
Expand Down Expand Up @@ -201,3 +200,15 @@ jobs:
GEMFILE: gemfiles/rails.5.2.mongoid.6.4.gemfile
steps:
- rspec-test

rspec-ruby-30-activerecord61:
docker:
- image: circleci/ruby:3.0
- image: docker.elastic.co/elasticsearch/elasticsearch:5.6.7
environment:
<<: *es56-env
working_directory: ~/repo
environment:
GEMFILE: gemfiles/rails.6.1.activerecord.gemfile
steps:
- rspec-test
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# master

* Update gemspec dependencies for Rails. Update CI gemfiles and matrix to tests against current LTS Rails versions.
# Version 5.2.0

## Changes

* Add support for Ruby 3 (@lowang, #734)
* Update gemspec dependencies for Rails. Update CI gemfiles and matrix to tests against current LTS Rails versions. (Bhacaz, #733)

# Version 5.1.0

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ gem 'guard-rspec'

gem 'redcarpet'
gem 'yard'

eval(File.read('gemfiles/ruby3.gemfile'), nil, 'gemfiles/ruby3.gemfile') if RUBY_VERSION >= '3.0.0' # rubocop:disable Security/Eval
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ Or install it yourself as:

### Ruby

Chewy is compatible with MRI 2.4-2.7. Ruby 3 support is currently in development.
Chewy is compatible with MRI 2.4-3.0¹.

> ¹ Ruby 3 is only supported with Rails 6.1
### Elasticsearch

Expand Down
2 changes: 1 addition & 1 deletion chewy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| # rubocop:disable BlockLength
spec.add_development_dependency 'elasticsearch-extensions'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'resque_spec'
spec.add_development_dependency 'rspec', '~> 3.7.0'
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', '0.52.1'
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails.6.1.activerecord.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ gem "will_paginate", require: false
gem "parallel", require: false
gem 'rspec_junit_formatter', '~> 0.4.1'

eval(File.read('gemfiles/ruby3.gemfile'), nil, 'gemfiles/ruby3.gemfile') if RUBY_VERSION >= '3.0.0'

gemspec path: "../"
10 changes: 10 additions & 0 deletions gemfiles/ruby3.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# kwargs support was added here:
# https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/method_double.rb#L66
# but this change was not released yet so for the time being use main branch
gem 'rspec', github: 'rspec/rspec', branch: 'main'
gem 'rspec-core', github: 'rspec/rspec-core', branch: 'main'
gem 'rspec-expectations', github: 'rspec/rspec-expectations', branch: 'main'
gem 'rspec-mocks', github: 'rspec/rspec-mocks', branch: 'main'
gem 'rspec-support', github: 'rspec/rspec-support', branch: 'main'
# extracted from stdlib
gem 'rexml'
4 changes: 2 additions & 2 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def exists?
# Suffixed index names might be used for zero-downtime mapping change, for example.
# Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
#
def create(*args)
create!(*args)
def create(*args, **kwargs)
create!(*args, **kwargs)
rescue Elasticsearch::Transport::Transport::Errors::BadRequest
false
end
Expand Down
3 changes: 2 additions & 1 deletion lib/chewy/search/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def self.storages
# limit: Chewy::Search::Parameters::Offset.new(10)
# )
# @param initial [{Symbol => Object, Chewy::Search::Parameters::Storage}]
def initialize(initial = {})
def initialize(initial = {}, **kinitial)
@storages = Hash.new do |hash, name|
hash[name] = self.class.storages[name].new
end
initial = initial.deep_dup.merge(kinitial)
initial.each_with_object(@storages) do |(name, value), result|
storage_class = self.class.storages[name]
storage = value.is_a?(storage_class) ? value : storage_class.new(value)
Expand Down
6 changes: 3 additions & 3 deletions lib/chewy/type/observe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def update_index(type_name, *args, &block)
update_proc = Observe.update_proc(type_name, *args, &block)

if Chewy.use_after_commit_callbacks
after_commit(callback_options, &update_proc)
after_commit(**callback_options, &update_proc)
else
after_save(callback_options, &update_proc)
after_destroy(callback_options, &update_proc)
after_save(**callback_options, &update_proc)
after_destroy(**callback_options, &update_proc)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Chewy
VERSION = '5.1.1'.freeze
VERSION = '5.2.0'.freeze
end
20 changes: 11 additions & 9 deletions spec/chewy/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,43 +180,45 @@
end
end

# rubocop:disable Style/BracesAroundHashParameters
specify do
expect(EventsIndex::Event.root.compose(
id: 1, category: {id: 2, licenses: {id: 3, name: 'Name'}}
)).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => {'id' => 3, 'name' => 'Name'}})
expect(EventsIndex::Event.root.compose({
id: 1, category: {id: 2, licenses: {id: 3, name: 'Name'}}
})).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => {'id' => 3, 'name' => 'Name'}})
end

specify do
expect(EventsIndex::Event.root.compose(id: 1, category: [
expect(EventsIndex::Event.root.compose({id: 1, category: [
{id: 2, 'licenses' => {id: 3, name: 'Name1'}},
{id: 4, licenses: nil}
])).to eq('id' => 1, 'category' => [
]})).to eq('id' => 1, 'category' => [
{'id' => 2, 'licenses' => {'id' => 3, 'name' => 'Name1'}},
{'id' => 4, 'licenses' => nil.as_json}
])
end

specify do
expect(EventsIndex::Event.root.compose('id' => 1, category: {id: 2, licenses: [
expect(EventsIndex::Event.root.compose({'id' => 1, category: {id: 2, licenses: [
{id: 3, name: 'Name1'}, {id: 4, name: 'Name2'}
]})).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => [
]}})).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => [
{'id' => 3, 'name' => 'Name1'}, {'id' => 4, 'name' => 'Name2'}
]})
end

specify do
expect(EventsIndex::Event.root.compose(id: 1, category: [
expect(EventsIndex::Event.root.compose({id: 1, category: [
{id: 2, licenses: [
{id: 3, 'name' => 'Name1'}, {id: 4, name: 'Name2'}
]},
{id: 5, licenses: []}
])).to eq('id' => 1, 'category' => [
]})).to eq('id' => 1, 'category' => [
{'id' => 2, 'licenses' => [
{'id' => 3, 'name' => 'Name1'}, {'id' => 4, 'name' => 'Name2'}
]},
{'id' => 5, 'licenses' => []}
])
end
# rubocop:enable Style/BracesAroundHashParameters

specify do
expect(EventsIndex::Event.root.compose(
Expand Down
6 changes: 4 additions & 2 deletions spec/chewy/search/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,20 @@
describe '#merge!' do
let(:first) { described_class.new(offset: 10, order: 'foo') }
let(:second) { described_class.new(limit: 20, offset: 20, order: 'bar') }
let(:first_clone) { first.clone }
let(:second_clone) { second.clone }

specify do
expect { first.merge!(second) }.to change { first.clone }
.to(described_class.new(limit: 20, offset: 20, order: %w[foo bar]))
end
specify { expect { first.merge!(second) }.not_to change { second.clone } }
specify { expect { first.merge!(second) }.not_to change { second_clone } }

specify do
expect { second.merge!(first) }.to change { second.clone }
.to(described_class.new(limit: 20, offset: 10, order: %w[bar foo]))
end
specify { expect { second.merge!(first) }.not_to change { first.clone } }
specify { expect { second.merge!(first) }.not_to change { first_clone } }

context 'spawns new storages for the merge' do
let(:names) { %i[limit offset order] }
Expand Down
4 changes: 2 additions & 2 deletions spec/chewy/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ def self.by_index(index)
filter { match name: "Name#{index}" }
end

field :name, KEYWORD_FIELD
field :name, **KEYWORD_FIELD
field :rating, type: :integer
end

define_type Country do
field :name, KEYWORD_FIELD
field :name, **KEYWORD_FIELD
field :rating, type: :integer
end
end
Expand Down

0 comments on commit 07a6b41

Please sign in to comment.