Refactor Lead Promotion Logic to Improve Separation of Concerns#1398
Refactor Lead Promotion Logic to Improve Separation of Concerns#1398TimothyKuehn wants to merge 3 commits intofatfreecrm:masterfrom
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
| #---------------------------------------------------------------------------- | ||
| def promote | ||
| @account, @opportunity, @contact = @lead.promote(params.permit!) | ||
| def promote |
Check notice
Code scanning / Rubocop
Keep indentation straight. Note
| #---------------------------------------------------------------------------- | ||
| 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
|
|
||
| ActiveSupport.run_load_hooks(:fat_free_crm_leads_controller, self) | ||
| end | ||
| end No newline at end of file |
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 | ||
|
No newline at end of file |
Check notice
Code scanning / Rubocop
Avoid trailing whitespace. Note
|
|
||
| ActiveSupport.run_load_hooks(:fat_free_crm_lead, self) | ||
| end | ||
| end No newline at end of file |
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
Leadsto 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.rbfor handling lead promotions.Removed promotion logic from
Lead.rb:Lead.rbhandling responsibilities outside its bounded context.Leadmodel.Reasoning:
The previous implementation violated Domain-Driven Design (DDD) principles, as the
Leadmodel 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: