Class: ContactPoint::AddressBookBuilder

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

Overview

Service object: address book builder.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AddressBookBuilder

Returns a new instance of AddressBookBuilder.



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

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.



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

def formatter
  @formatter
end

#organizationObject (readonly)

Returns the value of attribute organization.



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

def organization
  @organization
end

#partyObject (readonly)

Returns the value of attribute party.



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

def party
  @party
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#skip_organizationsObject (readonly)

Returns the value of attribute skip_organizations.



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

def skip_organizations
  @skip_organizations
end

#skip_personsObject (readonly)

Returns the value of attribute skip_persons.



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

def skip_persons
  @skip_persons
end

Class Method Details

.options_for_select(options = {}) ⇒ Object



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

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

Instance Method Details

#contact_options_flatObject



38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/contact_point/address_book_builder.rb', line 38

def contact_options_flat
  results = []
  results += resource_options
  results += organization_options
  # Mirror contact_options_grouped: when there's no organization, fall back
  # to the party itself so customer-less contacts (orphan contacts, opportunity
  # participants) still appear instead of an empty list. When an organization
  # exists, the party already comes in via organization.contacts.
  results += current_party_options if organization.nil?
  results.compact.uniq.sort
end

#contact_options_groupedObject



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

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.compact_blank
end

#current_party_optionsObject



65
66
67
68
69
70
71
72
73
# File 'app/services/contact_point/address_book_builder.rb', line 65

def current_party_options
  return [] unless party

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

#options_for_selectObject



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

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

  send(formatter)
end

#organization_optionsObject



54
55
56
57
58
59
60
61
62
63
# File 'app/services/contact_point/address_book_builder.rb', line 54

def organization_options
  return [] unless organization

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

#resource_optionsObject



50
51
52
# File 'app/services/contact_point/address_book_builder.rb', line 50

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