-
Notifications
You must be signed in to change notification settings - Fork 4
Delegation
By default, low_card_tables
makes all attributes of your low-card table show up as attributes on the referring table.
These attributes are defined as methods in a dynamically-created module called, for a model User
, User::LowCardDynamicMethods
. This method is included into your module. As a result, you can override these methods in your model, and that'll work just fine; you can also call super
in them, and it will call the appropriate low-card method.
No matter what, you can always access these attributes through the association name — for example, my_user.status.gender
will always work, as will my_user.status.gender = 'female'
.
If you don't like full delegation, you have several options:
class User < ActiveRecord::Base
has_low_card_table :status, :delegate => nil
end
This will turn off delegation entirely. Your only access to the low-card attributes is via (e.g.) my_user.status.gender
.
class User < ActiveRecord::Base
has_low_card_table :status, :delegate => [ :deleted, :gender ]
end
This will delegate only the deleted
and gender
attributes. Other attributes must be referred to via the association.
class User < ActiveRecord::Base
has_low_card_table :status, :delegate => { :except => [ :deleted, :gender ] }
end
This will delegate everything except the deleted
and gender
attributes.
class User < ActiveRecord::Base
has_low_card_table :status, :delegate => [ :deleted, :gender ]
end
You can also change the delegation prefix:
class User < ActiveRecord::Base
has_low_card_table :status, :prefix => true
end
Now these methods are called my_user.status_gender
, my_user.status_deleted
, and so on.
class User < ActiveRecord::Base
has_low_card_table :status, :prefix => 'foo'
end
Now these methods are called my_user.foo_gender
, my_user.foo_deleted
, and so on.
If you refer to multiple low-card tables from a single referring table, it's possible that there is more than one low-card table that defines a given attribute. In this case, the last definition wins. For example, if both user_statuses
and user_photo_statuses
have an attribute deleted
:
class User < ActiveRecord::Base
has_low_card_table :status
has_low_card_table :photo_status
end
Now my_user.deleted
will refer to my_user.photo_status.deleted
, and you can only access the status
from user_statuses
as my_user.status.deleted
. To avoid this situation, use :prefix
.
(This can also happen in the rare case where you refer to a single low-card table multiple times from the same referring table — for example, if you wanted to track user_status_id
and old_user_status_id
.)