-
Notifications
You must be signed in to change notification settings - Fork 190
Has many through supports aliased names #329
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
Changes from all commits
306eb96
a26c70c
a17e921
ada3e8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,6 +234,56 @@ def define_doctor_classes | |
|
|
||
| expect(patient.physicians).to contain_exactly(physician1, physician2) | ||
| end | ||
|
|
||
| describe "with the :source option" do | ||
| before do | ||
| # NOTE: Removing the Patient#physicians association and adding Patient#doctors | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perfect |
||
| Patient._reflections.delete('physicians') | ||
| Patient.class_eval do | ||
| define_method(:physicians) { raise NoMethodError, "The #physicians association is removed in this spec, use #doctors" } | ||
| define_method(:physicians=) { |_| raise NoMethodError, "The #physicians association is removed in this spec, use #doctors" } | ||
| end | ||
| Patient.has_many :doctors, through: :appointments, source: :physician | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kbrock what do you think of this approach where we simply add the differently named associations and dont bother removing the "default" ones. I was kind of wanting to not have both associations present on the models in the spec contexts, it felt a bit nicer, but it's not quite necessary and doesn't really effect the functionality being tested. One other thought is that I could take the approach here but also explicitly remove the associations and added methods, something like: Appointment._reflections.delete('physician')
Appointment.class_eval do
define_method(:physician) { raise NoMethodError }
define_method(:physician=) { raise NoMethodError }
end
Appointment.belongs_to :doctor, class_name: 'Physician', foreign_key: :physician_id... but that seems like a lot for something that really doesn't "do anything"; specs will pass with or without it. Still, maybe worth it tho?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Just having a single association does feel cleaner. Either way, a comment is our friend. Depending upon how you roll, something to this effect: # NOTE: the physician association is handled by doctor and no longer a valid association. Removing it.
# NOTE: the physician association is handled by doctor and no longer a valid association. Please don't use in this testI think I prefer less code, but totally up to you. |
||
| end | ||
|
|
||
| it "finds ActiveHash records through the join model" do | ||
| patient = Patient.create! | ||
|
|
||
| physician = Physician.last | ||
| Appointment.create!(physician: physician, patient: patient) | ||
|
|
||
| expect(patient.doctors).to contain_exactly(physician) | ||
| end | ||
| end | ||
|
|
||
| describe ":through when the join model uses an aliased association" do | ||
| before do | ||
| # NOTE: Removing the Appointment#physician association and adding Appointment#doctor | ||
| Appointment._reflections.delete('physician') | ||
| Appointment.class_eval do | ||
| define_method(:physician) { raise NoMethodError, "The #physician association is removed in this spec, use #doctor" } | ||
| define_method(:physician=) { |_| raise NoMethodError, "The #physician association is removed in this spec, use #doctor" } | ||
| end | ||
| Appointment.belongs_to :doctor, class_name: 'Physician', foreign_key: :physician_id | ||
|
|
||
| # NOTE: Removing the Patient#physicians association and adding Patient#doctors | ||
| Patient._reflections.delete('physicians') | ||
| Patient.class_eval do | ||
| define_method(:physicians) { raise NoMethodError, "The #physicians association is removed in this spec, use #doctors" } | ||
| define_method(:physicians=) { |_| raise NoMethodError, "The #physicians association is removed in this spec, use #doctors" } | ||
| end | ||
| Patient.has_many :doctors, through: :appointments | ||
| end | ||
|
|
||
| it "finds ActiveHash records through the join model" do | ||
| patient = Patient.create! | ||
|
|
||
| physician = Physician.last | ||
| Appointment.create!(doctor: physician, patient: patient) | ||
|
|
||
| expect(patient.doctors).to contain_exactly(physician) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "with a lambda" do | ||
|
|
||
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.
not sure why, but I do feel this is much easier to read.
If you are having trouble with it, you can add back.