Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor Lead Promotion Logic to Improve Separation of Concerns #1398

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/controllers/entities/leads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
class LeadsController < EntitiesController
include LeadPromotionHelper
before_action :get_data_for_sidebar, only: :index
autocomplete :account, :name, full: true

Expand Down Expand Up @@ -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

Layout/IndentationConsistency: Inconsistent indentation detected.

Check notice

Code scanning / Rubocop

Use 2 spaces for indentation. Note

Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.
@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

Specify exact keys allowed for mass assignment instead of using permit! which allows any keys.

Check notice

Code scanning / Rubocop

Use 2 spaces for indentation. Note

Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
@accounts = Account.my(current_user).order('name')
@stage = Setting.unroll(:opportunity_stage)

Expand Down Expand Up @@ -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

Layout/TrailingEmptyLines: Final newline missing.
23 changes: 23 additions & 0 deletions app/helpers/lead_promotion_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
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

Layout/TrailingWhitespace: Trailing whitespace detected.
def promote_lead(lead, params)

Check notice

Code scanning / Rubocop

Use 2 spaces for indentation. Note

Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
account_params = params[:account] || {}
opportunity_params = params[:opportunity] || {}

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
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

Layout/TrailingWhitespace: Trailing whitespace detected.
[account, opportunity, contact]
end
end

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
16 changes: 1 addition & 15 deletions app/models/entities/lead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,6 @@
end
end

# Promote the lead by creating contact and optional opportunity. Upon
# successful promotion Lead status gets set to :converted.
#----------------------------------------------------------------------------
def promote(params)
account_params = params[:account] || {}
opportunity_params = params[:opportunity] || {}

account = Account.create_or_select_for(self, account_params)
opportunity = Opportunity.create_for(self, account, opportunity_params)
contact = Contact.create_for(self, account, opportunity, params)

[account, opportunity, contact]
end

#----------------------------------------------------------------------------
def convert
update_attribute(:status, "converted")
Expand Down Expand Up @@ -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

Layout/TrailingEmptyLines: Final newline missing.
Loading