Module: Models::PartyIdentity

Extended by:
ActiveSupport::Concern
Included in:
Party
Defined in:
app/concerns/models/party_identity.rb

Overview

Name fields, display helpers, and type/role predicates for Party STI records.

== person? / organization? / homeowner? predicate resolution

The bare predicates defined here are the Party defaults. Customer and Contact
override them with subtly different semantics; pay attention when calling
them on a base Party versus the subclass:

  • On Customer (Models::CustomerOrganization):
    person? == homeowner?
    organization? == !homeowner?
  • On Contact:
    person? always true
    organization? == !homeowner? (delegated to its customer)
  • Note: is_homeowner? / is_organization? defined on Contact delegate to the
    contact's customer, while bare homeowner? / organization? use the
    Models::PartyIdentity definitions on the contact itself.

See Also:

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Whether this party is currently active.

Returns:

  • (Boolean)


259
260
261
# File 'app/concerns/models/party_identity.rb', line 259

def active?
  !inactive?
end

#admin?Boolean

Whether this party has admin privileges.

Returns:

  • (Boolean)


245
246
247
# File 'app/concerns/models/party_identity.rb', line 245

def admin?
  &.has_role?('admin')
end

#akaString

"Also known as" nickname stored in name2 when the party is not a person.

Returns:

  • (String)


73
74
75
# File 'app/concerns/models/party_identity.rb', line 73

def aka
  name2 || ''
end

#aka=(name) ⇒ String?

Sets the nickname, but only for non-person parties.

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)


81
82
83
# File 'app/concerns/models/party_identity.rb', line 81

def aka=(name)
  self.name2 = name unless person?
end

#all_activitiesActiveRecord::Associations::CollectionProxy<Activity>

Activities linked to this party.

Returns:

  • (ActiveRecord::Associations::CollectionProxy<Activity>)


319
320
321
# File 'app/concerns/models/party_identity.rb', line 319

def all_activities
  linked_activities
end

#blog_admin?Boolean

Whether this party has blog admin privileges.

Returns:

  • (Boolean)


266
267
268
# File 'app/concerns/models/party_identity.rb', line 266

def blog_admin?
  &.has_role?('blog_admin')
end

#broadcast_product_interest_changed(product_line_ids_added: [], product_line_ids_removed: []) ⇒ void

This method returns an undefined value.

Broadcasts a product-interest change for this party.

Parameters:

  • product_line_ids_added (Array<Integer>) (defaults to: [])
  • product_line_ids_removed (Array<Integer>) (defaults to: [])


354
355
356
357
# File 'app/concerns/models/party_identity.rb', line 354

def broadcast_product_interest_changed(product_line_ids_added: [],
                                       product_line_ids_removed: [])
  PartyProductInterestSync.broadcast(self, added: product_line_ids_added, removed: product_line_ids_removed)
end

#can_be_certified?Boolean

Whether this party type supports certification.

Returns:

  • (Boolean)


193
194
195
# File 'app/concerns/models/party_identity.rb', line 193

def can_be_certified?
  is_a?(Customer) || is_a?(Contact) || is_a?(Employee)
end

#can_list_all_contact_resources?Boolean

Whether this party can list all contact resources.

Returns:

  • (Boolean)


273
274
275
276
277
278
279
280
281
282
283
# File 'app/concerns/models/party_identity.rb', line 273

def can_list_all_contact_resources?
  if instance_of?(::Customer) ||
     (instance_of?(::Contact) &&  &&
      (.is_online_customer_administrator? ||
       .is_online_customer_sales_supervisor? ||
       .is_online_customer_accounting_supervisor?))
    true
  else
    instance_of?(::Employee)
  end
end

#clear_individual_namesnil

Clears the individual first/middle/last name fields.

Returns:

  • (nil)


121
122
123
124
125
# File 'app/concerns/models/party_identity.rb', line 121

def clear_individual_names
  self.name1 = nil
  self.name2 = nil
  self.name3 = nil
end

#clear_namesnil

Clears all name fields and the full name.

Returns:

  • (nil)


111
112
113
114
115
116
# File 'app/concerns/models/party_identity.rb', line 111

def clear_names
  self.name1 = nil
  self.name2 = nil
  self.name3 = nil
  self.full_name = nil
end

#contact?Boolean Also known as: is_contact?

Whether this record is a Contact.

Returns:

  • (Boolean)


209
210
211
# File 'app/concerns/models/party_identity.rb', line 209

def contact?
  instance_of? Contact
end

#customer?Boolean Also known as: is_customer?

Whether this record is a Customer.

Returns:

  • (Boolean)


200
201
202
# File 'app/concerns/models/party_identity.rb', line 200

def customer?
  instance_of? Customer
end

#determine_company_idInteger

Resolves the company ID for this party.

Returns:

  • (Integer)


295
296
297
298
299
300
# File 'app/concerns/models/party_identity.rb', line 295

def determine_company_id
  resolved = customer&.determine_company_id if respond_to?(:customer)
  resolved ||= self[:company_id]
  resolved ||= Company::USA
  resolved
end

#direct_commercial?Boolean Also known as: is_direct_commercial?

Whether this party is a direct commercial customer.

Returns:

  • (Boolean)


184
185
186
# File 'app/concerns/models/party_identity.rb', line 184

def direct_commercial?
  customer&.profile&.direct_commercial?
end

#direct_pro?Boolean Also known as: is_direct_pro?

Whether this party is a direct professional customer.

Returns:

  • (Boolean)


175
176
177
# File 'app/concerns/models/party_identity.rb', line 175

def direct_pro?
  customer&.profile&.direct_pro?
end

#employee?Boolean Also known as: is_employee?

Whether this record is an Employee.

Returns:

  • (Boolean)


