Class: Lead

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

Overview

Copyright © 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or 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



85
# File 'app/models/entities/lead.rb', line 85

def self.first_name_position ; "before" ; end

.per_pageObject

Default values provided through class methods.




84
# File 'app/models/entities/lead.rb', line 84

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.




144
145
146
147
148
# File 'app/models/entities/lead.rb', line 144

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

#convertObject




133
134
135
# File 'app/models/entities/lead.rb', line 133

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

#discard!(task) ⇒ Object

Discard a task from the lead.




152
153
154
# File 'app/models/entities/lead.rb', line 152

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

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




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

def full_name(format = nil)
  if format.nil? || format == "before"
    "#{self.first_name} #{self.last_name}"
  else
    "#{self.last_name}, #{self.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.




124
125
126
127
128
129
130
# File 'app/models/entities/lead.rb', line 124

def promote(params)
       = Account.create_or_select_for(self, params[:account])
  opportunity = Opportunity.create_for(self, , params[:opportunity])
  contact     = Contact.create_for(self, , opportunity, params)

  [, opportunity, contact]
end

#rejectObject




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

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

#save_with_permissions(params) ⇒ Object

Save the lead along with its permissions.




89
90
91
92
93
94
95
96
97
# File 'app/models/entities/lead.rb', line 89

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

#update_with_lead_counters(attributes) ⇒ Object

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




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

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

#update_with_permissions(attributes, users = nil) ⇒ Object

Deprecated: see update_with_lead_counters




101
102
103
104
# File 'app/models/entities/lead.rb', line 101

def update_with_permissions(attributes, users = nil)
  ActiveSupport::Deprecation.warn "lead.update_with_permissions is deprecated and may be removed from future releases, use user_ids and group_ids inside attributes instead and call lead.update_with_lead_counters"
  update_with_lead_counters(attributes)
end