I have been trying to get as close to SRP in my code as possible. I have a situation where I need to log leads when interactions happen for a contact. A lead may be created or updated. There are three different states that I would like to set leads to.
- Active - new interested interactions causes active lead to be created
- Converted - a existing lead is updated to converted if the product was purchased - this is managed with conversion flag
- Stale - a existing lead is updated to stale if the contact is not interested
class LeadGeneration
def initialize contact_id, options={}
@lead = Lead.find_or_initialize_by(contact_id: contact_id)
options.reverse_merge!(conversion: false, interested:true)
@conversion = options[:conversion]
@interested = options[:interested]
end
def call
if conversion?
convert_lead
else
process_lead
end
end
private
def lead
@lead
end
def conversion?
@conversion
end
def interested?
@interested
end
def convert_lead
lead.converted! unless lead.new_record? # new leads cannot be saved as converted
end
def process_lead
if interested?
activate_lead
else
stale_lead
end
end
def activate_lead
lead.active!
end
def stale_lead
lead.stale! unless lead.new_record? # new leads cannot be saved as stale
end
end
Right now I feel like I should be splitting up converting a lead and managing stale/active states. I'm not really sure what to do and tests are a little confusing right now.