218
219
220
# File 'app/concerns/models/party_identity.rb', line 218

def employee?
  instance_of? Employee
end

#first_nameString

First/given name stored in name1.

Returns:

  • (String)


28
29
30
# File 'app/concerns/models/party_identity.rb', line 28

def first_name
  name1 || ''
end

#first_name=(name) ⇒ String?

Sets the first name, stripping whitespace and storing nil for blank values.

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)


36
37
38
# File 'app/concerns/models/party_identity.rb', line 36

def first_name=(name)
  self.name1 = name.presence&.strip
end

#guest_individual_name?Boolean Also known as: has_guest_individual_name?

Whether the first name is a guest placeholder.

Returns:

  • (Boolean)


335
336
337
# File 'app/concerns/models/party_identity.rb', line 335

def guest_individual_name?
  name1 =~ /^Guest / || name1 == 'Guest'
end

#guest_name?Boolean Also known as: has_guest_name?

Whether the full name is a guest placeholder.

Returns:

  • (Boolean)


326
327
328
# File 'app/concerns/models/party_identity.rb', line 326

def guest_name?
  full_name =~ /^Guest / || full_name == 'Guest'
end

#homeowner?Boolean Also known as: is_homeowner?

Whether this party is a homeowner.

Returns:

  • (Boolean)


166
167
168
# File 'app/concerns/models/party_identity.rb', line 166

def homeowner?
  customer&.profile_id == ProfileConstants::HOMEOWNER
end

#id_and_nameString

Display string combining ID and full name.

Returns:

  • (String)


305
306
307
# File 'app/concerns/models/party_identity.rb', line 305

def id_and_name
  "#{id} #{full_name}"
end

#last_nameString

Last/family name stored in name3.

Returns:

  • (String)


58
59
60
# File 'app/concerns/models/party_identity.rb', line 58

def last_name
  name3 || ''
end

#last_name=(name) ⇒ String?

Sets the last name, stripping whitespace and storing nil for blank values.

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)


66
67
68
# File 'app/concerns/models/party_identity.rb', line 66

def last_name=(name)
  self.name3 = name.presence&.strip
end

#manager?Boolean

Whether this party has manager privileges.

Returns:

  • (Boolean)


252
253
254
# File 'app/concerns/models/party_identity.rb', line 252

def manager?
  &.is_manager?
end

#middle_nameString

Middle name stored in name2.

Returns:

  • (String)


43
44
45
# File 'app/concerns/models/party_identity.rb', line 43

def middle_name
  name2 || ''
end

#middle_name=(name) ⇒ String?

Sets the middle name, stripping whitespace and storing nil for blank values.

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)


51
52
53
# File 'app/concerns/models/party_identity.rb', line 51

def middle_name=(name)
  self.name2 = name.presence&.strip
end

#nameString

Full display name, falling back to an empty string.

Returns:

  • (String)


88
89
90
# File 'app/concerns/models/party_identity.rb', line 88

def name
  full_name || ''
end

#name=(name_to_use) ⇒ String?

Parses and assigns a full name, splitting person names into components.

Parameters:

  • name_to_use (String, nil)

Returns:

  • (String, nil)


96
97
98
99
100
101
102
103
104
105
106
# File 'app/concerns/models/party_identity.rb', line 96

def name=(name_to_use)
  logger.debug "Party#name called with #{name_to_use}"
  return if name_to_use.nil? || name_to_use.strip.empty?

  name_to_use&.strip!
  self.full_name = name_to_use
  return unless person?

  pnp = PersonNameParser.new name_to_use
  pnp.to_party(self)
end

#organization?Boolean Also known as: is_organization?

Whether this party represents an organization.

Returns:

  • (Boolean)


236
237
238
# File 'app/concerns/models/party_identity.rb', line 236

def organization?
  !(homeowner? || employee?)
end

#person?Boolean Also known as: is_person?

Whether this party represents an individual person.

Returns:

  • (Boolean)


227
228
229
# File 'app/concerns/models/party_identity.rb', line 227

def person?
  contact? || employee? # not all parties have homeowner? defined
end

#primary_partyParty

Returns this party as the primary party.

Returns:



288
289
290
# File 'app/concerns/models/party_identity.rb', line 288

def primary_party
  self
end

#public_facing_full_nameString?

Full name as it should appear in public-facing contexts.

Returns:

  • (String, nil)


145
146
147
148
149
# File 'app/concerns/models/party_identity.rb', line 145

def public_facing_full_name
  return nil if guest_individual_name?

  full_name
end

#public_facing_full_name=(name) ⇒ String?

Assigns or clears the public-facing full name.

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)


155
156
157
158
159
160
161
# File 'app/concerns/models/party_identity.rb', line 155

def public_facing_full_name=(name)
  if name.present?
    self.name = name
  else
    clear_individual_names
  end
end

#show_name_fourString Also known as: show_name_4

Compact four-character identifier used for some displays.

Returns:

  • (String)


130
131
132
133
134
# File 'app/concerns/models/party_identity.rb', line 130

def show_name_four
  "#{first_name&.[](0)}#{last_name&.first(3)}".upcase
rescue StandardError
  ''
end

#to_sString?

String representation of the party.

Returns:

  • (String, nil)


312
313
314
# File 'app/concerns/models/party_identity.rb', line 312

def to_s
  full_name
end

#update_product_interests(product_line_ids) ⇒ Array<Integer>

Replaces this party's product-line interests and broadcasts the change.

Parameters:

  • product_line_ids (Array<Integer>)

Returns:

  • (Array<Integer>)

    the cleaned, sorted product line IDs



345
346
347
# File 'app/concerns/models/party_identity.rb', line 345

def update_product_interests(product_line_ids)
  PartyProductInterestSync.call(self, product_line_ids)
end