Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ruby3 method proxying rebase. Closes #571 (rebased on master) #573

Merged
Merged
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
37 changes: 28 additions & 9 deletions lib/dynamoid/associations/single_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,37 @@ def ==(other)
target == other
end

# Delegate methods we don't find directly to the target.
#
# @private
# @since 0.2.0
def method_missing(method, *args)
if target.respond_to?(method)
target.send(method, *args)
else
super
if ::RUBY_VERSION < '2.7'
# Delegate methods we don't find directly to the target.
#
# @private
# @since 0.2.0
def method_missing(method, *args, &block)
if target.respond_to?(method)
target.send(method, *args, &block)
else
super
end
end
else
# Delegate methods we don't find directly to the target.
#
# @private
# @since 0.2.0
def method_missing(method, *args, **kwargs, &block)
if target.respond_to?(method)
target.send(method, *args, **kwargs, &block)
else
super
end
end
end

# @private
def respond_to_missing?(method_name, include_private = false)
target.respond_to?(method_name, include_private) || super
end

# @private
def nil?
target.nil?
Expand Down
6 changes: 6 additions & 0 deletions spec/app/models/magazine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ class Magazine
has_one :sponsor

belongs_to :owner, class_name: 'User', inverse_of: :books

def publish(advertisements:, free_issue: false)
result = advertisements * (free_issue ? 2 : 1)
result = yield(result) if block_given?
result
end
end
20 changes: 16 additions & 4 deletions spec/dynamoid/associations/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@
expect(user.books).to include magazine
end

it 'behaves like the object it is trying to be' do
expect(magazine.subscriptions).to include subscription
subscription.magazine.update_attribute(:size, 101)
context 'proxied behavior' do
let(:proxy) do
expect(magazine.subscriptions).to include(subscription)
subscription.magazine
end

it 'forwards dynamoid methods' do
proxy.update_attribute(:size, 101)

expect(Magazine.first.size).to eq 101
expect(Magazine.first.size).to eq 101
end

it 'forwards business-logic methods' do
expect(proxy.respond_to?(:publish)).to eq true
expect(proxy.publish(advertisements: 10)).to eq 10
expect(proxy.publish(advertisements: 10, free_issue: true) { |c| c *= 42 }).to eq 840
end
end
end

Expand Down