users and profiles
By: Johnathon Wright on: June 28, 2010
I find myself, yet again^n, considering users and profiles.
Say you have the following models:
- User
- Doctor
- Patient
Of course, the concept of user is not the same as the concept of a person. User might be more accurately called Login, since it could be a bot or a guest login used by 100 various anonymous people.
your views have access to currentuser. We'll make our lives easier by ssuming that currentuser is always populated with a valid user.
What I remember thinking last time I was in this bruhaha was that a user could have one or more profiles. That's what we conveniently have here. A user can be a doctor, and if so, they have a doctor profile... license number, type of practice, whatever. That same person could be a patient. If so, we'll have information like... emergency contact, lastcheckup, allergies.
So you can ask whether a user can see a patient. And obviously this falls under the User model since it's all about authentication and authorization.
--- ruby class User
def can_view?( patient ) (self == patient ) || (self.doctor && patient.doctor == self.doctor) end
end
ok... so now I want to welcome this user. If they are a doctor, I want to prepend their name with "Dr." firstname and lastname are on User.
--- ruby class User def fullname [title, firstname, lastname].join(' ') end
end
ok great. At some point I'm running through a list of doctors and I'll want to output their names.
--- ruby class Doctor def tos self.user.tos end
end
(a) we've already said that a user isn't necessarily a doctor; could be a group. (b) for this app, there's no guarantee a doctor even has a login.
I don't want to store firstname, lastname twice... so that leads me to the conclusion that I need a Person model.
Person firstname lastname dob blah
So then we get: Doctor#personid Patient#personid User#personid ???? maybe, instead of that... Person#userid
so then --- ruby class User def tos person.tos end end
class Person def tos title, firstname, last_name end end
class Doctor def tos person.tos end end
same for patient
ok this seems very sensible.