rails && users && roles
Reply
Reply to all Reply to allForward Forward Print Add Chris to Contacts
list Delete this message Show original Message text garbled?
Chris K.
to Lee
show details
11:58 am (3½ hours ago)
Okay, I’ve been thing about this whole user/role business with Rails.
Simple scenario:
A restaurateur has multiple restaurants
A restaurant has multiple restauranteurs
A restaurant has multiple chefs
Both restaurateurs and chefs can be any other kind of user as well, i.e.
‘marketer, member’
The base data for all users is the same:
create table user
(
email
password
first_name
last_name
)
create table restaurateur
(
is_admin
is_owner
)
Rails STI won’t work because a user is not limited to a single
user_type.
So, has_many :through seems the way to go. However, I want concrete
models for each user type, i.e. Restaurateur, Member, Chef, etc.
class user
has_many :restaurateurs
has_many :restaurants, :through => :restaurateurs
end
class restaurant
has_many :restaurateurs
has_many :users, :through => :restaurateurs
end
class restaurateur
belongs_to :user
belongs_to :restaurant
end
With this setup, if I wanted to iterate through each restaurateur and
print out the email address, I would have to do something like
restaurant.restaurateurs.each do
puts restaurateur.user.email
end
I want to clean the api up so that I can just do restaurateur.email.
My thought was to create a UserProxy module:
module UserProxy
def name
self.user.name
end
…
end
and then include the proxy in each user based class, i.e.
class Restaurteur
include UserProxy
end
This cleans up the api significantly and allows the proxy to be used
between all user types.
Of course, if the user class changes, so does the proxy, which is a big
pain in the ass for maintenance.
So, I’m interested in what your thoughts are:
- This particular solution
- How to build the proxy so that I don’t have to define each
method/attribute by hand within the proxy, i.e. how to copy the User
definition to the UserProxy in the most efficient manner.
I’ve looked into delegate, Forwardable and missing_method. I can’t seem
to get the syntax right or I’m just missing some concept.
Any ideas, thoughts, code (especially code … would be appreciated.
Cheers