Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions elasticsearch-model/lib/elasticsearch/model/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,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

Expand Down
8 changes: 8 additions & 0 deletions elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def bar
'insta barr'
end

def keyword_method(foo: 'default value')
foo
end

def as_json(options)
{foo: 'bar'}
end
Expand Down Expand Up @@ -104,4 +108,8 @@ 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