Class: AlchemyCrm::Contact

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/alchemy_crm/contact.rb

Constant Summary collapse

ACCESSIBLE_ATTRIBUTES =
[
  :salutation,
  :title,
  :firstname,
  :lastname,
  :email,
  :phone,
  :mobile,
  :address,
  :zip,
  :city,
  :company,
  :country,
  :subscriptions_attributes,
  :verified,
  :disabled,
  :tag_list
]
SEARCHABLE_ATTRIBUTES =
[
  "salutation",
  "title",
  "firstname",
  "lastname",
  "company",
  "address",
  "zip",
  "city",
  "country",
  "email",
  "phone",
  "mobile",
  "cached_tag_list"
]
EXPORTABLE_COLUMNS =
SEARCHABLE_ATTRIBUTES + [
  "verified",
  "disabled"
]
COLUMN_NAMES =
[
  [::I18n.t(:salutation, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Salutation'), "salutation"],
  [::I18n.t(:title, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Title'), "title"],
  [::I18n.t(:firstname, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Firstname'), "firstname"],
  [::I18n.t(:lastname, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Lastname'), "lastname"],
  [::I18n.t(:company, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Company'), "company"],
  [::I18n.t(:address, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Address'), "address"],
  [::I18n.t(:zip, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Zipcode'), "zip"],
  [::I18n.t(:city, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'City'), "city"],
  [::I18n.t(:country, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Country'), "country"],
  [::I18n.t(:email, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Email'), "email"],
  [::I18n.t(:phone, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Phone'), "phone"],
  [::I18n.t(:mobile, :scope => 'activerecord.attributes.alchemy_crm/contact', :default => 'Mobile'), "mobile"]
]
INTERPOLATION_NAME_METHODS =
%w(fullname name_with_title firstname lastname name email)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_human_attribute_name(attrb) ⇒ Object



207
208
209
# File 'app/models/alchemy_crm/contact.rb', line 207

def self.clean_human_attribute_name(attrb)
  human_attribute_name(attrb).gsub(/\*$/, '')
end

.fakeObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/alchemy_crm/contact.rb', line 145

def self.fake
  fake = new(
    :salutation => nil,
    :title => ::I18n.t(:title, :scope => 'alchemy_crm.fake_contact_attributes', :default => 'Dr.'),
    :firstname => ::I18n.t(:firstname, :scope => 'alchemy_crm.fake_contact_attributes', :default => 'Jon'),
    :lastname => ::I18n.t(:lastname, :scope => 'alchemy_crm.fake_contact_attributes', :default => 'Doe'),
    :email => ::I18n.t(:email, :scope => 'alchemy_crm.fake_contact_attributes', :default => '[email protected]'),
    :phone => ::I18n.t(:phone, :scope => 'alchemy_crm.fake_contact_attributes', :default => '1-1234567'),
    :mobile => ::I18n.t(:mobile, :scope => 'alchemy_crm.fake_contact_attributes', :default => '123-456789'),
    :address => ::I18n.t(:address, :scope => 'alchemy_crm.fake_contact_attributes', :default => 'Street 1'),
    :zip => ::I18n.t(:zip, :scope => 'alchemy_crm.fake_contact_attributes', :default => '10000'),
    :city => ::I18n.t(:city, :scope => 'alchemy_crm.fake_contact_attributes', :default => 'City'),
    :company => ::I18n.t(:company, :scope => 'alchemy_crm.fake_contact_attributes', :default => 'Company inc.'),
    :country => ::I18n.t(:country, :scope => 'alchemy_crm.fake_contact_attributes', :default => 'US')
  )
  fake.readonly!
  fake
end

.filterable_column_namesObject



192
193
194
# File 'app/models/alchemy_crm/contact.rb', line 192

def self.filterable_column_names
  COLUMN_NAMES
end

.find_by_query(query, options, paginate) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'app/models/alchemy_crm/contact.rb', line 172

def self.find_by_query(query, options, paginate)
  column_names = Contact.public_column_names
  search_string = (column_names.join(" LIKE '%#{query}%' OR ") + " LIKE '%#{query}%'")
  contacts = where(search_string)
  if paginate
    contacts.paginate(options)
  else
    contacts
  end
end

.find_or_create(data) ⇒ Object



196
197
198
199
200
201
# File 'app/models/alchemy_crm/contact.rb', line 196

def self.find_or_create(data)
  if (contact = Contact.find_by_email(data[:email])).nil?
    contact = Contact.create(data)
  end
  contact
end

.new_from_recipient(recipient) ⇒ Object



211
212
213
214
215
216
# File 'app/models/alchemy_crm/contact.rb', line 211

def self.new_from_recipient(recipient)
  raise "No recipient found!" if recipient.nil?
  contact = new(:email => recipient.email, :lastname => recipient.email)
  contact.readonly!
  contact
end

.new_from_vcard(vcard, verified) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/models/alchemy_crm/contact.rb', line 218

def self.new_from_vcard(vcard, verified)
  raise "No vcard found!" if vcard.nil?
  raise ::I18n.t(:imported_contacts_not_verified, :scope => :alchemy_crm) if !verified
  contacts = []
  ::Vpim::Vcard.decode(vcard).each do |card|
    remapped_attributes = {
      :company => card.org.blank? ? nil : card.org.first,
      :lastname => card.name.blank? ? nil : card.name.family,
      :firstname => card.name.blank? ? nil : card.name.given,
      :title => card.name.blank? ? nil : card.name.prefix,
      :address => card.address.blank? ? nil : card.address.street,
      :city => card.address.blank? ? nil : card.address.locality,
      :country => card.address.blank? ? nil : card.address.country,
      :zip => card.address.blank? ? nil : card.address.postalcode,
      :email => card.email.to_s,
      :phone => card.telephone ? card.telephone.location.include?("cell") ? nil : card.telephone.to_s : nil,
      :mobile => card.telephones.detect { |t| t.location.include?("cell") }.to_s,
      :verified => true
    }
    contacts << Contact.create(remapped_attributes)
  end
  contacts
end

.public_column_namesObject



188
189
190
# File 'app/models/alchemy_crm/contact.rb', line 188

def self.public_column_names
  Contact.column_names - ["created_at", "updated_at", "email_salt", "verified", "email_sha1", "id"]
end

.replace_tag(old_tag, new_tag) ⇒ Object



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

def self.replace_tag(old_tag, new_tag)
  self.tagged_with(old_tag).each do |contact|
    contact.tags.delete(old_tag)
    contact.tags << new_tag
    contact.save
  end
end

Instance Method Details

#all_newslettersObject

TODO: Zu den selbstgewählten Mailingformaten müssten noch alle durch den Backend User gewählten Formate mit angezeigt werden, oder?



184
185
186
# File 'app/models/alchemy_crm/contact.rb', line 184

def all_newsletters
  self.newsletters
end

#all_subscriptions_verified?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'app/models/alchemy_crm/contact.rb', line 203

def all_subscriptions_verified?
  self.subscriptions.inject(true){|acc, s| acc = s.verified? && acc; acc}
end

#disable!Object



88
89
90
91
92
93
94
# File 'app/models/alchemy_crm/contact.rb', line 88

def disable!
  update_attributes({
    :verified => false,
    :disabled => true
  })
  subscriptions.destroy_all
end

#fullnameObject

Returns a full name Salutation + Title + Firstname + Lastname



98
99
100
101
102
103
104
# File 'app/models/alchemy_crm/contact.rb', line 98

def fullname
  if lastname.present? || firstname.present?
    "#{translated_salutation} #{name_with_title}".squeeze(" ")
  else
    name
  end
end

#interpolation_name_valueObject

Uses the config to return the value for emails and forms %#name interpolations.



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

def interpolation_name_value
  name_interpolation_method = Config.get(:name_interpolation_method)
  if name_interpolation_method.present? && INTERPOLATION_NAME_METHODS.include?(name_interpolation_method.to_s) && self.respond_to?(name_interpolation_method.to_sym)
    self.send(name_interpolation_method.to_sym)
  else
    fullname
  end
end

#nameObject

Returns the name ot email, if no name is present Firstname + Lastname, or email



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

def name
  if lastname.present? || firstname.present?
    "#{firstname} #{lastname}".squeeze(" ")
  else
    email
  end
end

#name_with_titleObject

Returns the name and title Title + Firstname + Lastname



121
122
123
# File 'app/models/alchemy_crm/contact.rb', line 121

def name_with_title
  "#{title} #{name}".squeeze(" ")
end

#to_vcardObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/models/alchemy_crm/contact.rb', line 242

def to_vcard
  card = Vpim::Vcard::Maker.make2 do |maker|
    maker.add_name do |name|
      name.prefix = title unless title.blank?
      name.given = firstname unless firstname.blank?
      name.family = lastname unless lastname.blank?
    end
    maker.add_tel(phone) { |t| t.location = 'work' } unless phone.blank?
    maker.add_tel(mobile) { |t| t.location = 'cell' } unless mobile.blank?
    maker.add_email(email) { |e| e.location = 'work' } unless email.blank?
    maker.add_addr do |addr|
      addr.street = address
      addr.postalcode = zip
      addr.locality = city
      addr.country = country
    end
    maker.org = company
  end
  vcf = File.new(Rails.root.to_s + "/tmp/#{fullname}.vcf", "w")
  vcf.write(card.encode)
  vcf.close
end

#translated_salutationObject

Translated salutation

Translate the saluations in your config/LOCALE.yml

alchemy_crm:
  salutations:
    mr:
    ms:


115
116
117
# File 'app/models/alchemy_crm/contact.rb', line 115

def translated_salutation
  ::I18n.t(salutation, :scope => [:alchemy_crm, :salutations], :default => salutation.to_s.capitalize)
end