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

#270 Result method_missing #289

Merged
merged 1 commit into from
Oct 27, 2020
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
20 changes: 17 additions & 3 deletions lib/active_interactor/interactor/result.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'active_support/core_ext/module/delegation'

module ActiveInteractor
module Interactor
# An Interactor Result object.
Expand Down Expand Up @@ -41,7 +39,6 @@ class Result
# @!method successful?
# @see ActiveInteractor::Interactor::State#success?
delegate :called, :called!, :fail?, :failure?, :success?, :successful?, to: :state
delegate_missing_to :context

# Initializes a new instance of {Result}
#
Expand Down Expand Up @@ -104,12 +101,29 @@ def handle_listed_errors(errors)
errors.each { |error| handle_errors(error) }
end

def method_missing(method_name, *args, &block)
context.public_send(method_name, *args, &block)
ActiveInteractor::Deprecation::V2.deprecation_warning(
method_name,
'calling #context methods on an ActiveInteractor::Interactor::Result is deprecated use #context instead',
caller
)
rescue NoMethodError
super
end

def resolve_errors!
all_errors = context.errors.uniq.compact
context.errors.clear
all_errors.each { |error| context.errors.add(error[0], error[1]) }
end

def respond_to_missing?(method_name, include_private = false)
return true if method_name[/.*(?==\z)/m]

context.respond_to?(method_name, include_private) || super
end

def rollback_called!
state.called.reverse_each do |interactor|
next if state.rolled_back.include? interactor
Expand Down
30 changes: 30 additions & 0 deletions spec/active_interactor/interactor/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,34 @@
end
end
end

describe 'calling context methods' do
subject(:context_method) { result.bar = 'bar' }

before do
allow(ActiveInteractor::Deprecation::V2).to receive(:deprecation_warning).and_return(true)
end

let(:result) { described_class.new(context_object) }
let(:context_object) { build_context.new }

it { expect { context_method }.not_to raise_error }

it 'is expected to call the approriate context method' do
context_method

expect(context_object.bar).to eq 'bar'
end

it 'is expected to warn about deprecation' do
context_method

expect(ActiveInteractor::Deprecation::V2).to have_received(:deprecation_warning)
.with(
:bar=,
'calling #context methods on an ActiveInteractor::Interactor::Result is deprecated use #context instead',
any_args
)
end
end
end