<commit_before>class Request < ActiveRecord::Base
  #belongs_to :user
  belongs_to :team, foreign_key: "source_id"
  
  validates :source_id, numericality: true
  validates :target_id, numericality: true
  validates :team, presence: true
  validate :target_cannot_be_own_team, :new_size_cannot_be_too_big, :target_exists
  
  def target_cannot_be_own_team
    if self.target_id == self.source_id
      errors.add(:target, "Target cannot be own team")
    end
  end
  
  def new_size_cannot_be_too_big
    target = Team.find_by(id: self.target_id)
    source = Team.find_by(id: self.source_id)
    if target.getNumMembers + source.getNumMembers > Option.maximum_team_size
      errors.add(:size, "Target cannot accomodate all members")
    end
  end
  
  def target_exists
    if !Team.find_by(id: self.target_id)
      errors.add(:target, "Target does not exist")
    end
  end

  def target_users_list
      return Array.wrap(Team.find(self.target_id).users)
  end

  def join(source, target)
    #Push all the users from the source onto the target
    source.users.each do |user|
      target.users << user
      target.update_waitlist    
      user.team = target
    end
    #Make sure old requests from the source team are now using the new id
    old_requests = Request.where(source_id: source.id)
    old_requests.each do |req|
      target.requests << req
      req.team = target
    end
    
    #Delete the old team which the targets belonged to
    source.destroy
  end
  
  def showTargets
    Team.find_by(id: self.target_id).getMembers
  end<commit_msg>Fixed issue with reciprocal requests<commit_after>class Request < ActiveRecord::Base
  #belongs_to :user
  belongs_to :team, foreign_key: "source_id"
  
  validates :source_id, numericality: true
  validates :target_id, numericality: true
  validates :team, presence: true
  validate :target_cannot_be_own_team, :new_size_cannot_be_too_big, :target_exists
  
  def target_cannot_be_own_team
    if self.target_id == self.source_id
      errors.add(:target, "Target cannot be own team")
    end
  end
  
  def new_size_cannot_be_too_big
    target = Team.find_by(id: self.target_id)
    source = Team.find_by(id: self.source_id)
    if target.getNumMembers + source.getNumMembers > Option.maximum_team_size
      errors.add(:size, "Target cannot accomodate all members")
    end
  end
  
  def target_exists
    if !Team.find_by(id: self.target_id)
      errors.add(:target, "Target does not exist")
    end
  end

  def target_users_list
      return Array.wrap(Team.find(self.target_id).users)
  end

  def join(source, target)
    #Push all the users from the source onto the target
    source.users.each do |user|
      target.users << user
      target.update_waitlist    
      user.team = target
    end
    #Make sure old requests from the source team are now using the new id
    old_requests = Request.where(source_id: source.id)
    old_requests.each do |req|
      target.requests << req
      req.team = target
    end
    #Make sure old requests to the source team are now using the new id
    #Unless the old target was the current team, then don't do that
    old1_requests = Request.where(target_id: source.id)
    old1_requests.each do |req|
      if req.source_id == target.id
        target.requests.destroy(req)
      else
        req.target_id = target.id
      end
    end
    
    #Delete the old team 
    source.destroy
  end
  
  def showTargets
    Team.find_by(id: self.target_id).getMembers
  end
{'fraction_non_alphanumeric': 0.07560137457044673, 'fraction_numerical': 0.000572737686139748, 'mean_word_length': 3.1386255924170614, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3787633', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1118, 'n_words': 319}