Class: Highrise::Person

Inherits:
Subject
  • Object
show all
Includes:
Pagination, Searchable, Taggable
Defined in:
lib/highrise/person.rb

Instance Method Summary collapse

Methods included from Searchable

included

Methods included from Taggable

#tag!, #tags, #untag!

Methods included from Pagination

included

Methods inherited from Subject

#add_note, #add_task, #emails, #notes, #upcoming_tasks

Methods inherited from Base

headers, oauth_token=

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/highrise/person.rb', line 73

def method_missing(method_symbol, *args)
  method_name = method_symbol.to_s

  if method_name[-1,1] == "="
    attribute_name = method_name[0...-1]
    field = convert_method_to_field_label(attribute_name)
    return set_field_value(field.subject_field_label, args[0]) if field

    return super if attributes[attribute_name]

    subject_fields = SubjectField.find(:all)
    unless subject_fields.nil?
      subject_fields.each do |custom_field|
        if transform_subject_field_label(custom_field.label) == attribute_name
          return attributes["subject_datas"] << new_subject_data(custom_field, args[0])
        end
      end
    end
  else
    field = convert_method_to_field_label(method_name)
    return field(field.subject_field_label) if field
  end
  super
end

Instance Method Details

#addressObject


15
16
17
# File 'lib/highrise/person.rb', line 15

def address
  contact_data.addresses.first
end

#companyObject


7
8
9
# File 'lib/highrise/person.rb', line 7

def company
  Company.find(company_id) if company_id
end

#convert_method_to_field_label(method) ⇒ Object


64
65
66
67
68
69
70
71
# File 'lib/highrise/person.rb', line 64

def convert_method_to_field_label method
  custom_fields = attributes["subject_datas"] ||= []
  custom_fields.each do |field|
    method_name_from_field = transform_subject_field_label(field.subject_field_label)
    return field if method_name_from_field == method
  end
  nil
end

#email_addressesObject


23
24
25
# File 'lib/highrise/person.rb', line 23

def email_addresses
  contact_data.email_addresses.collect { |address| address.address } rescue []
end

#field(field_label) ⇒ Object


35
36
37
38
39
40
41
# File 'lib/highrise/person.rb', line 35

def field(field_label)
  custom_fields = attributes["subject_datas"] ||= []
  field = custom_fields.detect do |field|
    field.subject_field_label == field_label
  end
  field ? field.value : nil
end

#labelObject


31
32
33
# File 'lib/highrise/person.rb', line 31

def label
  'Party'
end

#nameObject


11
12
13
# File 'lib/highrise/person.rb', line 11

def name
  "#{first_name rescue ''} #{last_name rescue ''}".strip
end

#new_subject_data(field, value) ⇒ Object


43
44
45
# File 'lib/highrise/person.rb', line 43

def new_subject_data(field, value)
  Highrise::SubjectData.new(:subject_field_id => field.id, :subject_field_label => field.label, :value => value)
end

#phone_numbersObject


27
28
29
# File 'lib/highrise/person.rb', line 27

def phone_numbers
  contact_data.phone_numbers.collect { |phone_number| phone_number.number } rescue []
end

#set_field_value(field_label, new_value) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/highrise/person.rb', line 47

def set_field_value(field_label, new_value)
  custom_fields = attributes["subject_datas"] ||= []
  custom_fields.each do |field|
    return field.value = new_value if field.subject_field_label == field_label
  end

  SubjectField.find(:all).each do |custom_field|
    if custom_field.label == field_label
      return attributes["subject_datas"] << new_subject_data(custom_field, new_value)
    end
  end
end

#transform_subject_field_label(field_label) ⇒ Object


60
61
62
# File 'lib/highrise/person.rb', line 60

def transform_subject_field_label field_label
  field_label.downcase.tr(' ', '_')
end

#web_addressObject


19
20
21
# File 'lib/highrise/person.rb', line 19

def web_address
  contact_data.web_addresses.first
end