55502f40dc8b7c769880b10874abc9d0

Thanks

def create
    @lead = Lead.new(params[:lead])
    @contact = @lead.contact_detail = ContactDetail.new(params[:contact_detail])
    @crm_add_info = @lead.crm_additional_info = CrmAdditionalInfo.new(params[:crm_additional_info])
    account = @lead.check_account_existance(params[:account][:name])
    if account.nil?
      @account = Account.new(:name => params[:account][:name] ,:parent_id => 6 ,:account_type_id => 5)
      @lead.contact_detail.default = 1
      @account.owner = current_user
    else
      @account = account  
    end
    @crm_add_info.account = @contact.account = @lead.account = @account
    @lead.owner = current_user
    if @lead.save
      redirect_to @lead
    else
      render :action => 'new'
    end
  end

Refactorings

No refactoring yet !

B8ba61cc84ecb63c859435be28547dfb

steved

April 28, 2011, April 28, 2011 11:22, permalink

No rating. Login to rate!

Basic suggestion is to push code into model. I'm assuming contact, account, etc. are all associations on Lead? Might want to refactor model code further into smaller (more testable) methods.

# controller
def create
  @lead = Lead.new(params[:lead])
  if @lead.do_lots_of_stuff_and_save(params, current_user)
    redirect_to @lead
  else
    render :action => 'new'
  end
end

# app/models/lead.rb

def do_lots_of_stuff_and_save(params, current_user)
  self.contact_detail = ContactDetail.new(params[:contact_detail])
  self.crm_additional_info = CrmAdditionalInfo.new(params[:crm_additional_info])
  set_account(params, current_user)
  crm_add_info.account = contact.account = account
  self.owner = current_user
  save
end

def set_account(params, current_user)
  self.account = check_account_existance(params[:account][:name])
  if account.nil?
    self.account = Account.new(:name => params[:account][:name] ,:parent_id => 6 ,:account_type_id => 5)
    self.contact_detail.default = 1
    account.owner = current_user
  end
end
373e9915e7c3ec0455a2778c6cd0630d

Morey

November 24, 2011, November 24, 2011 04:19, permalink

No rating. Login to rate!

Good point. I hadn't thuohgt about it quite that way. :)

ERROR_BAD_DUPLICATES
373e9915e7c3ec0455a2778c6cd0630d

Morey

November 24, 2011, November 24, 2011 04:19, permalink

No rating. Login to rate!

Good point. I hadn't thuohgt about it quite that way. :)

ERROR_BAD_DUPLICATES
8e10bb8e40984415c70054c21accc66a

Ziggy

November 26, 2011, November 26, 2011 17:04, permalink

No rating. Login to rate!

For the love of God, keep writing these atrilecs.

ERROR_BAD_DUPLICATES
8e10bb8e40984415c70054c21accc66a

Ziggy

November 26, 2011, November 26, 2011 17:04, permalink

No rating. Login to rate!

For the love of God, keep writing these atrilecs.

ERROR_BAD_DUPLICATES
8e10bb8e40984415c70054c21accc66a

Ziggy

November 26, 2011, November 26, 2011 17:04, permalink

No rating. Login to rate!

For the love of God, keep writing these atrilecs.

ERROR_BAD_DUPLICATES

Your refactoring





Format Copy from initial code

or Cancel