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

[FYST-1704] Income/ donation forms: Inaccurate date selection #5570

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

squanto
Copy link
Contributor

@squanto squanto commented Feb 12, 2025

Link to pivotal/JIRA issue

Is PM acceptance required? (delete one)

  • Yes - don't merge until JIRA issue is accepted!

Reminder: merge main into this branch and get green tests before merging to main

What was done?

Update date accessible validations, so that all incoming parts of a date (day, month, and year) are validated together instead of piece by piece. This fixes cases where users enter invalid dates, like Feb 31st.

Copy link

Heroku app: https://gyr-review-app-5570-2ce2c8acb364.herokuapp.com/
View logs: heroku logs --app gyr-review-app-5570 (optionally add --tail)

@squanto squanto force-pushed the FYST-1704-all-states-income-donation-forms-date-field branch 2 times, most recently from 28a4708 to bc93673 Compare February 12, 2025 17:46
Copy link
Contributor

@arinchoi03 arinchoi03 left a comment

Choose a reason for hiding this comment

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

I think someone else @powersurge360 / @tahsinaislam who have worked on date_accessible in the past should review this but I'm trying to understand the code!

send("#{property}_day").to_i,
)
)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens if day/month/year attribute is missing? (fails the .present? check?) Is there validation further down the line that will give an error that the date is invalid b/c something is missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if one of the values is missing, then the date won't get set / updated, and it'll fail the inclusion check in it's model

@@ -33,6 +33,5 @@ class Az321Contribution < ApplicationRecord
validates :date_of_contribution,
inclusion: {
in: TAX_YEAR.beginning_of_year..TAX_YEAR.end_of_year
},
presence: true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

why is the presence check removed?

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 this the inclusion validation serves as a presence check as well, as nil values fall outside of the range. It made it so I didn't have to duplicate the error message

@@ -4,7 +4,6 @@ module DateAccessible
TAX_YEAR = Date.new(Rails.configuration.statefile_current_tax_year)

included do
private
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the significance of making these not private methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The methods from the module are class methods, so they can't be private, and I believe this line has no effect

subject.expiration_date_day = 21
subject.expiration_date_year = nil
subject.valid?
expect(subject.expiration_date).to eq(Date.new(year, 2, 1))
Copy link
Contributor

@arinchoi03 arinchoi03 Feb 13, 2025

Choose a reason for hiding this comment

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

hmm i don't understand this example -- the year is nil but the date is still set to year -- is it b/c year nil is invalid so therefore it was never written to the expiration date?

also should maybe split into a "it" block to describe what we're trying to test here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's right, the intent is to say that b/c ``year nil` is invalid, it won't write to the date. I'll split this out for better clarity on what it's testing

@mrotondo
Copy link
Contributor

mrotondo commented Feb 13, 2025

One blocking issue I noticed: This breaks the _year, _month, and _day attr_readers, which means that the forms no longer show the previously entered dates when rendering. To see this, go through a state_id page like /primary-state-id, enter valid issue & expiration dates, continue, then go back and note that the date selects are blank. Happy to pair on this - there's definitely some ruby wizardry at work, since I can't tell what the attr_readers are backed by but they seem to work when they've been used to set the date parts on an in-memory object, so clearly they're backed by something.

Edit: As @arinchoi03 mentioned, @powersurge360 would also be a good pair on this, as he created date_accessible

self.define_method("#{property}_day") do
send(property)&.day
end
attr_reader :"#{property}_month", :"#{property}_year", :"#{property}_day"
Copy link
Contributor

@powersurge360 powersurge360 Feb 13, 2025

Choose a reason for hiding this comment

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

I think this is the spot to repair the issue Mike surfaced. I think something like

self.define_method("#{property}_month") do
  super || send(property)&.month
end

should do the trick. Or maybe you'll have to retrieve the ivar directly.

@squanto
Copy link
Contributor Author

squanto commented Feb 20, 2025

With the update to the attr accessors, it appears we're now able to view incorrect fields when rerendering @mrotondo

@squanto squanto force-pushed the FYST-1704-all-states-income-donation-forms-date-field branch from 0b36b7b to 097de8d Compare February 20, 2025 17:28
Copy link
Contributor

@powersurge360 powersurge360 left a comment

Choose a reason for hiding this comment

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

Looks great. I left two notes but the only one that I feel is a blocker is translating a validation string in the new default validator for the date.

self.define_method("#{property}_day") do
send(property)&.day
self.instance_variable_get("@#{property}_day") || send(property)&.day
Copy link
Contributor

Choose a reason for hiding this comment

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

This will sidestep any other setters, as opposed to calling super. Intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's the intent - for this setter to default to the instance variable, with a backup of parsing the persisted value

define_method("#{property}_date_valid") do
date = send(property)
if date.present? && !Date.valid_date?(date.year, date.month, date.day)
errors.add(property, :invalid_date, message: "is not a valid calendar date")
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're doing validation, should be translated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

absolutely, thanks for the catch

@squanto squanto force-pushed the FYST-1704-all-states-income-donation-forms-date-field branch from 867a9a4 to fd87c4b Compare February 20, 2025 20:12
Copy link
Contributor

@powersurge360 powersurge360 left a comment

Choose a reason for hiding this comment

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

Issues addressed. Approved.

Co-authored-by: Hugo Melo <[email protected]>
@squanto squanto force-pushed the FYST-1704-all-states-income-donation-forms-date-field branch from a9e4734 to 4b7f5ba Compare February 21, 2025 15:43
Co-authored-by: Hugo Melo <[email protected]>
@squanto squanto force-pushed the FYST-1704-all-states-income-donation-forms-date-field branch from 4b7f5ba to e164c23 Compare February 21, 2025 17:22
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.

4 participants