Module: ErpBaseErpSvcs::Extensions::ActiveRecord::HasContacts::InstanceMethods
- Defined in:
- lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb
Instance Method Summary collapse
-
#add_contact(contact_mechanism_class, contact_mechanism_args = {}, contact_purposes = []) ⇒ Object
Adds contact.
-
#build_contact_methods ⇒ Object
Builds methods based on contacts and contact purposes associated to this record For example if there is a PhoneNumber with a contact purpose of Home associated it would create a method like home_phone_number.
-
#contact_mechanisms_to_hash(contact_mechanism_klass, contact_purposes = nil) ⇒ Object
Converts contact mechanisms related to this record to an array of hashes containing the contact records data.
-
#email_addresses_to_hash(contact_purposes = nil) ⇒ Object
Converts EmailAddress contact mechanisms related to this record to an array of hashes containing the contact records data.
- #emails ⇒ Object
-
#find_all_contacts_by_contact_mechanism(contact_mechanism_class) ⇒ Object
find all contacts by contact mechanism.
-
#find_contact(contact_mechanism_class, contact_mechanism_args = {}, contact_purposes = []) ⇒ Object
find first contact.
-
#find_contact_mechanism_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find first contact mechanism with purpose.
-
#find_contact_mechanisms_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find all contact mechanisms with purpose.
-
#find_contact_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find first contact with purpose.
-
#find_contacts(contact_mechanism_class, contact_mechanism_args = {}, contact_purposes = []) ⇒ Object
find all contacts.
-
#find_contacts_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find all contacts with purpose.
- #get_primary_contact(contact_mechanism_class) ⇒ Object
-
#has_contact?(contact_mechanism_klass, contact_purpose) ⇒ Boolean
Check if record has contact with purpose.
- #has_contact_with_purpose?(contact_mechanism_class, contact_purpose) ⇒ Boolean
- #has_phone_number?(phone_number) ⇒ Boolean
- #has_zip_code?(zip) ⇒ Boolean
- #phone_numbers ⇒ Object
-
#phone_numbers_to_hash(contact_purposes = nil) ⇒ Object
Converts PhoneNumber contact mechanisms related to this record to an array of hashes containing the contact records data.
- #postal_addresses ⇒ Object
-
#postal_addresses_to_hash(contact_purposes = nil) ⇒ Object
Converts PostalAddress contact mechanisms related to this record to an array of hashes containing the contact records data.
- #primary_email_address ⇒ Object (also: #primary_email)
- #primary_email_address=(email_address) ⇒ Object (also: #primary_email=)
- #primary_phone_number ⇒ Object (also: #primary_phone)
- #primary_phone_number=(phone_number) ⇒ Object (also: #primary_phone=)
- #primary_postal_address ⇒ Object (also: #primary_address)
- #primary_postal_address=(postal_address) ⇒ Object (also: #primary_address=)
- #set_primary_contact(contact_mechanism_class, contact_mechanism_instance) ⇒ Object
- #update_contact(contact_mechanism_class, contact, contact_mechanism_args) ⇒ Object
-
#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.
-
#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.
Instance Method Details
#add_contact(contact_mechanism_class, contact_mechanism_args = {}, contact_purposes = []) ⇒ Object
Adds contact
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 307 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.contact_record = self contact_purposes.each do |contact_purpose| if contact_purpose.is_a?(String) contact_mechanism.contact.contact_purposes << ContactPurpose.iid(contact_purpose) else contact_mechanism.contact.contact_purposes << contact_purpose end end contact_mechanism.contact.save! contact_mechanism.save! set_primary_contact(contact_mechanism_class, contact_mechanism) if is_primary contact_mechanism end |
#build_contact_methods ⇒ Object
Builds methods based on contacts and contact purposes associated to this record For example if there is a PhoneNumber with a contact purpose of Home associated it would create a method like home_phone_number
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 362 def build_contact_methods self.contacts.each do |contact| contact.contact_purposes.each do |contact_purpose| klass = contact.contact_mechanism.class.name self.class.send 'define_method', "#{contact_purpose.internal_identifier}_#{contact.contact_mechanism.class.name.underscore}" do _klass_const = klass.camelize.constantize _contact_purpose = contact_purpose find_contact_mechanism_with_purpose(_klass_const, _contact_purpose) end end end end |
#contact_mechanisms_to_hash(contact_mechanism_klass, contact_purposes = nil) ⇒ Object
Converts contact mechanisms related to this record to an array of hashes containing the contact records data
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 132 def contact_mechanisms_to_hash(contact_mechanism_klass, contact_purposes=nil) contact_mechanisms_data = [] # if the passed contact purpose is a string convert to an Array if contact_purposes && contact_purposes.is_a?(String) contact_purposes = [contact_purposes] end if contact_purposes contact_purposes.each do |contact_purpose| contact_mechanisms = find_contact_mechanisms_with_purpose(contact_mechanism_klass, contact_purpose) unless contact_mechanisms.empty? contact_mechanisms.collect do |item| data = item.to_data_hash data[:contact_purpose] = contact_purpose contact_mechanisms_data.push(data) end end end else contact_mechanisms = find_all_contacts_by_contact_mechanism(contact_mechanism_klass) contact_mechanisms.each do |contact_mechanism| data = contact_mechanism.to_data_hash data[:contact_purpose] = contact_mechanism.contact.contact_purpose.first.internal_identifier contact_mechanisms_data.push(data) end end contact_mechanisms_data end |
#email_addresses_to_hash(contact_purposes = nil) ⇒ Object
Converts EmailAddress contact mechanisms related to this record to an array of hashes containing the contact records data
109 110 111 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 109 def email_addresses_to_hash(contact_purposes=nil) contact_mechanisms_to_hash(EmailAddress, contact_purposes) end |
#emails ⇒ Object
49 50 51 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 49 def emails find_all_contacts_by_contact_mechanism(EmailAddress) end |
#find_all_contacts_by_contact_mechanism(contact_mechanism_class) ⇒ Object
find all contacts by contact mechanism
277 278 279 280 281 282 283 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 277 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
find first contact
286 287 288 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 286 def find_contact(contact_mechanism_class, contact_mechanism_args={}, contact_purposes=[]) find_contacts(contact_mechanism_class, contact_mechanism_args, contact_purposes).first end |
#find_contact_mechanism_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find first contact mechanism with purpose
239 240 241 242 243 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 239 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_mechanisms_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find all contact mechanisms with purpose
246 247 248 249 250 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 246 def find_contact_mechanisms_with_purpose(contact_mechanism_class, contact_purpose) contacts = self.find_contacts_with_purpose(contact_mechanism_class, contact_purpose) contacts.empty? ? [] : contacts.collect(&:contact_mechanism) end |
#find_contact_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find first contact with purpose
253 254 255 256 257 258 259 260 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 253 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_contacts(contact_mechanism_class, contact_mechanism_args = {}, contact_purposes = []) ⇒ Object
find all contacts
291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 291 def find_contacts(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 end |
#find_contacts_with_purpose(contact_mechanism_class, contact_purpose) ⇒ Object
find all contacts with purpose
267 268 269 270 271 272 273 274 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 267 def find_contacts_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_contacts(contact_mechanism_class, nil, [contact_purpose]) end |
#get_primary_contact(contact_mechanism_class) ⇒ Object
231 232 233 234 235 236 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 231 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_contact?(contact_mechanism_klass, contact_purpose) ⇒ Boolean
Check if record has contact with purpose
87 88 89 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 87 def has_contact?(contact_mechanism_klass, contact_purpose) !contact_mechanisms_to_hash(contact_mechanism_klass, [contact_purpose]).empty? end |
#has_contact_with_purpose?(contact_mechanism_class, contact_purpose) ⇒ Boolean
262 263 264 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 262 def has_contact_with_purpose?(contact_mechanism_class, contact_purpose) !find_contact_with_purpose(contact_mechanism_class, contact_purpose).nil? end |
#has_phone_number?(phone_number) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 57 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_zip_code?(zip) ⇒ Boolean
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 69 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 |
#phone_numbers ⇒ Object
53 54 55 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 53 def phone_numbers find_all_contacts_by_contact_mechanism(PhoneNumber) end |
#phone_numbers_to_hash(contact_purposes = nil) ⇒ Object
Converts PhoneNumber contact mechanisms related to this record to an array of hashes containing the contact records data
98 99 100 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 98 def phone_numbers_to_hash(contact_purposes=nil) contact_mechanisms_to_hash(PhoneNumber, contact_purposes) end |
#postal_addresses ⇒ Object
45 46 47 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 45 def postal_addresses find_all_contacts_by_contact_mechanism(PostalAddress) end |
#postal_addresses_to_hash(contact_purposes = nil) ⇒ Object
Converts PostalAddress contact mechanisms related to this record to an array of hashes containing the contact records data
120 121 122 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 120 def postal_addresses_to_hash(contact_purposes=nil) contact_mechanisms_to_hash(PostalAddress, contact_purposes) end |
#primary_email_address ⇒ Object Also known as: primary_email
183 184 185 186 187 188 189 190 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 183 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=
194 195 196 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 194 def primary_email_address=(email_address) self.set_primary_contact(EmailAddress, email_address) end |
#primary_phone_number ⇒ Object Also known as: primary_phone
166 167 168 169 170 171 172 173 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 166 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=
177 178 179 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 177 def primary_phone_number=(phone_number) self.set_primary_contact(PhoneNumber, phone_number) end |
#primary_postal_address ⇒ Object Also known as: primary_address
200 201 202 203 204 205 206 207 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 200 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=
211 212 213 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 211 def primary_postal_address=(postal_address) self.set_primary_contact(PostalAddress, postal_address) end |
#set_primary_contact(contact_mechanism_class, contact_mechanism_instance) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 217 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 |
#update_contact(contact_mechanism_class, contact, contact_mechanism_args) ⇒ Object
348 349 350 351 352 353 354 355 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 348 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_args.delete_if { |k, v| ['client_utc_offset'].include? k.to_s } contact.contact_mechanism.update_attributes!(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
343 344 345 346 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 343 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
331 332 333 334 335 336 337 338 339 |
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb', line 331 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 |