-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Lead Promotion Logic to Improve Separation of Concerns #1398
base: master
Are you sure you want to change the base?
Conversation
def promote | ||
@account, @opportunity, @contact = @lead.promote(params.permit!) | ||
def promote | ||
@account, @opportunity, @contact = promote_lead(@lead, params.permit!) |
Check warning
Code scanning / Brakeman
Specify exact keys allowed for mass assignment instead of using permit! which allows any keys. Warning
@@ -115,8 +116,8 @@ | |||
|
|||
# PUT /leads/1/promote | |||
#---------------------------------------------------------------------------- | |||
def promote | |||
@account, @opportunity, @contact = @lead.promote(params.permit!) | |||
def promote |
Check notice
Code scanning / Rubocop
Keep indentation straight. Note
@@ -115,8 +116,8 @@ | |||
|
|||
# PUT /leads/1/promote | |||
#---------------------------------------------------------------------------- | |||
def promote | |||
@account, @opportunity, @contact = @lead.promote(params.permit!) | |||
def promote |
Check notice
Code scanning / Rubocop
Use 2 spaces for indentation. Note
def promote | ||
@account, @opportunity, @contact = @lead.promote(params.permit!) | ||
def promote | ||
@account, @opportunity, @contact = promote_lead(@lead, params.permit!) |
Check notice
Code scanning / Rubocop
Use 2 spaces for indentation. Note
@@ -270,4 +271,4 @@ | |||
end | |||
|
|||
ActiveSupport.run_load_hooks(:fat_free_crm_leads_controller, self) | |||
end | |||
end |
Check notice
Code scanning / Rubocop
Checks trailing blank lines and final newline. Note
module LeadPromotionHelper | ||
# Promote the lead by creating a contact and optional opportunity. Upon | ||
# successful promotion Lead status gets set to :converted. | ||
#---------------------------------------------------------------------------- |
Check notice
Code scanning / Rubocop
Avoid trailing whitespace. Note
# Promote the lead by creating a contact and optional opportunity. Upon | ||
# successful promotion Lead status gets set to :converted. | ||
#---------------------------------------------------------------------------- | ||
def promote_lead(lead, params) |
Check notice
Code scanning / Rubocop
Use 2 spaces for indentation. Note
def promote_lead(lead, params) | ||
account_params = params[:account] || {} | ||
opportunity_params = params[:opportunity] || {} | ||
|
Check notice
Code scanning / Rubocop
Avoid trailing whitespace. Note
account = Account.create_or_select_for(lead, account_params) | ||
opportunity = Opportunity.create_for(lead, account, opportunity_params) | ||
contact = Contact.create_for(lead, account, opportunity, params) | ||
|
Check notice
Code scanning / Rubocop
Avoid trailing whitespace. Note
[account, opportunity, contact] | ||
end | ||
end | ||
|
Check notice
Code scanning / Rubocop
Avoid trailing whitespace. Note
@@ -184,4 +170,4 @@ | |||
end | |||
|
|||
ActiveSupport.run_load_hooks(:fat_free_crm_lead, self) | |||
end | |||
end |
Check notice
Code scanning / Rubocop
Checks trailing blank lines and final newline. Note
Probably not mergable as is.
A service object would perhaps be better fit for this Even then, the litmus test is still the migration costs vs benefits. |
Pull Request Summary
This pull request refactors the promotion logic for
Leads
to align with best practices in software design, specifically addressing concerns around bounded contexts and the single responsibility principle.Changes Made:
Created
LeadsPromotionHelper.rb
:Updated
LeadsController
:LeadsPromotionHelper.rb
for handling lead promotions.Removed promotion logic from
Lead.rb
:Lead.rb
handling responsibilities outside its bounded context.Lead
model.Reasoning:
The previous implementation violated Domain-Driven Design (DDD) principles, as the
Lead
model managed adjustments toAccount
,Opportunity
, andContact
. This approach entangled responsibilities, increasing complexity and making the code harder to maintain.By isolating the promotion logic into
LeadsPromotionHelper.rb
, we: