Class: Party

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/party.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



301
302
303
304
305
306
307
308
# File 'app/models/party.rb', line 301

def method_missing(m, *args, &block)
  if self.respond_to?(m)
    value = get_contact_by_method(m.to_s)
    (value.nil?) ? super : (return value)
  else
    super
  end
end

Instance Attribute Details

#create_relationship(description, to_party_id, reln_type) ⇒ Object

Creates a new PartyRelationship for this particular party instance.



43
44
45
46
47
48
49
50
# File 'app/models/party.rb', line 43

def create_relationship(description, to_party_id, reln_type)
  PartyRelationship.create(:description => description,
                           :relationship_type => reln_type,
                           :party_id_from => id,
                           :from_role => reln_type.valid_from_role,
                           :party_id_to => to_party_id,
                           :to_role => reln_type.valid_to_role)
end

#relationshipsObject (readonly)

Gathers all party relationships that contain this particular party id in either the from or to side of the relationship.



23
24
25
# File 'app/models/party.rb', line 23

def relationships
  @relationships
end

Instance Method Details

#add_contact(contact_mechanism_class, contact_mechanism_args = {}, contact_purposes = []) ⇒ Object

looks for contacts matching on value and purpose if a contact exists, it updates, if not, it adds it



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/models/party.rb', line 226

def add_contact(contact_mechanism_class, contact_mechanism_args={}, contact_purposes=[])
  is_primary = contact_mechanism_args['is_primary']
  contact_purposes = [contact_purposes] if !contact_purposes.kind_of?(Array) # gracefully handle a single purpose not in an array

  contact_mechanism_args.delete_if { |k, v| ['created_at', 'updated_at', 'is_primary'].include? k.to_s }
  contact_mechanism = contact_mechanism_class.new(contact_mechanism_args)
  contact_mechanism.contact.party = self
  contact_mechanism.contact.contact_purposes = contact_purposes
  contact_mechanism.contact.save
  contact_mechanism.save

  set_primary_contact(contact_mechanism_class, contact_mechanism) if is_primary

  contact_mechanism
end

#destroy_business_partyObject

Callbacks



53
54
55
56
57
# File 'app/models/party.rb', line 53

def destroy_business_party
  if self.business_party
    self.business_party.destroy
  end
end

#destroy_party_relationshipsObject



59
60
61
# File 'app/models/party.rb', line 59

def destroy_party_relationships
  PartyRelationship.destroy_all("party_id_from = #{id} or party_id_to = #{id}")
end

#find_all_contacts_by_contact_mechanism(contact_mechanism_class) ⇒ Object



201
202
203
204
205
206
207
# File 'app/models/party.rb', line 201

def find_all_contacts_by_contact_mechanism(contact_mechanism_class)
  table_name = contact_mechanism_class.name.tableize

  contacts = self.contacts.joins("inner join #{table_name} on #{table_name}.id = contact_mechanism_id and contact_mechanism_type = '#{contact_mechanism_class.name}'")

  contacts.collect(&:contact_mechanism)
end

#find_contact(contact_mechanism_class, contact_mechanism_args = {}, contact_purposes = []) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/party.rb', line 209

def find_contact(contact_mechanism_class, contact_mechanism_args={}, contact_purposes=[])
  table_name = contact_mechanism_class.name.tableize

  query = self.contacts.joins("inner join #{table_name} on #{table_name}.id = contact_mechanism_id and contact_mechanism_type = '#{contact_mechanism_class.name}'
                                 inner join contact_purposes_contacts on contact_purposes_contacts.contact_id = contacts.id
                                 and contact_purposes_contacts.contact_purpose_id in (#{contact_purposes.collect { |item| item.attributes["id"] }.join(',')})")

  contact_mechanism_args.each do |key, value|
    next if key == 'updated_at' or key == 'created_at' or key == 'id' or key == 'is_primary'
    query = query.where("#{table_name}.#{key} = ?", value) unless value.nil?
  end unless contact_mechanism_args.nil?

  query.first
end

#find_contact_mechanism_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object



186
187
188
189
190
# File 'app/models/party.rb', line 186

def find_contact_mechanism_with_purpose(contact_mechanism_class, contact_purpose)
  contact = self.find_contact_with_purpose(contact_mechanism_class, contact_purpose)

  contact.contact_mechanism unless contact.nil?
end

#find_contact_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object



192
193
194
195
196
197
198
199
# File 'app/models/party.rb', line 192

def find_contact_with_purpose(contact_mechanism_class, contact_purpose)
  #if a symbol or string was passed get the model
  unless contact_purpose.is_a? ContactPurpose
    contact_purpose = ContactPurpose.find_by_internal_identifier(contact_purpose.to_s)
  end

  self.find_contact(contact_mechanism_class, nil, [contact_purpose])
end

#find_relationships_by_type(relationship_type_iid) ⇒ Object



35
36
37
38
39
# File 'app/models/party.rb', line 35

def find_relationships_by_type(relationship_type_iid)
  PartyRelationship.includes(:relationship_type).
      where('party_id_from = ? or party_id_to = ?', id, id).
      where('relationship_types.internal_identifier' => relationship_type_iid.to_s)
end

#from_relationshipsObject



31
32
33
# File 'app/models/party.rb', line 31

def from_relationships
  @relationships ||= PartyRelationship.where('party_id_from = ?', id)
end

#get_contact_by_method(m) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'app/models/party.rb', line 269

def get_contact_by_method(m)
  method_name = m.split('_')
  return nil if method_name.size < 3 or method_name.size > 4
  # handles 1 or 2 segment contact purposes (i.e. home or employment_offer)
  # contact mechanism must be 2 segments, (i.e. email_address, postal_address, phone_number)
  if method_name.size == 4
    purpose = method_name[0] + '_' + method_name[1]
    klass = method_name[2] + '_' + method_name[3]
  else
    purpose = method_name[0]
    klass = method_name[1] + '_' + method_name[2]
  end

  #constantize klass to make sure it exists and is loaded
  begin
    klass_const = klass.camelize.constantize
    contact_purpose = ContactPurpose.find_by_internal_identifier(purpose)
    if contact_purpose.nil?
      return nil
    else
      find_contact_mechanism_with_purpose(klass_const, contact_purpose)
    end
  rescue NameError
    return nil
  end

end

#get_primary_contact(contact_mechanism_class) ⇒ Object



179
180
181
182
183
184
# File 'app/models/party.rb', line 179

def get_primary_contact(contact_mechanism_class)
  table_name = contact_mechanism_class.name.tableize

  self.contacts.joins("inner join #{table_name} on #{table_name}.id = contact_mechanism_id and contact_mechanism_type = '#{contact_mechanism_class.name}'")
  .where('contacts.is_primary = ?', true).readonly(false).first
end

#has_phone_number?(phone_number) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/party.rb', line 77

def has_phone_number?(phone_number)
  result = nil
  self.contacts.each do |c|
    if c.contact_mechanism_type == 'PhoneNumber'
      if c.contact_mechanism.phone_number == phone_number
        result = true
      end
    end
  end
  result
end

#has_role_type?(*passed_roles) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/party.rb', line 63

def has_role_type?(*passed_roles)
  result = false
  passed_roles.flatten!
  passed_roles.each do |role|
    role_iid = role.is_a?(RoleType) ? role.internal_identifier : role.to_s
    self.role_types.each do |this_role|
      result = true if (this_role.internal_identifier == role_iid)
      break if result
    end
    break if result
  end
  result
end

#has_zip_code?(zip) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/party.rb', line 89

def has_zip_code?(zip)
  result = nil
  self.contacts.each do |c|
    if c.contact_mechanism_type == 'PostalAddress'
      if c.contact_mechanism.zip == zip
        result = true
      end
    end
  end
  result
end

#primary_email_addressObject Also known as: primary_email



131
132
133
134
135
136
137
138
# File 'app/models/party.rb', line 131

def primary_email_address
  contact_mechanism = nil

  contact = self.get_primary_contact(EmailAddress)
  contact_mechanism = contact.contact_mechanism unless contact.nil?

  contact_mechanism
end

#primary_email_address=(email_address) ⇒ Object Also known as: primary_email=



142
143
144
# File 'app/models/party.rb', line 142

def primary_email_address=(email_address)
  self.set_primary_contact(EmailAddress, email_address)
end

#primary_phone_numberObject Also known as: primary_phone

************************************************************************************************ ** Contact Methods ************************************************************************************************



114
115
116
117
118
119
120
121
# File 'app/models/party.rb', line 114

def primary_phone_number
  contact_mechanism = nil

  contact = self.get_primary_contact(PhoneNumber)
  contact_mechanism = contact.contact_mechanism unless contact.nil?

  contact_mechanism
end

#primary_phone_number=(phone_number) ⇒ Object Also known as: primary_phone=



125
126
127
# File 'app/models/party.rb', line 125

def primary_phone_number=(phone_number)
  self.set_primary_contact(PhoneNumber, phone_number)
end

#primary_postal_addressObject Also known as: primary_address



148
149
150
151
152
153
154
155
# File 'app/models/party.rb', line 148

def primary_postal_address
  contact_mechanism = nil

  contact = self.get_primary_contact(PostalAddress)
  contact_mechanism = contact.contact_mechanism unless contact.nil?

  contact_mechanism
end

#primary_postal_address=(postal_address) ⇒ Object Also known as: primary_address=



159
160
161
# File 'app/models/party.rb', line 159

def primary_postal_address=(postal_address)
  self.set_primary_contact(PostalAddress, postal_address)
end

#respond_to?(m, include_private_methods = false) ⇒ Boolean

Returns:

  • (Boolean)


297
298
299
# File 'app/models/party.rb', line 297

def respond_to?(m, include_private_methods = false)
  (super ? true : get_contact_by_method(m.to_s)) rescue super
end

#set_primary_contact(contact_mechanism_class, contact_mechanism_instance) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/party.rb', line 165

def set_primary_contact(contact_mechanism_class, contact_mechanism_instance)
  # set is_primary to false for any current primary contacts of this type
  primary_contact_mechanism = get_primary_contact(contact_mechanism_class)
  if primary_contact_mechanism
    primary_contact_mechanism.is_primary = false
    primary_contact_mechanism.save
  end

  contact_mechanism_instance.is_primary = true
  contact_mechanism_instance.save

  contact_mechanism_instance
end

#to_labelObject

Alias for to_s



102
103
104
# File 'app/models/party.rb', line 102

def to_label
  to_s
end

#to_relationshipsObject



27
28
29
# File 'app/models/party.rb', line 27

def to_relationships
  @relationships ||= PartyRelationship.where('party_id_to = ?', id)
end

#to_sObject



106
107
108
# File 'app/models/party.rb', line 106

def to_s
  "#{description}"
end

#update_contact(contact_mechanism_class, contact, contact_mechanism_args) ⇒ Object



261
262
263
264
265
266
267
# File 'app/models/party.rb', line 261

def update_contact(contact_mechanism_class, contact, contact_mechanism_args)
  set_primary_contact(contact_mechanism_class, contact.contact_mechanism) if contact_mechanism_args[:is_primary] == true

  contact_mechanism_class.update(contact.contact_mechanism, contact_mechanism_args)

  contact.contact_mechanism
end

#update_contact_with_purpose(contact_mechanism_class, contact_purpose, contact_mechanism_args) ⇒ Object

looks for a contact matching on purpose if it exists, it updates it, if not returns false



256
257
258
259
# File 'app/models/party.rb', line 256

def update_contact_with_purpose(contact_mechanism_class, contact_purpose, contact_mechanism_args)
  contact = find_contact_with_purpose(contact_mechanism_class, contact_purpose)
  contact.nil? ? false : update_contact(contact_mechanism_class, contact, contact_mechanism_args)
end

#update_or_add_contact_with_purpose(contact_mechanism_class, contact_purpose, contact_mechanism_args) ⇒ Object

tries to update contact by purpose if contact doesn’t exist, it adds it



244
245
246
247
248
249
250
251
252
# File 'app/models/party.rb', line 244

def update_or_add_contact_with_purpose(contact_mechanism_class, contact_purpose, contact_mechanism_args)
  contact_mechanism = update_contact_with_purpose(contact_mechanism_class, contact_purpose, contact_mechanism_args)

  unless contact_mechanism
    contact_mechanism = add_contact(contact_mechanism_class, contact_mechanism_args, [contact_purpose])
  end

  contact_mechanism
end