<commit_before>class CompetitionTransfer < ActiveRecord::Base
  include Rails.application.routes.url_helpers

  belongs_to :user
  belongs_to :roster, foreign_key: 'competition_roster_id', class_name: 'CompetitionRoster'
  delegate :team,        to: :roster,   allow_nil: true
  delegate :division,    to: :roster,   allow_nil: true
  delegate :competition, to: :division, allow_nil: true

  validates :user, presence: true
  validates :roster, presence: true
  validates :is_joining, inclusion: { in: [true, false] }
  validates :approved, inclusion: { in: [true, false] }
  validate :single_player_entry, on: :create
  validate :on_team, on: :create
  validate :within_roster_size, on: :create

  after_initialize :set_defaults, unless: :persisted?

  after_create do
    next unless is_joining?

    msg = if approved?
            "You have been entered in #{competition.name} with '#{roster.name}'."
          else
            "It has been requested for you to transfer into '#{roster.name}' "\
            "for #{competition.name}."
          end

    user.notify!(msg, league_roster_path(competition, roster))
  end

  after_create do
    next if is_joining?

    msg = if approved?
            "You have been removed from '#{roster.name}' for #{competition.name}."
          else
            "It has been requested for you to transfer out of '#{roster.name}' "\
            "for #{competition.name}."
          end

    user.notify!(msg, league_roster_path(competition, roster))
  end

  after_update do
    next unless approved?

    if is_joining?
      user_msg = "The request to transfer into '#{roster.name}' for #{competition.name} "\
                 'has been approved.'
      capt_msg = "The request to transfer '#{user.name}' into '#{roster.name}' for "\
                 "#{competition.name} has been approved."
    else
      user_msg = "The request to transfer out of '#{roster.name}' for #{competition.name} "\
                 'has been approved.'
      capt_msg = "The request to transfer '#{user.name}' out of '#{roster.name}' for "\
                 "#{competition.name} has been approved."
    end

    user.notify!(user_msg, league_roster_path(competition, roster))
    User.get_revokeable(:edit, team).each do |captain|
      captain.notify!(capt_msg, user_path(user))
    end
  end

  before_destroy do
    if is_joining?
      user_msg = "The request to transfer into '#{roster.name}' for #{competition.name} "\
                 'has been denied.'
      capt_msg = "The request to transfer '#{user.name}' into '#{roster.name}' for "\
                 "#{competition.name} has been denied."
    else
      user_msg = "The request to transfer out of '#{roster.name}' for #{competition.name} "\
                 'has been denied.'
      capt_msg = "The request to transfer '#{user.name}' out of '#{roster.name}' for "\
                 "#{competition.name} has been denied."
    end

    user.notify!(user_msg, league_roster_path(competition, roster))
    User.get_revokeable(:edit, team).each do |captain|
      captain.notify!(capt_msg, user_path(user))
    end
  end

  private

  def set_defaults
    if competition.present? && (!competition.transfers_require_approval? ||
                                competition.signuppable?)
      self.approved = true
    end
  end

  def single_player_entry
    return unless competition.present? && is_joining

    transfers = competition.players.where(user: user)
    if transfers.exists?
      errors.add(:user_id, "is already entered in this league with #{transfers.first.roster.name}")
    end
  end

  def on_team
    return unless user.present? && roster.present?<commit_msg>Made transfers only auto-approve for pending rosters<commit_after>class CompetitionTransfer < ActiveRecord::Base
  include Rails.application.routes.url_helpers

  belongs_to :user
  belongs_to :roster, foreign_key: 'competition_roster_id', class_name: 'CompetitionRoster'
  delegate :team,        to: :roster,   allow_nil: true
  delegate :division,    to: :roster,   allow_nil: true
  delegate :competition, to: :division, allow_nil: true

  validates :user, presence: true
  validates :roster, presence: true
  validates :is_joining, inclusion: { in: [true, false] }
  validates :approved, inclusion: { in: [true, false] }
  validate :single_player_entry, on: :create
  validate :on_team, on: :create
  validate :within_roster_size, on: :create

  after_initialize :set_defaults, unless: :persisted?

  after_create do
    next unless is_joining?

    msg = if approved?
            "You have been entered in #{competition.name} with '#{roster.name}'."
          else
            "It has been requested for you to transfer into '#{roster.name}' "\
            "for #{competition.name}."
          end

    user.notify!(msg, league_roster_path(competition, roster))
  end

  after_create do
    next if is_joining?

    msg = if approved?
            "You have been removed from '#{roster.name}' for #{competition.name}."
          else
            "It has been requested for you to transfer out of '#{roster.name}' "\
            "for #{competition.name}."
          end

    user.notify!(msg, league_roster_path(competition, roster))
  end

  after_update do
    next unless approved?

    if is_joining?
      user_msg = "The request to transfer into '#{roster.name}' for #{competition.name} "\
                 'has been approved.'
      capt_msg = "The request to transfer '#{user.name}' into '#{roster.name}' for "\
                 "#{competition.name} has been approved."
    else
      user_msg = "The request to transfer out of '#{roster.name}' for #{competition.name} "\
                 'has been approved.'
      capt_msg = "The request to transfer '#{user.name}' out of '#{roster.name}' for "\
                 "#{competition.name} has been approved."
    end

    user.notify!(user_msg, league_roster_path(competition, roster))
    User.get_revokeable(:edit, team).each do |captain|
      captain.notify!(capt_msg, user_path(user))
    end
  end

  before_destroy do
    if is_joining?
      user_msg = "The request to transfer into '#{roster.name}' for #{competition.name} "\
                 'has been denied.'
      capt_msg = "The request to transfer '#{user.name}' into '#{roster.name}' for "\
                 "#{competition.name} has been denied."
    else
      user_msg = "The request to transfer out of '#{roster.name}' for #{competition.name} "\
                 'has been denied.'
      capt_msg = "The request to transfer '#{user.name}' out of '#{roster.name}' for "\
                 "#{competition.name} has been denied."
    end

    user.notify!(user_msg, league_roster_path(competition, roster))
    User.get_revokeable(:edit, team).each do |captain|
      captain.notify!(capt_msg, user_path(user))
    end
  end

  private

  def set_defaults
    if competition.present? && (!competition.transfers_require_approval? ||
                                !roster.approved?)
      self.approved = true
    end
  end

  def single_player_entry
    return unless competition.present? && is_joining

    transfers = competition.players.where(user: user)
    if transfers.exists?
      errors.add(:user_id, "is already entered in this league with #{transfers.first.roster.name}")
    end
  end

  def on_team
    return unless user.present? && roster.present?
{'fraction_non_alphanumeric': 0.11280653950953679, 'fraction_numerical': 0.0, 'mean_word_length': 2.7762345679012346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4312924', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2181, 'n_words': 637}