diff --git a/elasticsearch-model/lib/elasticsearch/model/proxy.rb b/elasticsearch-model/lib/elasticsearch/model/proxy.rb index 955280fb..6fdf6632 100644 --- a/elasticsearch-model/lib/elasticsearch/model/proxy.rb +++ b/elasticsearch-model/lib/elasticsearch/model/proxy.rb @@ -115,15 +115,18 @@ def initialize(target) @target = target end - # Delegate methods to `@target` + def ruby2_keywords(*) # :nodoc: + end if RUBY_VERSION < "2.7" + + # Delegate methods to `@target`. As per [the Ruby 3.0 explanation for keyword arguments](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/), the only way to work on Ruby <2.7, and 2.7, and 3.0+ is to use `ruby2_keywords`. # - def method_missing(method_name, *arguments, &block) + ruby2_keywords def method_missing(method_name, *arguments, &block) target.respond_to?(method_name) ? target.__send__(method_name, *arguments, &block) : super end # Respond to methods from `@target` # - def respond_to?(method_name, include_private = false) + def respond_to_missing?(method_name, include_private = false) target.respond_to?(method_name) || super end diff --git a/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb b/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb index e9d9712c..f21baad3 100644 --- a/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb +++ b/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb @@ -31,6 +31,10 @@ def bar 'insta barr' end + def keyword_method(foo: 'default value') + foo + end + def as_json(options) {foo: 'bar'} end @@ -98,7 +102,6 @@ def changes_to_save end context 'when instances are cloned' do - let!(:model) do DummyProxyModel.new end @@ -121,4 +124,9 @@ def changes_to_save expect(duplicate).to eq(duplicate_target) end end + + it 'forwards keyword arguments to target methods' do + expect(DummyProxyModel.new.__elasticsearch__.keyword_method(foo: 'bar')).to eq('bar') + end + end