Class: Vcard

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
HasAddress, I18nHelpers, DirectoryLookup
Defined in:
app/models/vcard.rb

Defined Under Namespace

Modules: DirectoryAddress, DirectoryLookup

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DirectoryLookup

#address_matches, #directory_filter, #directory_found?, #directory_lookup, #directory_matches, #family_name_match?, #family_name_matches, #filtered_matches, #great_match?, #great_matches, #locality_matches, #map_for_directory, #normalize, #perfect_match?, #perfect_matches

Methods included from HasAddress

#address_with_autobuild, included

Class Method Details

.by_name_conditions(name) ⇒ Object

Advanced finders



60
61
62
# File 'app/models/vcard.rb', line 60

def self.by_name_conditions(name)
  ['vcards.full_name LIKE :name OR vcards.family_name LIKE :name OR vcards.given_name LIKE :name OR vcards.nickname LIKE :name', {:name => name}]
end

.find_all_by_name(name) ⇒ Object



68
69
70
# File 'app/models/vcard.rb', line 68

def self.find_all_by_name(name)
  self.find(:all, :conditions => self.by_name_conditions(name))
end

.find_by_name(name) ⇒ Object



64
65
66
# File 'app/models/vcard.rb', line 64

def self.find_by_name(name)
  self.find(:first, :conditions => self.by_name_conditions(name))
end

Instance Method Details

#abbreviated_nameObject



53
54
55
56
57
# File 'app/models/vcard.rb', line 53

def abbreviated_name
  return read_attribute(:full_name) if read_attribute(:full_name)

  [given_name.try(:first).try(:upcase), family_name].compact.join(". ")
end

#address_linesObject

Helper methods



73
74
75
76
77
78
# File 'app/models/vcard.rb', line 73

def address_lines
  lines = [extended_address, street_address, post_office_box, "#{postal_code} #{locality}"]

  # Only return non-empty lines
  lines.map {|line| line.strip unless (line.nil? or line.strip.empty?)}.compact
end

#contact_lines(separator = " ") ⇒ Object



87
88
89
90
91
92
# File 'app/models/vcard.rb', line 87

def contact_lines(separator = " ")
  lines = contacts.map{|p| p.to_s unless (p.number.nil? or p.number.strip.empty?)}

  # Only return non-empty lines
  lines.map {|line| line.strip unless (line.nil? or line.strip.empty?)}.compact
end

#full_address_linesObject



80
81
82
83
84
85
# File 'app/models/vcard.rb', line 80

def full_address_lines
  lines = [honorific_prefix, full_name] + address_lines

  # Only return non-empty lines
  lines.map {|line| line.strip unless (line.nil? or line.strip.empty?)}.compact
end

#full_nameObject

Convenience accessors



46
47
48
49
50
51
# File 'app/models/vcard.rb', line 46

def full_name
  result = read_attribute(:full_name)
  result ||= [ family_name, given_name ].compact.join(' ')

  return result
end

#salutationObject

Salutation



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/vcard.rb', line 105

def salutation
  case honorific_prefix
  when 'Herr Dr. med.'
    result = "Sehr geehrter Herr Dr. " + family_name
  when 'Frau Dr. med.'
    result = "Sehr geehrte Frau Dr. " + family_name
  when 'Herr Dr.'
    result = "Sehr geehrter Herr Dr. " + family_name
  when 'Frau Dr.'
    result = "Sehr geehrte Frau Dr. " + family_name
  when 'Herr'
    result = "Sehr geehrter Herr " + family_name
  when 'Frau'
    result = "Sehr geehrte Frau " + family_name
  when 'Br.'
    result = "Sehr geehrter Bruder " + family_name
  when 'Sr.'
    result = "Sehr geehrte Schwester " + family_name
  else
    result = "Sehr geehrte Damen und Herren"
  end
  return result
end

#validate_nameObject



37
38
39
40
41
42
43
# File 'app/models/vcard.rb', line 37

def validate_name
  if full_name.blank?
    errors.add(:full_name, "#{t_attr(:full_name, Vcard)} #{I18n.translate('errors.messages.empty')}")
    errors.add(:family_name, "#{t_attr(:family_name, Vcard)} #{I18n.translate('errors.messages.empty')}")
    errors.add(:given_name, "#{t_attr(:given_name, Vcard)} #{I18n.translate('errors.messages.empty')}")
  end
end