Skip to content

Commit

Permalink
Use direct access to operation result context in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac committed Sep 27, 2024
1 parent e931025 commit acb938d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-08-07 02:24:58 UTC using RuboCop version 1.65.0.
# on 2024-09-27 05:26:10 UTC using RuboCop version 1.65.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -27,7 +27,7 @@ Metrics/ClassLength:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 141
Max: 144

# Offense count: 3
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Expand Down
5 changes: 4 additions & 1 deletion lib/operations/form/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ def method_missing(name, *args, **kwargs)
build_attribute.form.new(*args, **kwargs)
elsif plural_build_attribute&.form
plural_build_attribute.form.new(*args, **kwargs)
elsif operation_result
operation_result.context[name]
end
end

def respond_to_missing?(name, *)
has_attribute?(name) ||
build_nested_form?(build_attribute_name(name)) ||
self.class.attributes[nested_attribute_name(name)]&.form
self.class.attributes[nested_attribute_name(name)]&.form ||
operation_result&.context&.key?(name)
end

def model_name
Expand Down
4 changes: 3 additions & 1 deletion spec/operations/form/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
end
let(:attributes) { {} }
let(:messages) { {} }
let(:options) { {} }
let(:options) { { operation_result: instance_double(Operations::Result, context: { books: [] }) } }

before do
stub_const("Dummy::Form", form_class)
Expand Down Expand Up @@ -285,6 +285,7 @@
end

specify { expect(form.build_tag).to be_nil }
specify { expect(form.books).to eq [] }
specify { expect(form.foobar).to be_nil }
end

Expand All @@ -295,6 +296,7 @@
specify { expect(form).to respond_to(:build_post) }
specify { expect(form).to respond_to(:author_attributes=) }
specify { expect(form).to respond_to(:posts_attributes=) }
specify { expect(form).to respond_to(:books) }
specify { expect(form).not_to respond_to(:foobar) }
specify { expect(form).not_to respond_to(:build_tag) }
specify { expect(form).not_to respond_to(:tags_attributes=) }
Expand Down

0 comments on commit acb938d

Please sign in to comment.