<commit_before>module Users
  class InvitationsController < Devise::InvitationsController
    respond_to :html
    before_action :build_role_list, only: [:new, :create]

    skip_before_action :authenticate_user!, only: [:edit, :update, :destroy]
    skip_after_action :verify_authorized, only: [:edit, :update, :destroy]

    def new
      @user = User.new
      authorize @user

      render :new
    end

    def create
      user_for_authorisation
      authorize user_for_authorisation
      if user_for_authorisation.deleted?
        flash[:alert] = t('devise.invitations.user_exists', email: Settings.mail.tech_support)
        @user = user_for_authorisation
        render :new
      else
        super
      end
    end

    private

    def build_role_list
      if current_user.admin?
        @roles = User::ROLES
      else
        @roles = %w[user manager]
      end
    end

    def invite_params
      params.require(:user).permit(:email, :role, :name, :office_id)
    end

    def user_for_authorisation
      deleted_user_exists? || User.new(invite_params)
    end

    def deleted_user_exists?
      User.with_deleted.find_by(email: invite_params[:email])
    end
  end
end<commit_msg>Cache the user variable to allow re-use<commit_after>module Users
  class InvitationsController < Devise::InvitationsController
    respond_to :html
    before_action :build_role_list, only: [:new, :create]

    skip_before_action :authenticate_user!, only: [:edit, :update, :destroy]
    skip_after_action :verify_authorized, only: [:edit, :update, :destroy]

    def new
      @user = User.new
      authorize @user

      render :new
    end

    def create
      authorize user_for_authorisation
      if @user.deleted?
        flash[:alert] = t('devise.invitations.user_exists', email: Settings.mail.tech_support)
        render :new
      else
        super
      end
    end

    private

    def build_role_list
      if current_user.admin?
        @roles = User::ROLES
      else
        @roles = %w[user manager]
      end
    end

    def invite_params
      params.require(:user).permit(:email, :role, :name, :office_id)
    end

    def user_for_authorisation
      @user ||= deleted_user_exists? || User.new(invite_params)
    end

    def deleted_user_exists?
      User.with_deleted.find_by(email: invite_params[:email])
    end
  end
end
{'fraction_non_alphanumeric': 0.08655070004242682, 'fraction_numerical': 0.0, 'mean_word_length': 2.7909967845659165, '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': '4815642', 'n_tokens_mistral': 750, 'n_tokens_neox': 732, 'n_words': 171}