Module: Highrise::CustomFields

Included in:
Company, Person
Defined in:
lib/highrise/custom_fields.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/highrise/custom_fields.rb', line 41

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

#convert_method_to_field_label(method) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/highrise/custom_fields.rb', line 32

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

#field(field_label) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/highrise/custom_fields.rb', line 3

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

#new_subject_data(field, value) ⇒ Object



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

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

#set_field_value(field_label, new_value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/highrise/custom_fields.rb', line 15

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



28
29
30
# File 'lib/highrise/custom_fields.rb', line 28

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