<commit_before>require 'string'
require 'float'

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_defaults
  before_filter :set_lists, :only => [ :new, :create, :update, :edit ]
  before_filter :set_new_for_organizer
  before_filter :redirect_mobile_user

  include ActionController::Caching::Sweeping
  
  rescue_from CanCan::AccessDenied do |exception|
    redirect_to sign_in_path, :notice => t('users.please_sign_in')
  end

  check_authorization

  has_mobile_fu

  private
  
  def after_sign_in_path_for resource
    organizer_path
  end

  def after_sign_out_path_for(resource_or_scope)
    root_path
  end

  def after_sign_in_path_for resource
    organizer_path
  end
  
  def puts! arg
    unless Rails.env.production?
      puts '+++ +++'
      puts arg.inspect
    end
  end
  
  # Get locale from top-level domain or return nil if such locale is not available
  # You have to put something like:
  #   127.0.0.1 application.com
  #   127.0.0.1 application.it
  #   127.0.0.1 application.pl
  def extract_locale_from_tld
    parsed_locale = request.host.split('.').last
    I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale  : nil
  end
  
  # Get locale code from request subdomain (like http://it.application.local:3000)
  #   127.0.0.1 gr.application.local
  def extract_locale_from_subdomain
    parsed_locale = request.subdomains.first || 'en'
    I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : :en
  end

  def extract_layout_from_subdomain
    if 'm' == request.host.split('.').first
      @layout = 'organizer'
    elsif 'm' == request.host.split('.')[1]
      @layout = 'organizer'
    else
      @layout = 'application'
    end
  end

  def default_url_options(options={})
    options[:locale] = I18n.locale || I18n.default_locale
    # options[:layout] = @layout || 'application'
    options
  end
  
  def set_lists
    @cities = City.list
    @tags = Tag.all.list
    unless current_user.blank?
      @galleries = Gallery.where( :user => current_user ).list
      @reports = Report.where( :user => current_user ).list
    end    
  end
  
  def set_defaults
    # I18n.locale = extract_locale_from_tld || I18n.default_locale
    # @locale = I18n.locale = params[:locale] || I18n.default_locale
    # @locale = I18n.locale = extract_locale_from_subdomain
    @locale = I18n.locale = params[:locale] || 'en'

    @layout = extract_layout_from_subdomain

    @domain = request.domain
    @site = Site.where( :domain => @domain, :lang => @locale ).first

    @display_ads = true
    @display_help = true

    if user_signed_in?
      @current_user = current_user || session['current_user']
      @display_ads = @current_user.display_ads
      @display_help = @current_user.display_help
      if session[:current_city].blank?
        unless @current_user.current_city.blank?
          session[:current_city] = {
            :name => @current_user.current_city['name_'+@locale.to_s],
            :cityname => @current_user.current_city.cityname
          }
        end
      end
    end
    
    unless session[:current_city].blank?
      @current_city = City.where( :cityname => session[:current_city][:cityname] ).first
    end

    @action_name = params[:controller].gsub('/', '_') + '_' + params[:action]
    @action_classes = "#{params[:controller].gsub('/', '_')} #{params[:action]}" # #{@locale}

    # for the application header
    @list_citynames = City.where( :is_feature => true ).list_citynames @locale.to_s

    # for the application_mini header
    @parent_tags = Tag.all.where( :parent_tag => nil )
  end

  def load_features args = {}
    @newsitems = []

    if args[:cityname].blank?
      features = YAML.load_file("#{Rails.root}/config/features.yml")
    else
      features = YAML.load_file("#{Rails.root}/config/#{args[:cityname]}_features.yml")
    end
    @features = []
    features.each do |f|
      @features << f.symbolize_keys
    end
  end

  def set_new_for_organizer
    @addressbookitem = Addressbookitem.new
    @cities_user = CitiesUser.new
  end

  def redirect_mobile_user
    if is_mobile_device? && !(request.host.split('.').include? 'm')
      lang = (@locale == 'en') ? '' : "#{@locale}."
      port = (Rails.env.development?) ? ":#{request.port}" : ''
      redirect_to "#{request.protocol}#{lang}m.#{request.domain}#{port}#{request.path}"
    end
  end
  
