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

Forms using yaaf #3290

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Forms using yaaf #3290

wants to merge 1 commit into from

Conversation

justinlittman
Copy link
Contributor

Why was this change made? 🤔

How was this change tested? 🤨

⚡ ⚠ If this change involves consuming from or writing to another service (or shared file system), run integration test create_object_h2_spec.rb and/or test manually in [stage|qa] environment, in addition to specs. ⚡

Does your change introduce accessibility violations? 🩺

⚡ ⚠ Please ensure this change does not introduce accessibility violations (at the WCAG A or AA conformance levels); if it does, include a rationale. See the Infrastructure accessibility guide for more detail. ⚡

@@ -85,6 +85,7 @@ gem "sneakers", "~> 2.11" # rabbitMQ background processing
gem "state_machines-activerecord"
gem "strip_attributes"
gem "turbo-rails", "~> 1.0"
gem "view_component", "~> 2.56.2" # https://github.com/github/view_component/issues/1390
gem "view_component", "~> 3.5"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Surprise! We're using an old version of View Components, which causes all sorts of problems for form rendering.

gem "whenever", require: false # Work around https://github.com/javan/whenever/issues/831
gem "yaaf" # Form objects
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A tiny gem that helps with form objects.

@@ -0,0 +1,29 @@
<div data-controller="<%= controller %>" data-<%= controller %>-wrapper-selector-value=".<%= controller %>-wrapper">
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Part of the strategy is to abstract nested forms. This provides a nested form component that handles creating the template, rendering the rows, providing the elements needed by the nested forms stimulus controller, and rendering the add another button.

<%# Inserted elements will be injected before that target. %>
<div data-<%= controller %>-target="target"></div>

<% if add_another_button? %>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allow a customized add another button to be provided, but provide default.

@field = field
@clazz = clazz
@row = row
@ordered = ordered
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This allows specifying that the nested forms are ordered (e.g, authors).

end
end

def affiliations_forms
Copy link
Contributor Author

Choose a reason for hiding this comment

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

<model>_forms is used to provide a blank form when there are none. This is expected by the nested form component.

weight: contributor.weight)
end

def self.reject_all_blank?(params)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some form objects, some form attributes should be ignored when determining if a form is blank. (These attributes always have values.)

@@ -0,0 +1,150 @@
module Forms
class Work < Base
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Work is kinda complicated because it needs to handle both the work and work version models. It also has multiple nested forms (authors, contributors, email). This is an example of where a parent model is used.

@@ -49,5 +49,7 @@ class Application < Rails::Application
config.report_data = false
end
end

config.view_component.capture_compatibility_patch_enabled = true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

View Components struggles with forms. This patches some of those problems.

"@hotwired/stimulus": "^3.2.1",
"stimulus-autocomplete": "^3.1.0"
"stimulus-autocomplete": "^3.1.0",
"stimulus-rails-nested-form": "^4.1.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using stimulus-rails-nested-form instead of rolling our own.

@justinlittman
Copy link
Contributor Author

For the purposes of this proof-of-concept, I implemented a wokes controller (that sits alongside the existing works controller).

To create a new work (with the wokes controller): /collections/1/wokes/new
To edit an existing work (with the wokes controller): /wokes/17/edit

@justinlittman
Copy link
Contributor Author

I only implemented part of the work form:
image

However, it includes authors/contributors which requires double nesting (for affiliations), polymorphism (for authors vs contributors), and ordering (for authors), so, in theory, everything else is easy.

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.

1 participant