<commit_before>module Leagues
  class TransfersController < ApplicationController
    include LeaguePermissions

    before_action only: [:index] { @league = League.find(params[:league_id]) }
    before_action except: [:index] do
      @transfer_request = League::Roster::TransferRequest.find(params[:id])
      @league = @transfer_request.league
    end

    before_action :require_user_league_permission

    def index
      @divisions = @league.divisions
                          .includes(transfer_requests: :user)
                          .references(:transfer_requests)
                          .merge(League::Roster::TransferRequest.order(:created_at))

      @pending_transfer_requests = {}
      @old_transfer_requests = {}
      sort_transfer_requests
    end

    def update
      Transfers::ApprovalService.call(@transfer_request, current_user)

      flash[:error] = @transfer_request.errors.full_messages.first
      index
      render :index
    end

    def destroy
      Transfers::DenialService.call(@transfer_request, current_user)

      flash[:error] = @transfer_request.errors.full_messages.first
      index
      render :index
    end

    private

    def require_user_league_permission
      redirect_to league_path(@league) unless user_can_edit_league?
    end

    def sort_transfer_requests<commit_msg>Added ordering to divisions on league transfers page. Fixes #242.<commit_after>module Leagues
  class TransfersController < ApplicationController
    include LeaguePermissions

    before_action only: [:index] { @league = League.find(params[:league_id]) }
    before_action except: [:index] do
      @transfer_request = League::Roster::TransferRequest.find(params[:id])
      @league = @transfer_request.league
    end

    before_action :require_user_league_permission

    def index
      @divisions = @league.divisions
                          .includes(transfer_requests: :user)
                          .references(:transfer_requests)
                          .order(:id)
                          .merge(League::Roster::TransferRequest.order(:created_at))

      @pending_transfer_requests = {}
      @old_transfer_requests = {}
      sort_transfer_requests
    end

    def update
      Transfers::ApprovalService.call(@transfer_request, current_user)

      flash[:error] = @transfer_request.errors.full_messages.first
      index
      render :index
    end

    def destroy
      Transfers::DenialService.call(@transfer_request, current_user)

      flash[:error] = @transfer_request.errors.full_messages.first
      index
      render :index
    end

    private

    def require_user_league_permission
      redirect_to league_path(@league) unless user_can_edit_league?
    end

    def sort_transfer_requests
{'fraction_non_alphanumeric': 0.08013052936910806, 'fraction_numerical': 0.0010877447425670776, 'mean_word_length': 2.896892655367232, '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': '3218004', 'n_tokens_mistral': 766, 'n_tokens_neox': 722, 'n_words': 139}