-
Notifications
You must be signed in to change notification settings - Fork 501
Implement STI on User model #64959
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
Conversation
assert_select '#user_age' | ||
assert_select '#user_us_state', false | ||
assert_select '#user_gender_student_input', false | ||
assert_select '#student_age' |
There was a problem hiding this comment.
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
8d26809
to
6fdd7c1
Compare
- Move class mapping to find_sti_class method - Simplify factory
TYPE_TO_STI_CLASS_MAP = { | ||
TYPE_TEACHER => ::Teacher, | ||
TYPE_STUDENT => ::Student, | ||
'staff' => ::Teacher # Powerschool sends through 'staff' instead of 'teacher' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know if Powerschool SSO is still in use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, I was just maintaining current functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that feels like something we should do in a followup PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it can wait for sure. I'm running a few queries to see if we can remove this later.
Interesting. I'm working through some failing unit tests with my most recent PR feedback changes pushed. Will try to repro and validate this new bug. Thanks for testing and discovering this! |
user = ::User.new_with_session(user_params, session) | ||
|
||
sti_class = ::User.find_sti_class(user_params[:user_type]) | ||
user = user.becomes!(sti_class) if sti_class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User#become!
reinitializes the user instance, which breaks the tricky argument setup done by User.new_with_session
:
user = ::User.new_with_session(user_params, session) | |
sti_class = ::User.find_sti_class(user_params[:user_type]) | |
user = user.becomes!(sti_class) if sti_class | |
params = user_params | |
user_class = ::User.find_sti_class(params[:user_type]) || ::User | |
user = user_class.new_with_session(params, session) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, this is the fix! Thanks @artem-vavilov!
@@ -32,6 +35,7 @@ def call | |||
user_params[:school_info_attributes].permit(:school_id, :school_name, :school_type, :zip, :country) | |||
end | |||
when ::User::TYPE_STUDENT | |||
# binding.pry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# binding.pry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LTI teacher signup working for me now. Nice job on the quick fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicklathe Before merging this PR, please let me double-check the places where we manually convert the user instance into the required STI class, as it should work automatically
@artem-vavilov I just approved your linked PR to merge into this branch. |
Update User STI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! 🙌🏻
This reverts commit 0735a8b.
This PR adds STI (Single Table Inheritance) to our User model. There are two new classes introduced,
Teacher
andStudent
that both inherit from theUser
base class. One benefit of implementing STI on the User model is that it allows us organize the model along the axis ofuser_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 callTeacher.create()
, as STI provides additional class methods.This PR adds the following changes:
teacher.rb
andstudent.rb
and associated classestype
, this overrides that to beuser_type
find_sti_class
method overrideuser_builder.rb
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: