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

Define steps in ConsentForm #482

Merged
merged 4 commits into from
Sep 6, 2023
Merged

Define steps in ConsentForm #482

merged 4 commits into from
Sep 6, 2023

Conversation

tvararu
Copy link
Member

@tvararu tvararu commented Sep 5, 2023

Replace the on: context based validation with conditional validation that relies on specifying a form_step. This also makes the validations fire off for previous steps, and at the end for the final record step.

Unifies the *_params methods in the EditController into one update_params method.

required_for_step? takes an optional exact parameter, for transient steps like the school one that ask a question but don't actually save a field to the database (and shouldn't be re-validated on other steps).

Overall approach based on:
https://www.joshmcarthur.com/2014/12/23/rails-multistep-forms.html

@tvararu tvararu requested a review from a team as a code owner September 5, 2023 10:15
@tvararu tvararu temporarily deployed to manage-vacci-refactor-e-ucq2wr September 5, 2023 10:16 Inactive
app/controllers/consent_forms_controller.rb Outdated Show resolved Hide resolved
app/models/consent_form.rb Show resolved Hide resolved
Replace the `on:` context based validation with conditional validation
that relies on specifying a `form_step`. This also makes the validations
fire off for previous steps, and at the end for the final `record` step.

Unifies the `*_params` methods in the EditController into one
`update_params` method.

`required_for_step?` takes an optional `exact` parameter, for transient
steps like the `school` one that ask a question but don't actually save
a field to the database (and shouldn't be re-validated on other steps).

Overall approach based on:
https://www.joshmcarthur.com/2014/12/23/rails-multistep-forms.html
Copy link
Contributor

@misaka misaka left a comment

Choose a reason for hiding this comment

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

I feel like the methods in the model, at least, should have a spec. Can we (and should we) also test that the right validations are triggered at the right time, to test stuff like the school step having exact set?

@misaka
Copy link
Contributor

misaka commented Sep 5, 2023

Sorry, I should say that those comments aside, I really like this change ... stuff like sticking the form steps into the model is good, even if at first glance the form step s are a controller concern ... the fact that they are here with the step validations is good.

@tvararu tvararu temporarily deployed to manage-vacci-refactor-e-ucq2wr September 5, 2023 16:07 Inactive
Allows us to use a more terse syntax for testing validations.
@tvararu tvararu temporarily deployed to manage-vacci-refactor-e-ucq2wr September 6, 2023 08:31 Inactive
Comment on lines 32 to 36
# expect(cf).to validate_comparison_of(:date_of_birth)
# .is_less_than(Time.zone.today)
# .is_greater_than_or_equal_to(22.years.ago.to_date)
# .is_less_than_or_equal_to(3.years.ago.to_date)
# .on(:update)
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure why this doesn't work

NoMethodError:
       undefined method `validate_comparison_of' for #<RSpec::ExampleGroups::ConsentForm::Validations::WhenFormStepIsDateOfBirth "example at ./spec/models/consent_form_spec.rb:30">

Copy link
Contributor

Choose a reason for hiding this comment

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

Looked into this ... looks like the most recent version of should-matchers, 5.3.0 from Dec 2022, doesn't include this matcher. This matcher was added earlier this year and hasn't been released yet. 🤦

thoughtbot/shoulda-matchers#1552

@tvararu tvararu temporarily deployed to manage-vacci-refactor-e-ucq2wr September 6, 2023 10:18 Inactive
Comment on lines +52 to +56
it do
should validate_inclusion_of(:is_this_their_school).in_array(
%w[yes no]
).on(:update)
end
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the linter's idea, not mine

Copy link
Contributor

@misaka misaka left a comment

Choose a reason for hiding this comment

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

Nice. required_for_step still makes me itch, but :shipit:

@tvararu tvararu merged commit ca59de5 into main Sep 6, 2023
@tvararu tvararu deleted the refactor-edit branch September 6, 2023 20:28
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.

2 participants