<commit_before>class User < ActiveRecord::Base
  RELATIONSHIP = {
    :single => "Single"
  }


  has_many :person_a_matches, :class_name => "Match", :foreign_key => 'person_a_id'
  has_many :person_b_matches, :class_name => "Match", :foreign_key => 'person_b_id'
  has_many :recommendations, :class_name => "Match", :foreign_key => 'recommender_id'
  has_many :skipped, :class_name => "Match", :foreign_key => 'skipped_user_id'
  has_and_belongs_to_many :likes, :join_table => "users_likes"

  # from: http://railscasts.com/episodes/163-self-referential-association

  has_many :friendships
  has_many :friends, :through => :friendships, :source => :friend
  has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
  has_many :inverse_friends, :through => :inverse_friendships, :source => :user
  has_many :newmatches, :through => :friendships, :source => :friend, :conditions => "gender = 'female'"

  has_one :location
  has_many :interestedins
  has_many :meetingsfors

  validates_uniqueness_of :fb_id


  def self.fromFacebookUserObj(fbUserObj, full_retrieval = nil)
    user = User.find_by_fb_id(fbUserObj.identifier)
    if user.nil?
      user = User.new
      user.fb_id               = fbUserObj.identifier
    end

      if fbUserObj.respond_to?('profile') # think this iswhether this is the authenticated user or someone else
        fbUserObj = fbUserObj.profile
      end

      user.name                = fbUserObj.name
      user.gender              = fbUserObj.gender
      user.first_name          = fbUserObj.first_name
      user.last_name           = fbUserObj.last_name
      user.relationship_status = fbUserObj.relationship_status
      user.birthday            = fbUserObj.birthday
      user.locale              = fbUserObj.locale

      unless fbUserObj.birthday.nil? || fbUserObj.birthday.year == 0
        user.clean_birthday = fbUserObj.birthday
      end

      user.touch(:last_retrieved) if full_retrieval

    user.save
    return user
  end

  def populate_groups(group_list)
    group_list.each do |group|
      next if group.name.nil?
      
      Rails.logger.info "Logging " << group.name
      l = Like.find_by_fb_id(group.identifier)
      if l.nil?
        l = Like.new
        l.fb_id = group.identifier
        l.name = group.name
        l.link = group.endpoint
        l.save
      end
      
      self.likes << l
    end
    self.save
  end

  def populate_likes(like_list)
    like_list.each do |like|
      next if like.name.nil?
      
      Rails.logger.info "Logging " << like.name
      puts like.name
      l = Like.find_by_fb_id(like.identifier)
      if l.nil?
        l = Like.new
        l.fb_id = like.identifier
        l.name = like.name
        l.category = like.category
        l.link = like.endpoint
        l.save
      end
      
      self.likes << l
    end
    self.save
  end

  def next_match(person_id = nil)
    #Generates the next match for the provided person 
    #or generates a completely new match set
    #This match is saved!
    #Silently ignores person_id if invalid
    #But, returns nil if unable to generat a match

    person_a = person_id.nil? ? nil : friends.where("users.id = ?", person_id).first
    person_a = get_matchable_person if person_a.nil?

    return nil if person_a.nil?

    opposite_gender = (person_a.gender =="male") ? "female" : "male"

    person_b = get_matchable_person(opposite_gender)

    return nil if person_b.nil?

    m = Match.new
    m.status = Match::STATUS[:notselected]
    m.recommender = self
    m.person_a = person_a
    m.person_b = person_b
    #
    Rails.logger.info "Generating match between #{person_a.name} and #{person_b.name}"
    m.save and return m
  end

  protected
    def get_matchable_person(gender = nil)
      # > 18
      # 2) relationship_status = "not married"
<commit_msg>Update user model<commit_after>class User < ActiveRecord::Base
  RELATIONSHIP = {
    :single => "Single"
  }

  has_many :person_a_matches, :class_name => "Match", :foreign_key => 'person_a_id'
  has_many :person_b_matches, :class_name => "Match", :foreign_key => 'person_b_id'
  has_many :recommendations, :class_name => "Match", :foreign_key => 'recommender_id'
  has_many :skipped, :class_name => "Match", :foreign_key => 'skipped_user_id'
  has_and_belongs_to_many :likes, :join_table => "users_likes"

  # from: http://railscasts.com/episodes/163-self-referential-association

  has_many :friendships
  has_many :friends, :through => :friendships, :source => :friend
  has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
  has_many :inverse_friends, :through => :inverse_friendships, :source => :user
  has_many :newmatches, :through => :friendships, :source => :friend, :conditions => "gender = 'female'"

  has_one :location
  has_many :interestedins
  has_many :meetingsfors

  validates_uniqueness_of :fb_id

  def self.fromFacebookUserObj(fbUserObj, is_full_fetch = false)
    logger.info "fromFacebookUserObj"
    #will not populate friends, groups, likes
    #check first to see if there is an existing row in the database for this user
    user = User.find_by_fb_id(fbUserObj.identifier)
    user = User.new if user.nil?

    user.populate_from_fbUser(fbUserObj, is_full_fetch)
    return user
  end

  def fetch_and_populate()
      return false if fb_id.nil?

      fbObj = FbGraph::User.fetch(fb_id, :access_token => current_user.access_token)
      populate_from_fbUser(fbObj)
  end

  def fetch_and_populate_by_fb_id(new_fb_id)
    return false if new_fb_id.nil?

    self.fb_id = new_fb_id
    fetch_and_populate
  end

  def populate_from_fbUser(fbUserObj, is_full_fetch = false)
    logger.info "populate_from_fbUser"
    #will not populate friends, groups, likes
    #return boolean
      if fbUserObj.respond_to?('profile') # think this iswhether this is the authenticated user or someone else
        fbUserObj = fbUserObj.profile
      end

      self.fb_id               = fbUserObj.identifier
      self.name                = fbUserObj.name
      self.gender              = fbUserObj.gender
      self.first_name          = fbUserObj.first_name
      self.last_name           = fbUserObj.last_name
      self.relationship_status = fbUserObj.relationship_status
      self.birthday            = fbUserObj.birthday
      self.locale              = fbUserObj.locale
      self.link                = fbUserObj.link
      self.bio                 = fbUserObj.bio
      self.quotes              = fbUserObj.quotes
      self.religion            = fbUserObj.religion
      self.political           = fbUserObj.political
      self.fb_verified         = fbUserObj.verified
      self.updated_time        = fbUserObj.updated_time
      self.email               = fbUserObj.email
      self.highest_education   = get_highest_education_level(fbUserObj.education)

      unless fbUserObj.birthday.nil? || fbUserObj.birthday.year == 0
        user.clean_birthday = fbUserObj.birthday
      end

      self.touch(:last_retrieved) if is_full_fetch

      self.save
  end

    user.save
    return user
  end

  def populate_groups(group_list)
    group_list.each do |group|
      next if group.name.nil?
      
      Rails.logger.info "Logging " << group.name
      l = Like.find_by_fb_id(group.identifier)
      if l.nil?
        l = Like.new
        l.fb_id = group.identifier
        l.name = group.name
        l.link = group.endpoint
        l.save
      end
      
      self.likes << l
    end
    self.save
  end

  def populate_likes(like_list)
    like_list.each do |like|
      next if like.name.nil?
      
      Rails.logger.info "Logging " << like.name
      puts like.name
      l = Like.find_by_fb_id(like.identifier)
      if l.nil?
        l = Like.new
        l.fb_id = like.identifier
        l.name = like.name
        l.category = like.category
        l.link = like.endpoint
        l.save
      end
      
      self.likes << l
    end
    self.save
  end

  def next_match(person_id = nil)
    #Generates the next match for the provided person 
    #or generates a completely new match set
    #This match is saved!
    #Silently ignores person_id if invalid
    #But, returns nil if unable to generat a match

    person_a = person_id.nil? ? nil : friends.where("users.id = ?", person_id).first
    person_a = get_matchable_person if person_a.nil?

    return nil if person_a.nil?

    opposite_gender = (person_a.gender =="male") ? "female" : "male"

    person_b = get_matchable_person(opposite_gender)

    return nil if person_b.nil?

    m = Match.new
    m.status = Match::STATUS[:notselected]
    m.recommender = self
    m.person_a = person_a
    m.person_b = person_b
    #
    Rails.logger.info "Generating match between #{person_a.name} and #{person_b.name}"
    m.save and return m
  end

  protected
    def get_highest_education_level(education_array)
      return ""
    end

    def get_matchable_person(gender = nil)
      # > 18
      # 2) relationship_status = "not married"

{'fraction_non_alphanumeric': 0.0920877659574468, 'fraction_numerical': 0.0015514184397163121, 'mean_word_length': 2.6259541984732824, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1258133', 'n_tokens_mistral': 3102, 'n_tokens_neox': 2910, 'n_words': 660}