end<commit_msg>mobile is not ready yet<commit_after>require 'string'
require 'float'

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_defaults
  before_filter :set_lists, :only => [ :new, :create, :update, :edit ]
  before_filter :set_new_for_organizer
  before_filter :redirect_mobile_user

  include ActionController::Caching::Sweeping
  
  rescue_from CanCan::AccessDenied do |exception|
    redirect_to sign_in_path, :notice => t('users.please_sign_in')
  end

  check_authorization

  has_mobile_fu

  private
  
  def after_sign_in_path_for resource
    organizer_path
  end

  def after_sign_out_path_for(resource_or_scope)
    root_path
  end

  def after_sign_in_path_for resource
    organizer_path
  end
  
  def puts! arg
    unless Rails.env.production?
      puts '+++ +++'
      puts arg.inspect
    end
  end
  
  # Get locale from top-level domain or return nil if such locale is not available
  # You have to put something like:
  #   127.0.0.1 application.com
  #   127.0.0.1 application.it
  #   127.0.0.1 application.pl
  def extract_locale_from_tld
    parsed_locale = request.host.split('.').last
    I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale  : nil
  end
  
  # Get locale code from request subdomain (like http://it.application.local:3000)
  #   127.0.0.1 gr.application.local
  def extract_locale_from_subdomain
    parsed_locale = request.subdomains.first || 'en'
    I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : :en
  end

  def extract_layout_from_subdomain
    if 'm' == request.host.split('.').first
      @layout = 'organizer'
    elsif 'm' == request.host.split('.')[1]
      @layout = 'organizer'
    else
      @layout = 'application'
    end
  end

  def default_url_options(options={})
    options[:locale] = I18n.locale || I18n.default_locale
    # options[:layout] = @layout || 'application'
    options
  end
  
  def set_lists
    @cities = City.list
    @tags = Tag.all.list
    unless current_user.blank?
      @galleries = Gallery.where( :user => current_user ).list
      @reports = Report.where( :user => current_user ).list
    end    
  end
  
  def set_defaults
    # I18n.locale = extract_locale_from_tld || I18n.default_locale
    # @locale = I18n.locale = params[:locale] || I18n.default_locale
    # @locale = I18n.locale = extract_locale_from_subdomain
    @locale = I18n.locale = params[:locale] || 'en'

    @layout = extract_layout_from_subdomain

    @domain = request.domain
    @site = Site.where( :domain => @domain, :lang => @locale ).first

    @display_ads = true
    @display_help = true

    if user_signed_in?
      @current_user = current_user || session['current_user']
      @display_ads = @current_user.display_ads
      @display_help = @current_user.display_help
      if session[:current_city].blank?
        unless @current_user.current_city.blank?
          session[:current_city] = {
            :name => @current_user.current_city['name_'+@locale.to_s],
            :cityname => @current_user.current_city.cityname
          }
        end
      end
    end
    
    unless session[:current_city].blank?
      @current_city = City.where( :cityname => session[:current_city][:cityname] ).first
    end

    @action_name = params[:controller].gsub('/', '_') + '_' + params[:action]
    @action_classes = "#{params[:controller].gsub('/', '_')} #{params[:action]}" # #{@locale}

    # for the application header
    @list_citynames = City.where( :is_feature => true ).list_citynames @locale.to_s

    # for the application_mini header
    @parent_tags = Tag.all.where( :parent_tag => nil )
  end

  def load_features args = {}
    @newsitems = []

    if args[:cityname].blank?
      features = YAML.load_file("#{Rails.root}/config/features.yml")
    else
      features = YAML.load_file("#{Rails.root}/config/#{args[:cityname]}_features.yml")
    end
    @features = []
    features.each do |f|
      @features << f.symbolize_keys
    end
  end

  def set_new_for_organizer
    @addressbookitem = Addressbookitem.new
    @cities_user = CitiesUser.new
  end

#  def redirect_mobile_user
#    if is_mobile_device? && !(request.host.split('.').include? 'm')
#      lang = (@locale == 'en') ? '' : "#{@locale}."
#      port = (Rails.env.development?) ? ":#{request.port}" : ''
#      redirect_to "#{request.protocol}#{lang}m.#{request.domain}#{port}#{request.path}"
#    end
#  end
  
end
{'fraction_non_alphanumeric': 0.12447899064999436, 'fraction_numerical': 0.011039765686605835, 'mean_word_length': 3.282682103232031, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '5312536', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2942, 'n_words': 583}