Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ContactDetail < ActiveRecord::Base
- belongs_to :session
- before_save :normalize_phone_number
- after_commit :check_for_connections
- def self.find_by_phone_number(phone_number)
- ContactDetail.find_by(key: 'phone_number', value: phone_number)
- end
- def self.find_phone_number_by_session(session)
- session.contact_details.find_by(key: 'phone_number').value rescue nil
- end
- def normalize_phone_number
- return unless self.key == 'phone_number'
- # TODO: This will not work when we turn hashing back on!
- self.value = PhoneNumberHashService.to_hash(self.value)
- end
- def check_for_connections
- return unless self.key == 'phone_number'
- if pn = PhoneNumber.find_by_phone_number(self.value)
- handle_neo4j_connections(pn)
- handle_player_connections(pn)
- end
- end
- private
- def handle_player_connections(rel)
- rel.contact_book_items.each do |cbi|
- # Link up contact book items with real users
- cbi.update_attributes(contact_session: self.session)
- # Link up players with real users
- cbi.players.each do |player|
- player.update_attributes(session: self.session)
- end
- end
- end
- def handle_neo4j_connections(pn)
- pn_n4j = pn.neo4j_record
- if user_n4j = self.session.neo4j_record
- user_n4j.phone_numbers << pn_n4j
- user_n4j.save
- logger.info "Connecting ContactBook Associations with User"
- friends = Neo4jModels::ContactBookItem.where(phone_numbers: pn_n4j)
- .flat_map(&:contact_books)
- .flat_map(&:users)
- user_n4j.contact_book_associations << friends
- user_n4j.save
- else
- logger.warn "Not connecting user to phone_numbers - user isn't in neo4j"
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement