Skip to content

Add failing test for default form builder - #1090

Merged
Spone merged 6 commits into
ViewComponent:mainfrom
raisedevs:add-failing-test-for-default-form-builder
Dec 7, 2021
Merged

Spone merged 6 commits into
ViewComponent:mainfrom
raisedevs:add-failing-test-for-default-form-builder

Conversation

@finalburn

@finalburn finalburn commented Oct 7, 2021 •

Copy link
Copy Markdown
Collaborator

Summary

When form helpers are called directly, the default form builder is not used and must be specified. For example, calling this:

<%= fields do |f| %>
  <%= f.text_field :hello_world %>
<% end %>

does not use the default form builder. Either manually specifying the builder: argument to fields or calling helpers.fields (as I have in the tests here) works, though.

I feel that this is a little unintuitive - it's odd that form helpers do work directly within ViewComponent but do not inherit the default form builder specified by the controller. I feel that they should either raise an error or be delegated to helpers by default.

@finalburn
finalburn requested a review from a team as a code owner October 7, 2021 16:05
@finalburn

Copy link
Copy Markdown
Collaborator Author

I'd appreciate it if someone could add a hacktoberfest-accepted label to the PR. Thanks!

@joelhawksley

Copy link
Copy Markdown
Member

@boardfish thanks for opening this up. Let's take the delegation route ❤️

@finalburn

Copy link
Copy Markdown
Collaborator Author

@joelhawksley In line with a couple of other issues I've seen, particularly #1099, I'm wondering if this is symptomatic of a wider issue. It looks like it's possible to call form helpers, turbo_frame_tag, and other helpers without going via helpers, but they're not always guaranteed to work.

My worry with delegation is that we would have to list all the helpers it covers. I don't think ViewComponent needs to know which helpers the view has available to it, but method_missing, delegate_missing_to, and other solutions that would help us figure this out aren't performant.

@finalburn

Copy link
Copy Markdown
Collaborator Author

@joelhawksley Did you have any further thoughts on that, or do we still want to go with delegation?

@joelhawksley

Copy link
Copy Markdown
Member

@boardfish thanks for the ping. Thinking about this some more, I think we should document the existing incompatibility and push folks towards passing in a builder explicitly.

Allowing global state to change the rendered output of a component seems like a tricky thing to reason about and I think we should tread carefully in making changes that support such behavior.

@finalburn

Copy link
Copy Markdown
Collaborator Author

Good to know. I'll wrap this up with that in mind soon.

@finalburn

Copy link
Copy Markdown
Collaborator Author

@joelhawksley I've rebased, and documented the issue in 0dfac56. Hopefully that makes this good to go.

Comment thread docs/known_issues.md Resolved
Comment thread docs/CHANGELOG.md Resolved
@finalburn

Copy link
Copy Markdown
Collaborator Author

Rebased and passing the linter. Good to go?

@Spone

Spone commented Dec 7, 2021

Copy link
Copy Markdown
Collaborator

Yes, I'll merge!

@Spone
Spone merged commit 3042037 into ViewComponent:main Dec 7, 2021

@rmacklin rmacklin left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@boardfish I've added a few questions, but I might just be misunderstanding things - apologies if that's the case

Comment thread docs/CHANGELOG.md

## main

* Add failing test for default form builder and documentation around known issue.

@rmacklin rmacklin Dec 7, 2021 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit confusing - it's not actually a failing test anymore, right? (It looks like the test added is a passing test that shows how to work around the known issue.)

Comment thread docs/known_issues.md

## Forms don't use the default form builder

Calls to form helpers such as `form_with` in ViewComponents [don't use the default form builder](https://github.com/github/view_component/pull/1090#issue-753331927). This is by design, as it allows global state to change the rendered output of a component. Instead, consider passing a form builder into form helpers via the `builder` argument:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"This is by design, as it allows global state to change the rendered output of a component" is also kind of confusing to me - it seems to read as if we designed ViewComponent such that global state can change the rendered output, when it's actually been designed to avoid that.

If I'm understanding it correctly, perhaps something like "This is by design because doing so would allow..." could be a clearer way to communicate this. What do you think?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, the phrasing is awkward.

Suggested change
Calls to form helpers such as `form_with` in ViewComponents [don't use the default form builder](https://github.com/github/view_component/pull/1090#issue-753331927). This is by design, as it allows global state to change the rendered output of a component. Instead, consider passing a form builder into form helpers via the `builder` argument:
Calls to form helpers such as `form_with` in ViewComponents [don't use the default form builder](https://github.com/github/view_component/pull/1090#issue-753331927). This is by design, as it would otherwise allow global state to change the rendered output of a component. Instead, consider passing a form builder into form helpers via the `builder` argument:

with_controller_class DefaultFormBuilderController do
render_inline(DefaultFormBuilderComponent.new)

assert_text("changed by default form builder")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on my first comment, I'm wondering if it'd be helpful to complement this test with one that doesn't use helpers.fields in order to show that the effect would be the opposite in that case (i.e. the default form builder would be ignored). Is that the known issue that we want to document?

(Perhaps it'd even help to add a third test that doesn't use helpers. but does pass the builder explicitly, such that the test cases would document all the ways to go about it.)

@finalburn

Copy link
Copy Markdown
Collaborator Author

Thanks for your feedback @rmacklin - I'll create a follow-up PR to address these issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants