<commit_before>class ApplicationSearch

  attr_reader :error_message

  def initialize(reference, current_user)
    @reference = reference
    @current_user = current_user
  end

  def for_hwf
    return unless @reference.present?
    return false if application_exists_and_user_can_access
    return false if application_exists_and_user_cannot_access

    if online_application_exists
      @online_application
    else
      @error_message = I18n.t(:not_found, scope: scope)
      false
    end
  end

  private

  def application_exists_and_user_can_access
    if application_exists && user_can_access
      redirect_data = CompletedApplicationRedirect.new(@application)
      @error_message = I18n.t(:processed_html, scope: scope, application_path: redirect_data.path)
    end
  end

  def application_exists_and_user_cannot_access
    if application_exists && !user_can_access
      @error_message = I18n.t(:processed_by, scope: scope, office_name: application_office)
    end
  end

  def application_exists
    @application ||= Application.find_by(reference: @reference.upcase)
  end

  def user_can_access
    @application.office == @current_user.office
  end

  def online_application_exists
    @online_application ||= OnlineApplication.find_by(reference: @reference.upcase)
  end
<commit_msg>Updated application_search<commit_after>class ApplicationSearch

  attr_reader :error_message

  def initialize(reference, current_user)
    @reference = reference
    @current_user = current_user
  end

  def for_hwf
    return unless @reference.present?
    @reference.upcase!
    return false if application_exists_and_user_can_access
    return false if application_exists_and_user_cannot_access

    if online_application_exists
      @online_application
    else
      @error_message = I18n.t(:not_found, scope: scope)
      false
    end
  end

  private

  def application_exists_and_user_can_access
    if application_exists && user_can_access
      redirect_data = CompletedApplicationRedirect.new(@application)
      @error_message = I18n.t(:processed_html, scope: scope, application_path: redirect_data.path)
    end
  end

  def application_exists_and_user_cannot_access
    if application_exists && !user_can_access
      @error_message = I18n.t(:processed_by, scope: scope, office_name: application_office)
    end
  end

  def application_exists
    @application ||= Application.find_by(reference: @reference.upcase)
  end

  def user_can_access
    ApplicationPolicy.new(@current_user, @application).show?
  end

  def online_application_exists
    @online_application ||= OnlineApplication.find_by(reference: @reference.upcase)
  end

{'fraction_non_alphanumeric': 0.062146892655367235, 'fraction_numerical': 0.004519774011299435, 'mean_word_length': 4.387423935091278, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6181646', 'n_tokens_mistral': 805, 'n_tokens_neox': 766, 'n_words': 166}