Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. class ContactDetail < ActiveRecord::Base
  2. belongs_to :session
  3. before_save :normalize_phone_number
  4. after_commit :check_for_connections
  5.  
  6. def self.find_by_phone_number(phone_number)
  7. ContactDetail.find_by(key: 'phone_number', value: phone_number)
  8. end
  9.  
  10. def self.find_phone_number_by_session(session)
  11. session.contact_details.find_by(key: 'phone_number').value rescue nil
  12. end
  13.  
  14. def normalize_phone_number
  15. return unless self.key == 'phone_number'
  16. # TODO: This will not work when we turn hashing back on!
  17. self.value = PhoneNumberHashService.to_hash(self.value)
  18. end
  19.  
  20. def check_for_connections
  21. return unless self.key == 'phone_number'
  22. if pn = PhoneNumber.find_by_phone_number(self.value)
  23. handle_neo4j_connections(pn)
  24. handle_player_connections(pn)
  25. end
  26. end
  27. private
  28.  
  29. def handle_player_connections(rel)
  30. rel.contact_book_items.each do |cbi|
  31.  
  32. # Link up contact book items with real users
  33. cbi.update_attributes(contact_session: self.session)
  34. # Link up players with real users
  35. cbi.players.each do |player|
  36. player.update_attributes(session: self.session)
  37. end
  38.  
  39. end
  40. end
  41.  
  42. def handle_neo4j_connections(pn)
  43. pn_n4j = pn.neo4j_record
  44.  
  45. if user_n4j = self.session.neo4j_record
  46. user_n4j.phone_numbers << pn_n4j
  47. user_n4j.save
  48.  
  49. logger.info "Connecting ContactBook Associations with User"
  50. friends = Neo4jModels::ContactBookItem.where(phone_numbers: pn_n4j)
  51. .flat_map(&:contact_books)
  52. .flat_map(&:users)
  53.  
  54. user_n4j.contact_book_associations << friends
  55. user_n4j.save
  56. else
  57. logger.warn "Not connecting user to phone_numbers - user isn't in neo4j"
  58. end
  59. end
  60.  
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement