<commit_before># Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

# rubocop:disable ClassLength
class ApplicationController < ActionController::Base
  helper :layout
  # See ActionController::RequestForgeryProtection for details
  protect_from_forgery
  before_action :app_setup_check
  before_action :authenticate_user!, unless: :skip_authentication?
  before_action :cart, unless: :devise_controller?

  with_options unless: ->(_u) { User.count == 0 } do |c|
    c.before_filter :load_configs
    c.before_filter :seen_app_configs
    c.before_filter :fix_cart_date
    c.before_filter :set_view_mode
    c.before_filter :check_view_mode
    c.before_filter :make_cart_compatible
  end

  helper_method :cart, :current_or_guest_user

  rescue_from CanCan::AccessDenied do |_exception|
    flash[:error] = 'Sorry, that action or page is restricted.'
    if current_user && current_user.view_mode == 'banned'
      flash[:error] = 'That action is restricted; it looks like you\'re a '\
        'banned user! Talk to your administrator, maybe they\'ll be willing '\
        'to lift your restriction.'
    end
    # redirect_to request.referer ? request.referer : main_app.root_url
    redirect_to main_app.root_url
  end

  # -------- before_filter methods -------- #

  def app_setup_check
    return if AppConfig.first && (User.count != 0)
    flash[:notice] = 'Hey there! It looks like you haven\'t fully set up '\
      'your application yet. To create your first superuser and configure '\
      'the application, please run $bundle exec rake app:setup in the '\
      'terminal. For more information, please see our github page: '\
      'https://github.com/YaleSTC/reservations'
    render file: 'application_setup/index', layout: 'application'
  end

  def load_configs
    @app_configs = AppConfig.first
  end

  def seen_app_configs
    return if AppConfig.first.viewed || current_user.nil?
    if can? :edit, :app_config
      flash[:notice] = 'Since this is your first time viewing the '\
        'application configurations, we recommend that you take some time '\<commit_msg>handle norecorderror in application controller<commit_after># Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

# rubocop:disable ClassLength
class ApplicationController < ActionController::Base
  helper :layout
  # See ActionController::RequestForgeryProtection for details
  protect_from_forgery
  before_action :app_setup_check
  before_action :authenticate_user!, unless: :skip_authentication?
  before_action :cart, unless: :devise_controller?

  with_options unless: ->(_u) { User.count == 0 } do |c|
    c.before_filter :load_configs
    c.before_filter :seen_app_configs
    c.before_filter :fix_cart_date
    c.before_filter :set_view_mode
    c.before_filter :check_view_mode
    c.before_filter :make_cart_compatible
  end

  helper_method :cart, :current_or_guest_user

  rescue_from CanCan::AccessDenied do |_exception|
    flash[:error] = 'Sorry, that action or page is restricted.'
    if current_user && current_user.view_mode == 'banned'
      flash[:error] = 'That action is restricted; it looks like you\'re a '\
        'banned user! Talk to your administrator, maybe they\'ll be willing '\
        'to lift your restriction.'
    end
    # redirect_to request.referer ? request.referer : main_app.root_url
    redirect_to main_app.root_url
  end

  rescue_from ActiveRecord::RecordNotFound do |_exception|
    flash[:error] = 'Oops, you tried to go somewhere that doesn\'t exist.'
    redirect_to main_app.root_url
  end

  # -------- before_filter methods -------- #

  def app_setup_check
    return if AppConfig.first && (User.count != 0)
    flash[:notice] = 'Hey there! It looks like you haven\'t fully set up '\
      'your application yet. To create your first superuser and configure '\
      'the application, please run $bundle exec rake app:setup in the '\
      'terminal. For more information, please see our github page: '\
      'https://github.com/YaleSTC/reservations'
    render file: 'application_setup/index', layout: 'application'
  end

  def load_configs
    @app_configs = AppConfig.first
  end

  def seen_app_configs
    return if AppConfig.first.viewed || current_user.nil?
    if can? :edit, :app_config
      flash[:notice] = 'Since this is your first time viewing the '\
        'application configurations, we recommend that you take some time '\
{'fraction_non_alphanumeric': 0.08776132404181185, 'fraction_numerical': 0.0008710801393728223, 'mean_word_length': 3.739938080495356, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2260131', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1325, 'n_words': 433}