<commit_before># -*- coding: utf-8 -*-
class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :init_body_class, :mailer_set_url_options

  # Set a permanent cookie w/data from the user profile. Used by the masthead in other webapps
  def set_profile_cookie
    departments = current_user.roles.map { |d| { name: d.name, homepage_url: d.homepage_url } if d.category == 'department' }.compact
    workingfields = current_user.roles.map { |w| { name: w.name, homepage_url: w.homepage_url } if w.category == 'working_field' }.compact

    if Rails.env.development?
      myprofile = "myprofile-development"
    elsif Rails.env.test?
      myprofile = "myprofile-test"
    else
      myprofile = "myprofile"
    end

    cookies.permanent[myprofile] = {
      value: {
        # Singular is for old style masthead
        department:   departments.present? ? departments.first[:homepage_url].gsub('http://komin.malmo.se/', '') : '',
        workingfield: workingfields.present? ? workingfields.first[:homepage_url].gsub('http://komin.malmo.se/', '') : '',
        # Plural for masthead w/dropdowns
        departments:   departments,
        workingfields: workingfields
      }.to_json,
      expires: 365*10,
      path: '/',
      domain: 'malmo.se'
    }
  end

  # Save user agent and set permanent cookie for "remember me"
  def track_user_agent
    # Existing tracker from the browser
    tracker_id = cookies.signed[:user_agent].present? ? cookies.signed[:user_agent][:id] : nil

    # Get or create tracker from the UserAgent model
    tracker = UserAgent.track(current_user.id, tracker_id, params[:remember_me], request.env['HTTP_USER_AGENT'] )

    # Set tracker in browser
    cookies.permanent.signed[:user_agent] = {
      value:  { id: tracker[:id], token: tracker[:token] },
      secure: !Rails.env.development?,
      httponly: true,
      path: root_path
    }
  end

  # Catch errors and record not founds
  unless Rails.env.development?
    rescue_from ActionController::RoutingError, with: :not_found
    rescue_from AbstractController::ActionNotFound, with: :not_found
    rescue_from ActiveRecord::RecordNotFound, with: :not_found
    rescue_from Exception, with: :error_page
  end
  def action_missing(method, *args)
    not_found
  end

  def not_found(exception = "404")
    logger.warn "Exception: #{exception}"
    logger.warn "  Full path: #{request.fullpath}"
    logger.warn "  Referer: #{request.referer}"
    reset_body_classes
    render template: "404", status: 404
  end

  def error_page(exception = "500", msg = "Prova att navigera med menyn ovan.")
    logger.error "Exception: #{exception}"
    logger.error "  User id: #{session[:user_id] ? session[:user_id] : 'not logged in'}"
    logger.error "  Referer: #{request.referer}"
    logger.error "  Params: #{params}"
    reset_body_classes
    @msg = msg
    render template: "500", status: 500
  end

  protected
<commit_msg>Added UA to error logging<commit_after># -*- coding: utf-8 -*-
class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :init_body_class, :mailer_set_url_options

  # Set a permanent cookie w/data from the user profile. Used by the masthead in other webapps
  def set_profile_cookie
    departments = current_user.roles.map { |d| { name: d.name, homepage_url: d.homepage_url } if d.category == 'department' }.compact
    workingfields = current_user.roles.map { |w| { name: w.name, homepage_url: w.homepage_url } if w.category == 'working_field' }.compact

    if Rails.env.development?
      myprofile = "myprofile-development"
    elsif Rails.env.test?
      myprofile = "myprofile-test"
    else
      myprofile = "myprofile"
    end

    cookies.permanent[myprofile] = {
      value: {
        # Singular is for old style masthead
        department:   departments.present? ? departments.first[:homepage_url].gsub('http://komin.malmo.se/', '') : '',
        workingfield: workingfields.present? ? workingfields.first[:homepage_url].gsub('http://komin.malmo.se/', '') : '',
        # Plural for masthead w/dropdowns
        departments:   departments,
        workingfields: workingfields
      }.to_json,
      expires: 365*10,
      path: '/',
      domain: 'malmo.se'
    }
  end

  # Save user agent and set permanent cookie for "remember me"
  def track_user_agent
    # Existing tracker from the browser
    tracker_id = cookies.signed[:user_agent].present? ? cookies.signed[:user_agent][:id] : nil

    # Get or create tracker from the UserAgent model
    tracker = UserAgent.track(current_user.id, tracker_id, params[:remember_me], request.env['HTTP_USER_AGENT'] )

    # Set tracker in browser
    cookies.permanent.signed[:user_agent] = {
      value:  { id: tracker[:id], token: tracker[:token] },
      secure: !Rails.env.development?,
      httponly: true,
      path: root_path
    }
  end

  # Catch errors and record not founds
  unless Rails.env.development?
    rescue_from ActionController::RoutingError, with: :not_found
    rescue_from AbstractController::ActionNotFound, with: :not_found
    rescue_from ActiveRecord::RecordNotFound, with: :not_found
    rescue_from Exception, with: :error_page
  end
  def action_missing(method, *args)
    not_found
  end

  def not_found(exception = "404")
    logger.warn "Exception: #{exception}"
    logger.warn "  Full path: #{request.fullpath}"
    logger.warn "  Referer: #{request.referer}"
    reset_body_classes
    render template: "404", status: 404
  end

  def error_page(exception = "500", msg = "Prova att navigera med menyn ovan.")
    logger.error "Exception: #{exception}"
    logger.error "  User id: #{session[:user_id] ? session[:user_id] : 'not logged in'}"
    logger.error "  User Agent: #{request.user_agent}"
    logger.error "  Referer: #{request.referer}"
    logger.error "  Params: #{params}"
    reset_body_classes
    @msg = msg
    render template: "500", status: 500
  end

  protected

{'fraction_non_alphanumeric': 0.1159711747947042, 'fraction_numerical': 0.008044243338360985, 'mean_word_length': 3.463724756918474, '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': '1201791', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1854, 'n_words': 466}