Class: Contact

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/entities/contact.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: contacts

id              :integer         not null, primary key
user_id         :integer
lead_id         :integer
assigned_to     :integer
reports_to      :integer
first_name      :string(64)      default(""), not null
last_name       :string(64)      default(""), not null
access          :string(8)       default("Public")
title           :string(64)
department      :string(64)
source          :string(32)
email           :string(64)
alt_email       :string(64)
phone           :string(32)
mobile          :string(32)
fax             :string(32)
blog            :string(128)
linkedin        :string(128)
facebook        :string(128)
twitter         :string(128)
born_on         :date
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

.create_for(model, account, opportunity, params) ⇒ Object

Class methods.




173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/entities/contact.rb', line 173

def self.create_for(model, , opportunity, params)
  attributes = {
    lead_id:     model.id,
    user_id:     params[:account][:user_id] || .user_id,
    assigned_to: params[:account][:assigned_to],
    access:      params[:access]
  }
  %w[first_name last_name title source email alt_email phone mobile blog linkedin facebook twitter skype do_not_call background_info].each do |name|
    attributes[name] = model.send(name.intern)
  end

  contact = Contact.new(attributes)

  # Set custom fields.
  if model.class.respond_to?(:fields)
    model.class.fields.each do |field|
      contact.send "#{field.name}=", model.send(field.name) if contact.respond_to?(field.name)
    end
  end

  contact.business_address = Address.new(street1: model.business_address.street1, street2: model.business_address.street2, city: model.business_address.city, state: model.business_address.state, zipcode: model.business_address.zipcode, country: model.business_address.country, full_address: model.business_address.full_address, address_type: "Business") unless model.business_address.nil?

  # Save the contact only if the account and the opportunity have no errors.
  if .errors.empty? && opportunity.errors.empty?
    # Note: contact.account = account doesn't seem to work here.
    contact. = AccountContact.new(account: , contact: contact) unless .id.blank?
    if contact.access != "Lead" || model.nil?
      contact.save
    else
      contact.save_with_model_permissions(model)
    end
    contact.opportunities << opportunity unless opportunity.id.blank? # must happen after contact is saved
  end
  contact
end

.first_name_positionObject



122
123
124
# File 'app/models/entities/contact.rb', line 122

def self.first_name_position
  "before"
end

.per_pageObject

Default values provided through class methods.




118
119
120
# File 'app/models/entities/contact.rb', line 118

def self.per_page
  20
end

Instance Method Details

#attach!(attachment) ⇒ Object

Attach given attachment to the contact if it hasn’t been attached already.




157
158
159
# File 'app/models/entities/contact.rb', line 157

def attach!(attachment)
  send(attachment.class.name.tableize) << attachment unless send("#{attachment.class.name.downcase}_ids").include?(attachment.id)
end

#discard!(attachment) ⇒ Object

Discard given attachment from the contact.




163
164
165
166
167
168
169
# File 'app/models/entities/contact.rb', line 163

def discard!(attachment)
  if attachment.is_a?(Task)
    attachment.update_attribute(:asset, nil)
  else # Opportunities
    send(attachment.class.name.tableize).delete(attachment)
  end
end

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




127
128
129
130
131
132
133
# File 'app/models/entities/contact.rb', line 127

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

#save_with_account_and_permissions(params) ⇒ Object

Backend handler for [Create New Contact] form (see contact/create).




138
139
140
141
142
143
# File 'app/models/entities/contact.rb', line 138

def (params)
  (params)
  result = save
  opportunities << Opportunity.find(params[:opportunity]) unless params[:opportunity].blank?
  result
end

#update_with_account_and_permissions(params) ⇒ Object

Backend handler for [Update Contact] form (see contact/update).




147
148
149
150
151
152
153
# File 'app/models/entities/contact.rb', line 147

def (params)
  (params)
  # Must set access before user_ids, because user_ids= method depends on access value.
  self.access = params[:contact][:access] if params[:contact][:access]
  self.attributes = params[:contact]
  save
end