Class: ContactPoint::AddressBookBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/services/contact_point/address_book_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AddressBookBuilder

Returns a new instance of AddressBookBuilder.



9
10
11
12
13
14
15
16
# File 'app/services/contact_point/address_book_builder.rb', line 9

def initialize(options={})
  @party = options[:party]
  @resource = options[:resource]
  @organization = extract_organization
  @skip_persons = options[:skip_persons]
  @skip_organizations = options[:skip_organizations]
  @formatter = options[:group_results] ? :contact_options_grouped : :contact_options_flat
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter.



3
4
5
# File 'app/services/contact_point/address_book_builder.rb', line 3

def formatter
  @formatter
end

#organizationObject (readonly)

Returns the value of attribute organization.



3
4
5
# File 'app/services/contact_point/address_book_builder.rb', line 3

def organization
  @organization
end

#partyObject (readonly)

Returns the value of attribute party.



3
4
5
# File 'app/services/contact_point/address_book_builder.rb', line 3

def party
  @party
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'app/services/contact_point/address_book_builder.rb', line 3

def resource
  @resource
end

#skip_organizationsObject (readonly)

Returns the value of attribute skip_organizations.



3
4
5
# File 'app/services/contact_point/address_book_builder.rb', line 3

def skip_organizations
  @skip_organizations
end

#skip_personsObject (readonly)

Returns the value of attribute skip_persons.



3
4
5
# File 'app/services/contact_point/address_book_builder.rb', line 3

def skip_persons
  @skip_persons
end

Class Method Details

.options_for_select(options = {}) ⇒ Object



5
6
7
# File 'app/services/contact_point/address_book_builder.rb', line 5

def self.options_for_select(options = {})
  new(options).options_for_select
end

Instance Method Details

#contact_options_flatObject



37
38
39
40
41
42
# File 'app/services/contact_point/address_book_builder.rb', line 37

def contact_options_flat
  results = []
  results += resource_options
  results += organization_options
  results.compact.uniq.sort
end

#contact_options_groupedObject



25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/contact_point/address_book_builder.rb', line 25

def contact_options_grouped
  results = {}
  results[resource.to_s] = resource_options
  if organization
    results[organization.full_name] = organization_options
  elsif party
    # this is to handle contacts not linked to a customer (e.g opportunity participants)
    results[party.full_name] = current_party_options
  end
  results.reject{|k,v| v.blank?}
end

#current_party_optionsObject



60
61
62
63
64
65
66
67
# File 'app/services/contact_point/address_book_builder.rb', line 60

def current_party_options
  return [] unless party
  if (party.try(:is_person?) && !skip_persons) || (party.try(:is_organization?) && !skip_organizations)
    return [[party.name, party.id]]
  else
    return []
  end
end

#options_for_selectObject



18
19
20
21
22
23
# File 'app/services/contact_point/address_book_builder.rb', line 18

def options_for_select
  # Return empty array if neither party nor resource is available
  return [] unless @party || @resource

  send(formatter)
end

#organization_optionsObject



48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/contact_point/address_book_builder.rb', line 48

def organization_options
  return [] unless organization
  @organization_options ||= begin
    opts = []
    opts = [[organization.name, organization.id]] unless skip_organizations
    if organization.respond_to?(:contacts) && !skip_persons
      opts += organization.contacts.active.where.not(full_name: nil).pluck(:full_name, :id)
    end
    opts.compact.uniq.sort_by(&:first)
  end
end

#resource_optionsObject



44
45
46
# File 'app/services/contact_point/address_book_builder.rb', line 44

def resource_options
  @resource_options ||= (resource.try(:participants_options_for_select) || []).compact.uniq.sort
end