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

Implement STI on User model #64959

Open
wants to merge 7 commits into
base: staging
Choose a base branch
from
Open

Implement STI on User model #64959

wants to merge 7 commits into from

Conversation

nicklathe
Copy link
Contributor

@nicklathe nicklathe commented Apr 1, 2025

This PR adds STI (Single Table Inheritance) to our User model. There are two new classes introduced, Teacher and Student that both inherit from the User base class. One benefit of implementing STI on the User model is that it allows us organize the model along the axis of user_type and move student and teacher specific logic into their respective classes.

This functionality should not break existing usage. For example, you can continue to create a new user by calling User.create(user_type: "Teacher"). You can also now instead call Teacher.create(), as STI provides additional class methods.

This PR adds the following changes:

  • Creates teacher.rb and student.rb and associated classes
  • Adds STI inheritance column override (default is type, this overrides that to be user_type
  • Adds find_sti_class method override
  • Adds logic to set the user class in user_builder.rb
  • Updates unit tests to handle the introduction of the new STI classes

Links

Testing story

You can test locally, and will be able to create new teacher and student accounts as before, as well as downgrade/upgrade account styles (Teacher -> Student, Student -> Teacher.

Deployment strategy

Follow-up work

Privacy

Security

Caching

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

assert_select '#user_age'
assert_select '#user_us_state', false
assert_select '#user_gender_student_input', false
assert_select '#student_age'
Copy link
Contributor Author

@nicklathe nicklathe Apr 3, 2025

Choose a reason for hiding this comment

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

Rails prepends the user class for these attributes because the form builder knows the object is a User model. However since a user is now either a Teacher or Student class, these attributes are now being prepended with student_ rather than user_. This is probably obvious to those very familiar with Rails, but I was unaware of this prepending logic.

  - Add STI definitions to user.rb
  - Add Student and Teacher classes
  - Fix unit tests in user_test.rb

Signed-off-by: Nick Lathe <[email protected]>
  - Partial registration user builder
  - Password resetter by email
@nicklathe nicklathe force-pushed the nicklathe/user-sti-v2 branch from 8d26809 to 6fdd7c1 Compare April 7, 2025 14:58
@nicklathe nicklathe marked this pull request as ready for review April 10, 2025 02:58
@nicklathe nicklathe requested a review from a team April 10, 2025 23:16
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