Module: ConSync::Syncable
- Defined in:
- lib/con_sync/syncable.rb
Instance Method Summary collapse
- #matched_contacts ⇒ Object
- #phone_number ⇒ Object
- #phone_number=(string) ⇒ Object
- #sync_contacts(contact_hash = {}) ⇒ Object
Instance Method Details
#matched_contacts ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/con_sync/syncable.rb', line 80 def matched_contacts matched_contacts = [] user_phones = [] user_emails = [] self.contacts.each do |con| user_phones.concat con.phones user_emails.concat con.emails end user_phones.each do |phone| u = User.where(phone_number: phone.number.encrypt(:symmetric)).limit(1).first matched_contacts << u unless u.blank? end user_emails.each do |mail| u = User.where(email: mail.email).limit(1).first matched_contacts << u unless u.blank? end return matched_contacts end |
#phone_number ⇒ Object
3 4 5 |
# File 'lib/con_sync/syncable.rb', line 3 def phone_number return self.encrypted_number.try(:decrypt,:symmetric) || "" end |
#phone_number=(string) ⇒ Object
7 8 9 10 11 |
# File 'lib/con_sync/syncable.rb', line 7 def phone_number=(string) self.cc_prefix, num = string.extract_country_code self.encrypted_number = num.encrypt(:symmetric) self.encrypted_number end |
#sync_contacts(contact_hash = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/con_sync/syncable.rb', line 13 def sync_contacts(contact_hash = {}) result = {:new => {success:[], failed:[]}, modified: {success:[], failed:[]}, deleted: {success:[], failed:[]}} raise ArgumentError, "You need to provide contacts hash." if contact_hash.blank? ########################################################################## #########################Create new Contacts############################## if !contact_hash[:new].blank? contacts = contact_hash[:new] contacts.each do |aContact| phones = aContact[:phones] emails = aContact[:emails] aContact.delete :phones aContact.delete :emails newContact = Contact.new(contact_params(aContact)) if phones phones.each do |aPhone| newContact.phones.build(phone_params(aPhone)) end end if emails emails.each do |anEmail| newContact.emails.build(email_params(anEmail)) end end if newContact.save # result[:new][:success] << newContact.record_id self.contacts << newContact else result[:new][:failed] << newContact.record_id end end end ########################################################################## ########################Modify Contacts################################### if !contact_hash[:modified].blank? modified_contacts = contact_hash[:modified] modified_contacts.each do |con| theContact = self.contacts.find_by_record_id(con[:record_id]) if ! theContact.blank? if !theContact.update_attributes(contact_params(con)) result[:modified][:failed] << theContact.record_id puts "Error Updating Contact." else # result[:modified][:success] << theContact.record_id end end end end ########################################################################## ########################Delete Contacts################################### if !contact_hash[:deleted].blank? delete_contacts = contact_hash[:deleted] delete_contacts.each do |con| if self.contacts.find_by_record_id(con[:record_id].to_i).destroy # result[:deleted][:success] << con[:record_id] else result[:deleted][:failed] << con[:record_id] end end end after_contact_sync self.save return result end |