Class: Lead

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/entities/lead.rb

Overview

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

Schema Information

Table name: leads

id :integer not null, primary key user_id :integer campaign_id :integer assigned_to :integer first_name :string(64) default(""), not null last_name :string(64) default(""), not null access :string(8) default("Public") title :string(64) company :string(64) source :string(32) status :string(32) referred_by :string(64) email :string(64) alt_email :string(64) phone :string(32) mobile :string(32) blog :string(128) linkedin :string(128) facebook :string(128) twitter :string(128) rating :integer default(0), not null do_not_call :boolean default(FALSE), not null deleted_at :datetime created_at :datetime updated_at :datetime background_info :string(255) skype :string(128)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_name_positionObject



91
92
93
# File 'app/models/entities/lead.rb', line 91

def self.first_name_position
  "before"
end

.per_pageObject

Default values provided through class methods.



87
88
89
# File 'app/models/entities/lead.rb', line 87

def self.per_page
  20
end

Instance Method Details

#attach!(task) ⇒ Object

Attach a task to the lead if it hasn't been attached already.



148
149
150
# File 'app/models/entities/lead.rb', line 148

def attach!(task)
  tasks << task unless task_ids.include?(task.id)
end

#convertObject




137
138
139
# File 'app/models/entities/lead.rb', line 137

def convert
  update_attribute(:status, "converted")
end

#discard!(task) ⇒ Object

Discard a task from the lead.



154
155
156
# File 'app/models/entities/lead.rb', line 154

def discard!(task)
  task.update_attribute(:asset, nil)
end

#full_name(format = nil) ⇒ Object Also known as: name




159
160
161
162
163
164
165
# File 'app/models/entities/lead.rb', line 159

def full_name(format = nil)
  if format.nil? || format == "before"
    "#{first_name} #{last_name}"
  else
    "#{last_name}, #{first_name}"
  end
end

#promote(params) ⇒ Object

Promote the lead by creating contact and optional opportunity. Upon successful promotion Lead status gets set to :converted.



125
126
127
128
129
130
131
132
133
134
# File 'app/models/entities/lead.rb', line 125

def promote(params)
   = params[:account] || {}
  opportunity_params = params[:opportunity] || {}

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

  [, opportunity, contact]
end

#rejectObject




142
143
144
# File 'app/models/entities/lead.rb', line 142

def reject
  update_attribute(:status, "rejected")
end

#save_with_permissions(params) ⇒ Object

Save the lead along with its permissions.



97
98
99
100
101
102
103
104
105
# File 'app/models/entities/lead.rb', line 97

def save_with_permissions(params)
  self.campaign = Campaign.find(params[:campaign]) unless params[:campaign].blank?
  if params[:lead][:access] == "Campaign" && campaign # Copy campaign permissions.
    save_with_model_permissions(Campaign.find(campaign_id))
  else
    self.attributes = params[:lead]
    save
  end
end

#update_with_lead_counters(attributes) ⇒ Object

Update lead attributes taking care of campaign lead counters when necessary.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/entities/lead.rb', line 109

def update_with_lead_counters(attributes)
  if campaign_id == attributes[:campaign_id] # Same campaign (if any).
    self.attributes = attributes
    save
  else                                            # Campaign has been changed -- update lead counters...
    decrement_leads_count                         # ..for the old campaign...
    self.attributes = attributes                  # Assign new campaign.
    lead = save
    increment_leads_count                         # ...and now for the new campaign.
    lead
  end
end