GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS

Written by thoughtbot

Role Suggesting Name

Consider a CreditCard class:

class CreditCard < ActiveRecord::Base
  belongs_to :brand_manager, class_name: 'User'
end

class User < ActiveRecord::Base
  has_many :credit_cards, foreign_key: 'brand_manager_id'
end

It is used like this:

brand_manager.credit_cards
credit_card.brand_manager

User in this context would be too generic. In the domain-specific language of this application, the people with credit cards are referred to as "brand managers".

Why not name the model BrandManager?

In this case, User is overloaded to handle three different roles using simple flags on the model. This allows us to use Clearance normally and keeps authentication and standard user vocabulary available where it makes sense.

class Impression < ActiveRecord::Base
  belongs_to :campaign, class_name: 'Offer'
end

In this example, the object plays two different roles depending on to whom it is being displayed. The object is an offer to recipients but its Role Suggesting Name is campaign to advertisers and impressions.

Learn more about Role Suggesting Name in Philippe Hanrigou's talk, What the Ruby craftsman can learn from the Smalltalk master.